Skip to content

Non-exhaustive expression-position match on integers silently yields zero value #994

Closed

Description

@eas4ai

Classification

Spec violation with silent wrong-value runtime behavior (audit finding MATCH-001). Spec §9.x: 'Expression-position match (value is used/returned): must be exhaustive.'

Source chain (verified by execution at 450733e)

Enum and bool subjects ARE checked (non-exhaustive match: missing variant 'Blue', non-exhaustive match on bool — both loud, both correct). But an integer-subject match in return position with no _ arm passes with check silently, and at runtime the fall-through produces the type's zero value (classify(2) returned '', printed empty, rc=0). Probes .audit/probes/readonly_pass_20260903/matchx{,2,3}_probe.w.

Two defects, one root

  1. Check-time: exhaustiveness is implemented per finite domain (enum variants, bool) but infinite domains never demand _.
  2. Runtime: the fall-through fabricates a zero value instead of failing (unreachable/panic). Even where checking lags, the codegen default must not invent a value.

Repair boundary

Demand _ (or compiler-proven full coverage) for expression-position matches over non-finite subjects — same diagnostic shape as the bool case; AND make unmatched fall-through a runtime panic rather than a zero value, so any future checking gap fails loud instead of fabricating data.

Activity

  1. ehartford commented on Sep 16, 2026

    @ehartford
    Contributor

    Review: reproduced, with one correction: the fabricated value is uninitialised memory, not a guaranteed zero

    Verdict. Established on current main. An expression-position match over an integer with no _ arm passes with check, while the same shape over an enum or bool is rejected with the existing non-exhaustive match diagnostics. At runtime the unmatched case returns whatever the result temporary happens to hold: the report saw the type's zero value; on this host classify(2) returned "zero" (the first arm's value) and score(7) returned 20 (the second arm's). The MIR shows why: the fall-through edge jumps to the join block and moves out of a result local that was never assigned on that path. The ownership trace records the place as Maybe at that move, yet --validate-all, --validate-ownership, and with analyze audit:all all report ok, so the validators accept a move from a possibly-uninitialised place.

    Tested revision and compiler

    item value
    source main @ 5809ac1 (2026-09-14)
    compiler with build of 5809ac1 in a detached worktree, self-reports with v0.15.2.1
    platform macOS Darwin 25.6.0, arm64

    Probe matrix

    program with check with run
    fn classify(n: i32) -> str: match n: 0 => "zero" | 1 => "one"; print classify(0/1/2) ok [zero] [one] [zero] (third should not compile)
    fn score(n: i32) -> i32: match n: 0 => 10 | 1 => 20; score(0/1/7) ok 10 20 20
    control: enum Color match missing Blue, return position non-exhaustive match: missing variant 'Blue' n/a
    control: bool match with only true, return position non-exhaustive match on bool n/a
    control: statement-position integer match without _ ok, prints after correct per §9 (partial allowed)

    Root cause (observed in MIR)

    --dump-mir for classify:

    bb0: _4 = binop(eq, copy _2, const 0); switchInt(copy _4) -> [1: bb2, otherwise: bb3];
    bb1: _0 = move _3; _3 = const zst(ty16); return;        // join
    bb2: _3 = const "zero"; goto -> bb1;
    bb3: _5 = binop(eq, copy _2, const 1); switchInt(copy _5) -> [1: bb4, otherwise: bb1];   // no arm: straight to join
    bb4: _3 = const "one"; goto -> bb1;
    

    --trace-ownership classify:_3: bb1.stmt6 event=move before=_3=Maybe after=_3=Moved text="_0 = move _3;". The lowering has no unreachable/panic block for the exhausted chain; the last otherwise targets the join and the return reads _3 uninitialised. What the caller sees is therefore stack residue (here the previous call's arm value, in the reporter's run a zero), which is why the title's "zero value" is one observed outcome rather than the rule.

    Sema: the exhaustiveness checks at src/SemaCheck.w:12692 (bool), :12721 (sealed trait), and :12755 (enum variants) are the only ones; there is no branch for integer, str, or other infinite-domain subjects, so no _ is demanded.

    Spec

    §9 "Pattern Matching" (spec lines 4310-4316): "Expression-position match (value is used/returned): must be exhaustive." The integer case violates this at check time, and the runtime behaviour violates the mission's safety bar (reading uninitialised memory from safe code). Statement position is correctly partial.

    Tools that stayed silent

    • validate-ownership: ok on a body whose own trace marks the moved place Maybe. Per AGENTS.md this validator gap is part of the bug: a move from a Maybe-state place in a return should be rejected.
    • with analyze audit:all: violations=0 ok.

    Related PRs

    None merged or open touch integer-subject exhaustiveness or the match fall-through lowering.

    Would I champion a fix? Yes, both halves.

    Acceptance criteria:

    1. Check time: an expression-position match whose subject type is not a finite domain (integers, str, floats, ...) and has no _/binding catch-all arm is rejected with a diagnostic of the same shape as the bool case (non-exhaustive match on i32: add a _ arm). Both probe programs above fail with check.
    2. Lowering: the exhausted comparison chain's final otherwise targets an unreachable terminator (or a panic with the subject value), never the join block, so any future checking gap fails loudly instead of reading an uninitialised local.
    3. Validator: validate_ownership_body rejects a move/copy from a place whose state is Maybe on a return path, and src/MirValidationTests.w gains a hostile body for it (ties into MIR validator negative coverage: validate_mir_body and use-after-kill have no malformed-MIR cases #989).
    4. Behaviour tests: the statement-position partial match stays accepted; a test/compile_errors fixture for the integer expression-position case.

    Untested

    Floats and str subjects were not probed; they share the code path (no domain branch in Sema). Linux and Windows not executed.

  2. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): does not reproduce. An integer match without _ is now refused; #1388 made exhaustiveness read whole patterns (05b674e).

    $ cat main.w
    fn classify(n: i32) -> str:
        match n:
            0 => "zero"
            1 => "one"
    fn main:
        print(f"[{classify(2)}]")
    $ with check main.w
    error: non-exhaustive match on 'i32': `-1` is not covered
     --> main.w:2:5
      = help: add an arm that matches it, or a `_` arm
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions