Repository navigation
Description
Unreachable-code detection: goto and Never-call divergence (§20b.5)
Summary
Unreachable-code detection currently recognizes only three statement kinds as diverging (return, break, continue). Code after goto or after a call to a Never-returning function (panic, exit, todo(), unreachable()) is silently accepted, and the labeled-statement suppression rules of §20b.5 are vacuous because goto is never treated as a divergence source. This issue completes divergence detection so the spec's "all code after unconditional control flow transfers" rule actually holds.
Spec
§20b.5 Unreachable Code (docs/with-specification.md:10436):
The compiler detects unreachable code via control flow analysis and rejects it. This applies to all code after unconditional control flow transfers, including
return, labeled or unlabeledbreak, labeled or unlabeledcontinue,goto, and calls to functions with return typeNever(e.g.,exit(),panic()).
A labeled statement may be reachable only via
goto. Ordinary unreachable-code diagnostics are suppressed for a labeled statement and for following statements until the next control-flow terminator or the next labeled statement:
fn example:
goto 'done
print("never") // unreachable
'done
print("finished") // reachable via goto
Also relevant: §29.10 (todo()/unreachable() "are treated as diverging control-flow points for typing and reachability analysis") and the §20b.5 comptime-if exemption (the check runs after comptime evaluation; must not regress).
Current state
- Divergence detection lives in
Sema.check_block(src/SemaCheck.w:4356-4395). Per-statement,block_divergedis set only by node kind atsrc/SemaCheck.w:4382-4383:The "unreachable code" error is emitted atif stmt_kind == NodeKind.NK_RETURN or stmt_kind == NodeKind.NK_BREAK or stmt_kind == NodeKind.NK_CONTINUE: block_diverged = 1src/SemaCheck.w:4363-4365(statements) and4391-4393(block tail). NK_GOTOis type-checked atsrc/SemaCheck.w:2900-2906and returnsself.ty_void— it never marks divergence. Statements after agotoare accepted (requirement verdicts20.2.5.1/.3/.4: "goto/Never divergence undetected", "kind-based, not full CFA").- The type facts needed for
Never-call divergence already exist: each statement's type is computed atsrc/SemaCheck.w:4374(let stmt_ty = self.check_expr(stmt)),todo()/unreachable()returnself.ty_never(src/SemaCheck.w:11277),break/continueexpressions returnty_never(src/SemaCheck.w:2992,3005), and a user/stdlib function declared-> Neverresolves through theNeverprimitive (src/Sema.w:1284,src/SemaDecl.w:2227).check_blockeven consultslast_stmt_ty == self.ty_neverfor the block result type (src/SemaCheck.w:4390) — but never for divergence. - Labeled statements:
NK_LABEL(d0=label_sym, d1=statement,src/Ast.w:99-101) is checked transparently atsrc/SemaCheck.w:2897-2898. There is no suppression logic, which is currently harmless only becausegotonever setsblock_diverged(requirements20.2.5.5/.6verdict: "vacuous"). - The comptime-if exemption already works and is tested (
test/spec/spec_ss20b_6_comptime_unreachable_exemption.w; requirement20.2.5.7/.8verdicts ok).
Root cause: divergence is a syntactic node-kind whitelist instead of using the Never type fact that sema already computes for every statement.
Implementation plan
src/SemaCheck.wcheck_block(~4380-4388): extend the divergence condition:- keep the existing
NK_RETURN/NK_BREAK/NK_CONTINUEkind checks (return statements may not be typedty_neverin all paths); - add
stmt_kind == NodeKind.NK_GOTO; - add
stmt_ty == self.ty_never as TypeId— this covers calls toNever-returning functions (panic,exitonce typedNever,todo(),unreachable()) and any expression sema already types asNever(e.g. amatchwhose arms all diverge), giving the "control flow analysis" behavior the spec asks for without a separate CFG.
- keep the existing
- Labeled-statement suppression (same loop): before the unreachable check, if
self.ast.kind(stmt) == NodeKind.NK_LABEL, resetblock_diverged = 0(and leavereported_unreachableuntouched so each dead region reports once). This implements §20b.5 exactly: suppression starts at the labeled statement and lasts until the next terminator (which setsblock_divergedagain) or the next labeled statement (which resets again). The label's inner statement (d1) must still be checked for its own divergence (e.g.'done return x). - Audit
Neversignatures: verify that the diverging stdlib entry points the spec names (exit(),panic) are actually declared-> Never. Todaylib/std/libc.w:60declarespub extern fn exit(code: i32) -> void. Decide per-symbol with the maintainer's spec reading: §20b.5 explicitly listsexit()/panic()asNeverexamples, so retype the user-facing wrappers (lib/std/process.w, preludepanic) as-> Never. This is a stdlib-only change (nort//Link.whazard), but watch for knock-on type errors in callers that relied onvoid. - Determinism note: the new condition uses only per-statement AST order and the statement's computed type — no map iteration — so fixpoint determinism is unaffected.
- Do not regress the comptime exemption:
check_blockruns after comptime branch elimination, so no change is needed; the existing spec test guards it.
Edge cases to handle/verify:
gotoas the inner statement of a label ('top goto 'top) — divergence applies after the labeled statement.- An
if/elsewhere both arms diverge types asNeveronly if sema already computes that; if it does not, that is acceptable (kind-based + Never-based detection is what §20b.5 requires; do not build a full CFG here —src/BorrowCfg.wnotes its CFG is for future borrow analyses). - Error-message text: reuse the existing
"unreachable code"message for consistency with current tests.
Test plan
test/compile_errors/ (one error each, //! expect-check-fail: or //! expect-error: unreachable code):
err_unreachable_after_goto.w— statement aftergoto 'donewith no intervening label.err_unreachable_after_panic.w— statement afterpanic("boom").err_unreachable_after_todo.w— statement aftertodo()in a function body.err_unreachable_after_exit.w— statement afterexit(0)(onceexitis-> Never).err_unreachable_after_never_call.w— statement after a call to a user function declared-> Never.
test/spec/:
spec_ss20b_5_goto_label_reachability.w(//! expect-stdout: finished) — the exact §20b.5 example:goto 'done, deadprintremoved,'done, then reachable code executes; plus a case where the label is followed by another terminator and then a second labeled region (suppression window boundaries).- Re-run
spec_ss20b_6_comptime_unreachable_exemption.wunchanged (regression guard).
Negative-of-the-negative (must still compile): code after a conditional goto/panic inside an if arm.
Acceptance criteria
- Code after
gotois rejected with "unreachable code". - Code after calls typed
Never(panic,exit,todo(),unreachable(), user-> Neverfns) is rejected. - A labeled statement after a terminator is accepted, and statements following it up to the next terminator are accepted (suppression per §20b.5).
- Comptime-if-eliminated branches still produce no unreachable error.
- All new tests pass; no existing test regresses.
-
with build && with build :fixpoint && with build :testpass;with build :test-greenrecorded.
Dependencies
- Bug: todo()/unreachable() lower to LLVM unreachable, not a panic (§29.10, §4.10, §14.11.1) #544 (todo()/unreachable() lower to LLVM
unreachable, not a panic) — independent codegen-side bug; both cite §29.10. This issue only consumes theNevertype fact, so neither blocks the other. - Tests: labels goto edge cases coverage (§13) #506 (labels/goto edge-case tests) — overlapping test surface for goto + with-guard/defer interaction.
- Tests: with control flow coverage (§23, §7) #536 (with control flow tests) —
gotoinsidewithblocks; divergence detection must not falsely fire on the with-lowered form.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 20.2.5.1, 20.2.5.3, 20.2.5.4, 20.2.5.5, 20.2.5.6.
Activity
ehartford commentedon Jun 11, 2026
Implemented in 58043ae. Added unreachable-code detection after goto and after expressions/calls typed Never, preserved label/goto reachability, corrected unannotated return inference so ordinary bare-return helpers do not falsely diverge, and made std process/libc exit signatures return Never. Added positive and negative tests for goto, todo/unreachable, exit, user Never calls, labeled reachability, and unannotated return inference. Verification passed: build, fixpoint, full test suite, and test-green. The full suite hit one transient raylib package-fetch failure; cli-selfhost-project-tests passed on direct rerun and the full suite passed cleanly afterward.