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¶
- The thin optional. A union of
Noneand 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 forNone. It needs neither a box nor a tag word, andNoneallocates nothing. Any other union keeps ADR 0029's box: a scalar case (int | None,float | None,bool | None), two non-Nonecases (str | list[int],int | str), and a union withoutNone. - 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-narrowedNonereaching a helper is a compiler bug the verifier catches, where it would otherwise print an empty string. Widening goes the other way, also free: aCinto aC | Noneslot is the same pointer. - The operations, on the pointer.
x is None,x is not None,x == None,matchwithcase None:: a null test.case C() as c:,case str(s):,case list(xs):: the other branch, narrowed.- The truth of
x:Falsewhen null, otherwise the payload's truth as ADR 0029 defines it (an instance is true; astror a container by its length). x or d: the pointer when it is non-null and true, otherwised.==and!=between aC | Noneand aC(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:Noneequals onlyNone.print,str,repr, f-strings:Nonewhen null, the payload's text otherwise.d.get(k)with pointer values: the found pointer, retained as the slot rule requires, or NULL.- 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. - 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 | Noneslot is anItemslot that may hold NULL. A field of typeC | Nonehas the slot kind ofC; alist[C | None]hasC's element kind with NULL elements allowed. The object and list runtimes release by kind through NULL-tolerant helpers;copy.copyandcopy.deepcopycopy a NULL as NULL. - 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 | Noneparameter or result is theconst char *astris, NULL forNone, and the generated header says which. A compiled function called from CPython takesNoneas NULL and returns NULL asNone, and refuses an argument that is neither a str norNonewithTypeError, as CPython'szargument code does. - 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 | Nonestands; 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 plainCfield. 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
Nonebox, 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
strfrom 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
strand containers, andstr | Noneis common in the census's fields. - An immortal
Nonebox for every union. It removes the allocation forNonebut 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).