Skip to content

ADR 0066: An optional of a pointer type is a pointer that may be null

Status: Final Type: Feature Created: 2026-10-27 Amends: 0029 (Representation: a union of None and one pointer-held case is not boxed) Related-ADRs: 0038 (narrowing, which becomes free), 0007 (the placeholder, which is never read), 0024, 0026 (the slot rule, unchanged), 0065 (a class set with None), 0058 (a heap tuple, an instance of its class), 0015 (bytes | None), 0056 (the export boundary). Provenance: the Shed Skin census (kanoodle).

Context

ADR 0029 holds a union in a counted heap box: a reference count, a tag, one payload. For C | None the box holds nothing the payload pointer does not: the tag says instance or None; a null pointer says the same. kanoodle, compiled past its last refusal, printed CPython's output and ran at less than half of CPython's speed (notes/measurements.md, 2026-10-26). The dancing-links loop stores a Column | None into a Column | None field on nearly every line; each store allocated a box and freed the one it replaced, and allocation dominated a sample of the run. The census's other linked structures (richards, bh, chaos, go, loop) have the same shape. None itself was a fresh box per use, so even self.right = None allocated.

Three facts make a null pointer usable as None. The release and retain helpers of the counted types already return at once on NULL, and so does copy.deepcopy's walker. NULL means nothing else a program can observe. ADR 0007's placeholder is never read, because a caller tests the pending exception before it looks at a result. A field or name that may be unbound is tracked by a bound flag (0033, 0023), never a NULL test. Nor does any list, dict or set helper take NULL as a value. Strings are the exception: the str helpers for a channel crossing and for the perimeter map NULL to the empty string, so a null that reached either would print "" where CPython prints None. That fact shapes rule 2.

Decision

  1. The thin optional. A union of None and exactly one case held as a pointer (an instance, a class set of ADR 0065, a heap tuple, str, bytes, list[T], dict[K, V], set[T]) is that pointer, NULL for None. It needs neither a box nor a tag word, and None allocates nothing. Any other union keeps ADR 0029's box: a scalar case (int | None, float | None, bool | None), two non-None cases (str | list[int], int | str), and a union without None.
  2. It is its own IR type. The IR gains an optional type per pointer type, lowered exactly as the pointer it wraps. Only one operation goes from the optional to the pointer: a narrowing that is free at run time and is emitted only where ADR 0038 narrows, under a null test or an assert. The verifier's runtime-call check (IR13) then refuses any optional handed to a helper that wants the pointer, the two NULL-to-empty str helpers included, so an un-narrowed None reaching a helper is a compiler bug the verifier catches, where it would otherwise print an empty string. Widening goes the other way, also free: a C into a C | None slot is the same pointer.
  3. The operations, on the pointer.
  4. x is None, x is not None, x == None, match with case None:: a null test. case C() as c:, case str(s):, case list(xs):: the other branch, narrowed.
  5. The truth of x: False when null, otherwise the payload's truth as ADR 0029 defines it (an instance is true; a str or a container by its length).
  6. x or d: the pointer when it is non-null and true, otherwise d.
  7. == and != between a C | None and a C (ADR 0038): pointer identity when no __eq__ is defined. A tuple's optional and a tuple compare by value, the tuple's rule, after the null test: None equals only None.
  8. print, str, repr, f-strings: None when null, the payload's text otherwise.
  9. d.get(k) with pointer values: the found pointer, retained as the slot rule requires, or NULL.
  10. A method call, a field read or an index through an optional the program has narrowed: the narrowed pointer. Unnarrowed, it is refused and the message names the guard to write. ADR 0038 rechecks a narrowed field at each read, so that a call that reset it raises CPython's AttributeError; that recheck becomes a null test on the field's slot.
  11. Ownership is the pointer's own. A thin optional slot owns one reference to its payload when non-null, and releases it with the payload type's own helper, which already returns on NULL. The slot rule (ADR 0026) gains no row: an Item | None slot is an Item slot that may hold NULL. A field of type C | None has the slot kind of C; a list[C | None] has C's element kind with NULL elements allowed. The object and list runtimes release by kind through NULL-tolerant helpers; copy.copy and copy.deepcopy copy a NULL as NULL.
  12. The export boundary. An instance has never crossed the stable C ABI (PP501), so neither does an optional of one, nor a boxed union, which is refused with PP501 too. A str | None parameter or result is the const char * a str is, NULL for None, and the generated header says which. A compiled function called from CPython takes None as NULL and returns NULL as None, and refuses an argument that is neither a str nor None with TypeError, as CPython's z argument code does.
  13. Unchanged. A union at a task boundary stays refused (A69). A thin optional is still a union in the language: everything ADR 0029 and ADR 0038 accept or refuse about C | None stands; only the representation changes.

A heap tuple's optional needs no further decision, since a heap tuple is an instance of a synthesized class (0058). tuple[T, ...] | None resolves wherever an annotation does, beside None alone; a tuple beside a class or a second tuple is refused, since a tuple class has no tag a class set dispatches on. A tuple literal assigned to a name bound as the optional is stored as the case, not into 0045's hidden component slots.

Each kind of payload is held by a CPython differential on the three native backends. Release IR for programs without optionals is unchanged by this representation.

Consequences

  • No allocation for None, and none for widening a pointer into an optional; a store into an optional field is a retain and a release, as for a plain C field. kanoodle went from about twice CPython's time to a fraction of it on the C backend; the LLVM backend gains less, since the object-unit helpers stay calls there (notes/measurements.md, 2026-10-27).
  • A representation change refuses and admits nothing: the census's refusals did not move at any step. A probe of the container operations accepted and refused the same set before and after.
  • The union counters no longer count thin optionals; the object counters, which still balance, show that their instances come back.
  • Two representations for one language type: the frontend has to say which one a union is, at every site that builds, reads or releases one. The IR type of rule 2 makes a site that forgets fail verification before it can read a pointer as a box.
  • ADR 0029's named successor for the hot path, an immortal None box, is still owed to the scalar optionals (int | None), which this ADR leaves boxed.

Alternatives considered

  • Reuse the pointer's IR type (no distinct optional type). Fewer types, but nothing would stop an un-narrowed optional str from reaching a NULL-to-empty helper and printing "": exactly the class of silent wrongness the verifier exists to make impossible.
  • Instances only (A164's alternative). A second representation rule later for str and containers, and str | None is common in the census's fields.
  • An immortal None box for every union. It removes the allocation for None but keeps one per widening, which is the cost kanoodle pays.
  • Keeping the box at the export boundary. More code in the wrapper for an ABI that no outside code depends on yet (A164).