Description
Spec reference
- §9 Functions and Expressions —
if/whileconditions are boolean expressions; With has no truthiness or implicit conversion to bool (§4 Types). - §4.6 Type Inference — a Unit-typed expression in bool position is a type error the checker must reject.
Minimal reproduction
fn g():
print("side effect")
fn main:
if g(): print("yes")
else: print("no")
with check reports ok. with build succeeds. The binary prints side effect, then dies with SIGTRAP (rc=133) — the branch is lowered as a switch on a value the void call never produced.
Same result with while g(): ... — checks clean.
Root cause (exact line)
Sema.check_if_expr (src/SemaCheck.w:8016) checks the condition at line 8025:
self.check_expr(cond)
The result type is discarded; there is no comparison against bool anywhere in the function (the only condition diagnostic in Sema is the comptime-evaluability one at SemaCheck.w:12105). The while-loop path has the same omission.
Expected vs actual
Expected: error: if condition must be bool (or equivalent) naming the condition span.
Actual: no diagnostic; codegen emits a branch on an undefined value; runtime trap.
Reproduces on installed with v0.15.1-gd527f2992 and the D7 dirty-tree compiler.
Notes
- This hole is what lets Unannotated fn with early 'return <value>' finalizes as Unit return; caller branches on undef — silent runtime trap #653 (Unit-inferred returns) survive to codegen:
if f(3):against a Unit-returningfshould have been rejected at the call site. - Fix should cover
if,while, and any other bool-position contexts that share the pattern (guards,and/oroperands) — worth an audit of everycheck_expr(cond)-and-discard site in SemaCheck.w.
Activity
D7 checkpoint: eliminate-self receiver migration + integrated analysi…
ehartford commentedon Jul 14, 2026
Fixed in 759667c. if/while/do-while conditions had their type computed and discarded; added check_bool_condition (requires bool; accepts diverging Never / already-errored) and routed the three sites through it. The self-host flip sweep found 6 compiler-internal sites branching on i32 predicates and made them explicit with != 0. Verified: 'if g():' (g returns Unit) now errors 'if condition must be bool'; normal bool conditions unaffected. Fixtures: err_if_condition_not_bool, err_while_condition_not_bool. Full gate green incl. fixpoint.
Sema/MIR: require bool conditions; accept the unit pattern (#654, #631)