Skip to content

Bug: todo()/unreachable() lower to LLVM unreachable, not a panic (§29.10, §4.10, §14.11.1) #544

Closed

Description

@ehartford

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-8984 special-cases generic calls whose callee symbol is sym_todo or sym_unreachable; at src/CodegenDispatch.w:8967-8969 it emits wl_build_unreachable(self.builder) directly.
  • src/CodegenDispatch.w:10198-10221 lowers TermKind.TK_UNREACHABLE to raw LLVM unreachable at src/CodegenDispatch.w:10219-10221.
  • The C backend maps TermKind.TK_UNREACHABLE to abort() at src/CCodegen.w:7078-7079, so backend behavior is already divergent.
  • Panic substrate exists: rt/rt_core.w:737-754 implements with_panic_core, writes panic: <msg> at <file>:<line> to stderr, and exits with rt_exit(1).
  • lib/std/builtins.w:19 declares extern fn with_panic(msg: str, file: str, line: i32) -> void.
  • lib/std/builtins.w:55-83 implements assert, require, check, and assertion helpers by calling with_panic, using src() defaults for the first three.
  • lib/std/builtins.w currently has no ordinary pub fn panic, pub fn todo, or pub fn unreachable declarations in the scanned file; the names are handled by compiler special cases.
  • Exit-code expectations are inconsistent: rt/rt_core.w:754 exits 1, while test/behavior/behav_checked_overflow_panic.w:1-2 expects exit 134 for overflow panic and build/selfhost.w:341 writes a harness self-test expecting exit 134 from assert(false).
  • rt/rt_core.w is seed-sensitive under AGENTS bootstrap rules; runtime and link changes must be staged carefully.

Implementation plan

  1. Add ordinary stdlib declarations in lib/std/builtins.w:
    • pub fn panic(msg: str, loc: str = src()) -> Never
    • pub fn todo(msg: str = "not implemented", loc: str = src()) -> Never
    • pub fn unreachable(msg: str = "unreachable", loc: str = src()) -> Never
      Each should call with_panic(msg, loc, 0) and then be typed/lowered as diverging. If with_panic itself can be typed as Never, prefer that after codegen supports noreturn externs.
  2. Mark with_panic as noreturn/diverging in codegen. src/CodegenDispatch.w:12742-12778 creates and calls the runtime panic function; ensure generated LLVM has the right noreturn/unreachable semantics after the call.
  3. Delete the sym_todo/sym_unreachable bare-unreachable special case at src/CodegenDispatch.w:8967-8969; let normal function-call lowering handle the stdlib functions.
  4. Route §4.10 implicit TK_UNREACHABLE through 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.
  5. Unify panic exit-code policy before locking tests. Either make with_panic_core exit 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.
  6. 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 public panic is added with the same substrate.
  • Test that todo()/unreachable() type as Never in 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.w changes: with build, with build :fixpoint, with build :test, then seed/install targets only after committed verified code.

Acceptance criteria

  • todo() and unreachable() 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/unreachable calls to bare LLVM unreachable.
  • §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

  1. ehartford commented on Jun 12, 2026

    @ehartford
    ContributorAuthor

    Implemented in f7a420e. panic, todo, and unreachable are now ordinary std.builtins functions returning Never; user calls no longer lower to raw LLVM unreachable, backend-generated unreachable terminators route through runtime panic, and LLVM/emit-C treat Never as a void ABI type. Added focused behavior and compile-error coverage for panic messages, Never use 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: direct await_first([]) behavior coverage is blocked by newly filed #558, where the existing std.task generic impl IntoIter[Task[T]] signature does not instantiate.

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