Repository navigation
Description
Bug: todo()/unreachable() lower to LLVM unreachable, not a panic (§29.10, §4.10, §14.11.1)
Summary
todo() and unreachable() are specified as Never-typed diverging builtins, but the LLVM backend special-cases them to bare unreachable instead of calling the panic substrate. That gives undefined behavior/no useful diagnostic at runtime. The fix should make them ordinary Never-typed stdlib functions that panic, remove the codegen special case, and route implicit unreachable paths through the same panic behavior.
Spec
docs/with-specification.md:10939-10945 defines todo() and unreachable() as divergence-oriented builtins with type Never, accepting zero or one str-compatible message and acting as diverging control-flow points. docs/with-specification.md:2214 onward describes implicit unreachable paths after §4.10 return analysis. The requirements campaign also ties this to 14.11.1.28, where lib/std/task.w:60-68 currently uses todo("await_first: empty input").
Current state
src/CodegenDispatch.w:8950-8984special-cases generic calls whose callee symbol issym_todoorsym_unreachable; atsrc/CodegenDispatch.w:8967-8969it emitswl_build_unreachable(self.builder)directly.src/CodegenDispatch.w:10198-10221lowersTermKind.TK_UNREACHABLEto raw LLVM unreachable atsrc/CodegenDispatch.w:10219-10221.- The C backend maps
TermKind.TK_UNREACHABLEtoabort()atsrc/CCodegen.w:7078-7079, so backend behavior is already divergent. - Panic substrate exists:
rt/rt_core.w:737-754implementswith_panic_core, writespanic: <msg> at <file>:<line>to stderr, and exits withrt_exit(1). lib/std/builtins.w:19declaresextern fn with_panic(msg: str, file: str, line: i32) -> void.lib/std/builtins.w:55-83implementsassert,require,check, and assertion helpers by callingwith_panic, usingsrc()defaults for the first three.lib/std/builtins.wcurrently has no ordinarypub fn panic,pub fn todo, orpub fn unreachabledeclarations in the scanned file; the names are handled by compiler special cases.- Exit-code expectations are inconsistent:
rt/rt_core.w:754exits 1, whiletest/behavior/behav_checked_overflow_panic.w:1-2expects exit 134 for overflow panic andbuild/selfhost.w:341writes a harness self-test expecting exit 134 fromassert(false). rt/rt_core.wis seed-sensitive under AGENTS bootstrap rules; runtime and link changes must be staged carefully.
Implementation plan
- Add ordinary stdlib declarations in
lib/std/builtins.w:pub fn panic(msg: str, loc: str = src()) -> Neverpub fn todo(msg: str = "not implemented", loc: str = src()) -> Neverpub fn unreachable(msg: str = "unreachable", loc: str = src()) -> Never
Each should callwith_panic(msg, loc, 0)and then be typed/lowered as diverging. Ifwith_panicitself can be typed asNever, prefer that after codegen supports noreturn externs.
- Mark
with_panicas noreturn/diverging in codegen.src/CodegenDispatch.w:12742-12778creates and calls the runtime panic function; ensure generated LLVM has the right noreturn/unreachable semantics after the call. - Delete the
sym_todo/sym_unreachablebare-unreachable special case atsrc/CodegenDispatch.w:8967-8969; let normal function-call lowering handle the stdlib functions. - Route §4.10 implicit
TK_UNREACHABLEthrough a panic call with a useful message instead of raw LLVM unreachable. Keep genuinely proven-impossible compiler-internal blocks separate if any are required for optimization. - Unify panic exit-code policy before locking tests. Either make
with_panic_coreexit 134 to match current test directives, or update all panic expectation tests to the chosen value in one change. Do not leave assert/overflow/todo panics with inconsistent exit codes. - Update C backend behavior for user-facing unreachable/todo paths as needed so it calls the same panic surface or at least produces the same observable message/exit contract.
Test plan
- Behavior tests with directives for:
todo()default message,todo("custom"),unreachable()default message,unreachable("custom"),panic("custom")if publicpanicis added with the same substrate.
- Test that
todo()/unreachable()type asNeverin value position, e.g.let x: i32 = todo("x")type-checks. - Regression test for
await_first([])or the relevant empty-input task path once task tests can exercise it. - Test implicit-unreachable fallthrough from §4.10 produces a panic rather than raw undefined behavior.
- Run bootstrap-sensitive gates in the staged order required by AGENTS if
rt/rt_core.wchanges:with build,with build :fixpoint,with build :test, then seed/install targets only after committed verified code.
Acceptance criteria
-
todo()andunreachable()call the panic substrate and print useful diagnostics. - Both functions remain
Never-typed and usable in any expected value position. - Codegen no longer lowers user
todo/unreachablecalls to bare LLVMunreachable. - §4.10 implicit unreachable paths use a loud panic path.
- Panic exit codes are unified across assert, overflow, todo, unreachable, and test directives.
- Runtime changes respect the repository bootstrap sequencing rules.
Dependencies
Needs split or at least careful staging: stdlib/codegen lowering can be separated from panic exit-code unification, and any rt/rt_core.w change must follow AGENTS bootstrap rules. Coordinate with #428 if bounds-check tests assert exact panic exit behavior.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 14.11.1.28, 29.10.1.1, 4.10.1.21.
Activity
Requirements triage campaign: checkbox, verdict, and issue-link every…
ehartford commentedon Jun 12, 2026
Implemented in f7a420e.
panic,todo, andunreachableare now ordinary std.builtins functions returningNever; user calls no longer lower to raw LLVM unreachable, backend-generated unreachable terminators route through runtime panic, and LLVM/emit-C treatNeveras a void ABI type. Added focused behavior and compile-error coverage for panic messages,Neveruse in value position, arity, named-argument rejection, and non-string messages. Verification passed after rebasing onto current main:WITH=$PWD/out/bin/with ./out/release/bin/with build,:fixpoint,:test, and:test-green. Note: directawait_first([])behavior coverage is blocked by newly filed #558, where the existing std.task genericimpl IntoIter[Task[T]]signature does not instantiate.