Skip to content

if/while condition type is never checked — 'if unit_call():' passes check and traps at runtime #654

Closed

Description

@ehartford

Spec reference

  • §9 Functions and Expressions — if/while conditions 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

Activity

  1. ehartford commented on Jul 14, 2026

    @ehartford
    ContributorAuthor

    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.

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