Design decisions

30. Front-end recovery (2026-11-21)

  • 28.8 impl Trait for a built-in type; Hash on it. impl Hash for Int: fn hash(self) -> U64 = .. gives Int a method x.hash(), found by the value's type after fields unrolls or a generic is instantiated; before, an impl attached only to structs. std.derive uses it for a Hash trait over Int, U64, Char, Bool and Str (splitmix64) and hash_fields, so impl Hash for P: fn hash(self) = hash_fields(self) is a struct's whole derive. Reason: the derive must be library code, and a field-wise fold needs one method name that dispatches on each field's type; free fns cannot overload. Not yet: the compiler's own HashMap key hash does not call Hash. Reverse: delete attach_impls_for_builtins and prim_methods in cpl-ir/src/lower and the Hash part of stdlib/derive.cp.
  • 28.9 A struct built field by field: P(e for f in fields(..)). A struct constructor whose one argument is a generator over fields runs e once per field, in order, with that field's type expected, and builds the struct from the values: P(read() for _ in fields(P)) reads one, P(x + y for (x, y) in fields(a, b)) adds two. fields(P) names the type being built and binds nothing (_); values must be of that type. It is the construction half of reflection (28.1 walks fields, this makes them), so Read and field-wise maps are library code: std.derive.read_fields<T>() = T(read() for _ in fields(T)). A struct with a new still goes through new, so its invariants hold. Rejected: T(..tuple) spread (..t already means a range) and fields(mut x) (needs a starting value, and a struct has none). Reverse: delete fields_ctor in cpl-ir/src/lower/call.rs and cpl-check/src/check/call.rs.
  • 30.1 A broken-ports benchmark scores recovery. crates/cpl-cli/tests/recovery.rs applies six single-line edits (drop a closer, drop a block colon, cut a value, double an operator, garble a declared name) to every port and counts errors, cascades (every error after the first, since each copy has one root cause) and whether the first error is on the broken line. It gates no silent copies, first-error accuracy >= 99.5% and cascades per broken copy under a ratchet. The ratchet is a rate, not a count, because port lanes keep adding ports. Reason: "no cascades" needs a number. RECOVERY_SHOW=<edit name> prints every copy that cascades under that edit. Reversible: delete the test and its scripts/check.sh step.
  • 30.2 Lexer diagnostics are pushed before the lexer's poison. Reason: the poison exists to keep other errors off a reported span; applied first it also hid the report itself, so let $x = 1 checked clean. Reversible: revert the sink order in cpl-check/src/db.rs.
  • 30.3 The parser reads past an unreadable character inside a statement. bump skips T::Error tokens that do not start a line and marks the statement recovering, so it is poisoned. Reason: the character is already reported; dropping the whole line also dropped the names it declares. A line-initial error token still drops the line, since nothing says what statement it was. Reversible: remove the loop in Parser::bump.
  • 30.4 A function whose header has a syntax error stays bound with an unknown type. FileCtx::def maps the defs of a statement poisoned on its first line through Def::recovered, which gives functions Ty::Error. Reason: a header missing its ) parsed as a different signature (fn ask(d: Char, dist: Int -> Int: takes a fn(Int) -> Int), and every call reported a mismatch. Values keep their inferred type: a broken initializer already infers Ty::Error where it is broken, and var total = 0 + should still hover as Int. Struct and enum headers keep their parsed shape (open: a struct missing its ) still reports every later field use). Reversible: drop the head_is_poisoned branch in FileCtx::def.
  • 30.5 Planned syntax is one table, parsed and reported as E0504. cpl-syntax/src/planned.rs lists each 0.3 form this version does not implement: its feature name, the design section that specifies it, what to write today, and an example whose one error is that one. The parser reads the form (so the statement and its names survive), reports "not implemented yet: (see
  • 30.6 Golden diagnostics are rendered cpl check output. cpl-cli/tests/diagnostics/*.cp with the exact stderr beside it in .err; planned-syntax examples are written there from the table. BLESS=1 rewrites them. Reason: the rendered text is what the contestant reads, including help lines and spans, so that is what is pinned. Reversible: delete the directory and diagnostics.rs.
  • 28.10 A struct's ? borrows a field's niche; no Niche trait. ?S for a struct with an Int field (not narrowed below 64 bits) or a position field stores none as that field at INT64_MIN or -1, the value no S holds (decision 49.1), so ?Edge is as wide as Edge: 16 bytes for Edge(to: Int, w: Int), not 24. The first such field is used, fixed per type (spec 22: niches are fixed per type, never per use). It majorizes std::optional<S> (narrower, same operations), so it is implicit. A user Niche trait naming a none value was rejected: every struct that has a free value already has one in a field, and a trait would let a library claim a value its own code can produce, turning a wrong none into silent corruption. Interpreter unchanged. Measured (scoreboard.py --full, 7 runs, same machine): cf2147f 604 -> 420 ms (2.84x -> 1.80x matched C++), RSS 24.2 -> 19.9 MB; cf2057d RSS 9.9 -> 8.7 MB. Emitted C++ grows about 1.1 KB on the six ports that use it (the OptField runtime piece), so the baseline was rewritten. Reverse: drop the Ty::Struct arm of Ty::Opt in cpl-codegen/src/names.rs and the niche_field runtime piece.
  • 28.11 A trait writes its own unimplemented message. A call f(x) whose T: Tr bound x's type has no impl Tr for now fails at the call, `Bool` does not implement `Tr`, instead of later inside f's body as "no method"; a trait declared with @on_unimplemented("...{T}...") adds its text as the help, {T} the type shown (spec 14 writes #on_unimplemented; @ is the one decorator sigil, CORE-ALIGN). Any library can declare it, so the stdlib has no privilege (std.derive.Hash uses it to point at hash_fields). Only traits declared in a module are checked; an impl over a generic type counts as met, so no valid program is newly rejected (291/291 ports pass scoreboard.py --fast). Test: diagnostics/trait_names_its_own_fix. Reverse: return true from implements in cpl-ir/src/lower/mono.rs.
  • 28.12 Gen is a trait of static fns, and a generic call takes its return type from the type wanted. trait Gen: fn gen(mut r: Rng) -> Self has no self; T.gen(mut r) inside fn gen<T: Gen> calls the impl of whatever fills T, built-in types included. A generic fn whose type parameter appears only in its return type (fn gen<T: Gen>(mut r: Rng) -> T) takes it from the expected type, so let x: Int = gen(mut r) and P(gen(mut r) for _ in fields(P)) draw each field by its own type; before, such a parameter silently defaulted to Int. gen_fields<T> is then one line of std.derive, and impl Gen for S: fn gen(mut r: Rng) -> S = gen_fields(mut r) derives a generator for a stress test. Same seed, same values in cpl run and the C++ (programs/derive_gen). Reason: generators derived from types are the stress goal, and the missing pieces were general (static trait calls, return-type inference), not generator-specific. Reverse: drop call_hint in cpl-ir/src/lower/mono.rs and the built-in arm before match self.sym(base) in Lowerer::method.
  • 28.13 read() of a struct calls its impl Read. A struct whose impl Read declares fn read() -> S is read through it wherever read() meets it: alone, as an array element, as a field of another struct. The read plan (Spec) gains one node, Spec::Call, holding the lowered call, so the interpreter evaluates it and the C++ emits it like any expression. Before, the impl was accepted and silently ignored, and the input was read field by field (silent wrongness). Test: programs/read_impl (1-based input read as 0-based cells). Reason: spec 00 makes Read a library trait with return-type dispatch; this is the user half of that. cpl stress still generates such a struct's input field by field. Reverse: drop the read lookup at the top of struct_spec in cpl-ir/src/lower/mod.rs.
  • 28.14 ?? stays core. The §1 queue listed ?? for the stdlib on lazy parameters; it stays a core operator. Its right side may leave the enclosing fn or loop (read_opt() ?? return, ?? continue), and a lazy argument is a closure, where return would leave only the closure. Spec 00 places ?? in core C4 for exactly this ("laziness and Never"). A library fn or<T>(x: ?T, lazy d: () -> T) -> T would be a second spelling of the same idea with less power. Reverse: give lazy arguments non-local return/break (a core change), then define ?? as that fn.
  • 30.7 Dreams are a diagnostics corpus. scripts/dream-gaps.py checks every proto/ports/*/dream/*.cp and files each error under the gap it names: syntax: (an E0504 feature), library: (a name, module or method "not in the stdlib (yet)"), import: (a stdlib name used without its use), rule: (a decided rule the dream breaks), backend: (an E0501 the interpreter or codegen can't run yet), or unnamed:. The scoreboard prints the ranked summary. The bar is that every dream's first error names its gap. Reason: the factory needs to see which missing construct blocks the most dreams, and a dream whose first error is "expected X" teaches nothing. Reversible: drop the script and the scoreboard's dreams block.
  • 30.8 A planned form outside the design still gets E0504, marked "(not in the design)". Dreams reach for forms the design never specified (fn (a: T) + (b: T), by: <, Str of "01?", enum E: a = -1, e is x, var a, b: T = 0). Each is read, reported once by name with today's spelling as help, and recovered as today's nearest form. Reason: naming the feature is the useful message, and the marker keeps the table from passing these off as designed; a design section replaces the marker when the design takes the form up. Reversible: make see mandatory and turn those entries back into parse errors.
  • 30.9 "Not in the stdlib (yet)" goes on names unknown everywhere, except one-letter names. An unresolved name with no close in-scope or prelude name, no stdlib module and no removed keyword gets the suffix, plus a did-you-mean against stdlib names (with the use as a fix) when one is close; a missing std. module or a name a std. module lacks gets it too. No stdlib export is one letter and contest code is full of one-letter locals, so no w in scope stays bare. Reversible: the unknown condition in Checker::unresolved and the two branches in db.rs.
  • 30.10 const A = 1, B = 2 declares several constants; a field used in a method without self says so. The first is how dreams write related limits, costs nothing to read (each becomes its own const, so every tool sees ordinary declarations); the second turns a bare "not in scope" into the fix (add self). Fields already resolve bare inside methods that take self. Reversible: the while after the first const in parser/stmt.rs; the field_of_self branch in Checker::unresolved.
  • 30.11 C spellings (&&, ||, !x, ..<) are errors with the cp spelling as the fix, and parse as that spelling. Reason: they are the commonest slips from C++ and Python, the fix is exact, and recovering as and/or/not/.. keeps the rest of the file checked. A single & or | stays bitwise. Reversible: Parser::c_logic and Parser::range_op in cpl-syntax.
  • 30.12 cpl fix applies the fixes errors carry first, then lint fixes. It is the editor's quick fixes run in a loop (at most 8 rounds); once no error is left it also makes the fixes cpl lint --fix makes, which the lint codemods (L0204, L0415, L0510, L0511) document as cpl fix. Assigning to a let fixes the declaration (let becomes var), and only the first assignment to one let is reported. A plain number where a domain is needed gets V(...) as its fix. Reason: one command for every machine-applicable fix; lint fixes wait for a file without errors so a codemod never rewrites a program that does not check. The fix-it benchmark measures error fixes alone (apply_error_fixes). Reversible: cpl_cli::apply_error_fixes and apply_lint_fixes; drop the ASSIGN_TO_LET de-duplication in db.rs.
  • 30.13 Fix-its are measured on a second benchmark of common mistakes. The broken-ports edits (cut a value, drop a closer) have no single right fix, so fix_it_scoreboard plants one mistake per copy instead (C spellings, let for a var, a misspelled member) and counts errors with a fix and copies a cpl fix restores byte for byte. Ratchet: fix-its on at least 88% of errors and restoring at least 95% of the copies that report one. Only this benchmark applies every fix: the broken-ports scoreboard counts fix-its but skips the rewrite, which kept the recovery test inside the fast gate's time. Reversible: delete fix_it_scoreboard.
  • 30.14 A domain or range mismatch labels where each side's index space comes from. The message already names both spaces (a is indexed by V, but n is a 1..=9); a label on each plain name's declaration says where each got it (a is declared over V here, n is declared as a 1..=9 here). A top-level name declared in another item is labelled as a point at its use, and the database moves the label onto the declaration. Reason: the fix is usually at the declaration, not the use. Reversible: Checker::origin and the WRONG_DOMAIN branch in db.rs.
  • 30.15 After a lowering error, cpl run does not report names it does not know. The checker has accepted every name by the time lowering runs, so an unknown name there means an earlier lowering error dropped its binding (for (i, c) in s.item: loses i and c). Reason: one error per root cause; this cut the member-typo benchmark from 656 to 522 errors. Reversible: Lower::reported_an_error in cpl-ir/src/lower/mod.rs.