Skip to content

Built-in methods get real bodies and declared signatures in the standard library (D110); delete the freeze door and the method-value adapter #3483

Open

Description

@ehartford

Eric, 2026-11-19 (the freeze door: "A now, B as the direction").

Today. The methods the compiler implements itself (List.len, List.push, HashMap.get, str.to_upper, Option.unwrap, the float math methods, …) have no declaration: their parameter modes are rows in src/BuiltinSigs.w, their receiver modes a table in Sema.builtin_method_receiver_mode, their bodies MirIntrinsics. To name one as a value (D223, List[i32].len), Sema writes the closure (x, a) => x.method(a) after the syntax tree is frozen, through one purpose-built entry, AstPool.add_method_value_closure.

Wanted. Each built-in method is a method declared in the standard library, with its signature written once in With (D110: "builtin container methods have declared signatures") and a body that calls the intrinsic. Then Type.method is an ordinary method value, and calls, values, move checking and lowering all read the one declaration.

Definition of done.

  • Every row of BuiltinSigs.w and every case of builtin_method_receiver_mode is a declaration in lib/std; both tables are deleted.
  • AstPool.add_method_value_closure, add_after_freeze, adapter_node_count, Sema.builtin_method_as_value, method_value_closures, method_adapter_param_types and the freeze-door-check gate target are deleted. Nothing adds to the tree after the freeze.
  • behav_d223_builtin_method_values passes unchanged.
  • No slowdown of a call to a built-in method (the body inlines to the intrinsic).

Activity

  1. ehartford commented on Nov 20, 2026

    @ehartford
    ContributorAuthor

    First step merged (#3489): List.len is declared in lib/std/collections.w. What the pilot established:

    • A declaration in std takes precedence over the built-in, at a call and as a method value, with no compiler change. So the migration is one method at a time.
    • Run time is unchanged: 300 million calls in a loop take the same time (the body inlines at -O1).
    • Compile time is unchanged: the self-check is 106 s with the declaration, 107 s without.

    Plan for the rest: move each built-in method to a declaration in std, deleting its BuiltinSigs row and its case in builtin_method_receiver_mode in the same change; methods whose bodies need storage the library cannot reach yet (growth, hashing) call the intrinsic from their declared body. When the last one has moved, delete the freeze door, the adapter and freeze-door-check.

  2. ehartford commented on Nov 21, 2026

    @ehartford
    ContributorAuthor

    A constraint found by :release-checks (2026-11-20), which stopped the third step (str.contains, starts_with, ends_with declared in std.string with their built-ins deleted; branch builtin-bodies-3, not merged):

    Units compiled without the prelude call str methods and rely on the built-ins: the Clang bridge object (--no-prelude), and a migrated corpus's generated test harness (stc-test: ctest_assert_str). With the built-in gone, the method resolves to the std.string declaration, whose body that unit never checked, and lowering aborts with BUG: user generic call lacks a concrete contract … sig=-1.

    Two things follow:

    1. Before a str (or any primitive's) built-in method is deleted, a unit without the prelude must still reach its std declaration: the module that declares a primitive's methods has to be loaded and checked on demand, or such units must import it. A corpus cannot be edited (D112), so it has to be the first.
    2. The abort is a tool that stayed silent: a call that resolves to a declaration with no checked body should be a diagnostic naming the missing import, not an ICE in lowering.

    List.len and List.is_empty (merged) are not affected: :release-checks was green for both.

  3. ehartford commented on Nov 21, 2026

    @ehartford
    ContributorAuthor

    Every str method is now a library declaration (#3489, #3490, #3495, #3496, #3504, #3507): thirteen built-ins moved, two on List (len, is_empty) and eleven on str (contains, starts_with, ends_with, index_of, to_upper, to_lower, replace, repeat, split, byte_at, slice), each with its compiler copies deleted in the same change.

    What each move had to touch, for the ones still to do: the entry in builtin_method_intrinsic; the name-based return-type table (compute_builtin_intrinsic_method_return_type), which otherwise types a call that has nothing to call; the BuiltinSigs row; the MirIntrinsic and CcBuiltin members with their arms in both code generators; any special case elsewhere (a MIR validator rule for slice/byte_at, the copy-on-write list for split); the C header prototype when the runtime function takes a list out-pointer; and err_2358_builtin_extra_arg, which needs an example that is still built in.

    Left: the rest of List, HashMap/HashSet/SlotMap, Option/Result, the numeric methods, channels and fibers. Then the freeze door and the method-value adapter go.

  4. ehartford commented on Nov 21, 2026

    @ehartford
    ContributorAuthor

    Fourteen moved; the thin ones are done. List.join landed in #3510 (declared for List[str]; it exposed #3508, fixed in #3509).

    What is left is not a runtime call behind a name:

    • List core (push, get, pop, remove, clear, slot, range, split_at, iter_place, get_disjoint, contains): the code generator emits element-size arithmetic and per-element drops around the runtime call. A library body needs those spelled in With over the list's storage.
    • Channels and fibers (send, recv, close, cancel, is_done, was_cancelled): raw calls on handle types the compiler knows by name; their types need library declarations first.
    • FixedString, SlotMap, the slot types, the numeric methods (rotate_left, popcount, min, max, abs, mul_add): each maps to an LLVM operation, not a runtime function.

    D110 asks for a real body and a declared signature. For the numeric ones the body is an intrinsic operation; for the List core it is With code over raw storage. I will write that split up as the plan for the rest before moving more.

  5. ehartford commented on Nov 25, 2026

    @ehartford
    ContributorAuthor

    The plan for the rest, as promised (2026-11-25, main 8509133). Fourteen are moved; BuiltinSigs still has 93 rows. They fall into four families, and each needs a different thing.

    1. Numeric methods (rotate_left, rotate_right, swap_bytes, bitreverse, popcount, clz, ctz, abs, min, max, mul_add, the float math functions)

    Each is one operation over every integer width (or both float widths). A library declaration needs one of:

    • a declaration per type: twelve integer types times ten methods, each body the same line. It works today and is exactly the repetition the mission is against.

    • one declaration for every integer type. Both spellings work today, when the trait declares its receiver (D7). Checked on main: impl[T: Ord] Smaller for T: fn least(other: T) -> T: if *self < other: *self else: other serves i32 and i64, and a trait default method with one impl per type does too. (An earlier version of this comment said neither worked. My probes had left the receiver out of the trait, which makes the method an associated one; the fault was mine. What it did turn up is real and separate: Eq, Ord and Default were not implemented for i8, i16, u16, u32 and u64, fixed on its own.)

      So the numeric family needs no new language surface for min and max: an integer trait with the methods as defaults, or a blanket impl over Ord.

    • And the body itself: min/max are plain With (if self < other: self else: other compiles to the same select). The bit operations need the operation to be nameable from With: a small set of generic intrinsic functions in std (bits.rotate_left[T](x, n), …) that the method bodies call, lowered as the MirIntrinsic is today. abs returns the unsigned counterpart of its receiver, which a signature cannot say yet (-> Unsigned[Self]).

    The one question in this family that is yours: how abs's return type is written. Today it is a rule in the compiler (unsigned_counterpart). Prediction: an associated type on the integer trait (Self.Unsigned), 70%.

    2. The List core (push, get, pop, remove, clear, slot, range, split_at, get_disjoint, contains, with_capacity), ListSlot, ListRange

    The code generator emits element-size arithmetic and per-element drops around the runtime call. A library body is With over the list's storage, as Stack and Deque already are over theirs. It needs List[T]'s representation declared in std (it is the compiler's today) and sizeof[T], raw element access and drop-in-place in the body. Nothing to rule. The risk is speed: the done-when says no slowdown, so each move is measured on the compiler's own check time and the pipeline benchmark before it lands.

    3. Iter adapters (22 rows: map, filter, fold, …)

    These have library bodies already in spirit (D69's push generators); the rows exist for the intrinsic fast path. This is step 3 of the ruled iterator campaign, family by family, and what #2220 (collections at compile time) waits on.

    4. Channels, fibers, SlotMap, FixedString

    Handle types the compiler knows by name. Their types get library declarations first, then their methods are ordinary extern-backed bodies like the str ones.

    Order

    2 and 4 need no ruling and no new language surface, so they go first: SlotMap and FixedString (4), then the List core (2) with measurements. Then family 1's min and max through a trait, and 3. The freeze door and the method-value adapter are deleted when the last row goes.

  6. ehartford commented on Nov 25, 2026

    @ehartford
    ContributorAuthor

    FixedString is moved (#3611): ten methods are library declarations, new stays built in (it needs a zeroed value of a generic type). About 200 lines of compiler copies are gone, and the move closed two soundness holes in the built-in as_view (it returned a str aliasing the buffer; now -> &str from self).

    Also on the way: Eq, Ord and Default for the five integer widths that lacked them (#3610).

    BuiltinSigs rows left: 90 (93 before this; my plan above said 59, which was a miscount, since corrected). Next in the posted order: SlotMap.

  7. ehartford commented on Nov 25, 2026

    @ehartford
    ContributorAuthor

    Two more moved (2026-11-25):

    BuiltinSigs rows left: 87 of 93. Next: SlotMap's ownership-moving methods, then the List core.

  8. ehartford commented on Nov 28, 2026

    @ehartford
    ContributorAuthor

    Progress (merged in PR #3674): moved f32/f64 min and max onto impl MinMax in lib/std/traits.with (IEEE 754 minNum/maxNum NaN semantics), removed float.min/float.max from src/BuiltinSigs.with, and deleted MirIntrinsic.MIN/MAX and CcBuiltin.MIN/MAX from Sema, LLVM codegen, and C codegen. BuiltinSigs rows left: 78 (77 pre-existing + List.new_in added in #570).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions