Skip to content

Unannotated block tail: value dropped while the caller formats it (garbage), invalid MIR on mixed match arms, validator silent #1180

Closed

Description

@ehartford

Part of #1178 / D43. Unconditional under every ruling.

1. Silent garbage

var seen: i32
fn pick(x: bool):
    seen = 0
    if x: 1 else: 2
fn main: print(f"{pick(true)}")

Prints 67682320. Without the seen = 0 line it prints 1.

--dump-mir on the installed compiler:

fn sym745(pick) {
    _0: ty14  // return            <- Unit
  bb0: _2 = const 0ty3; switchInt(copy _1) -> [1: bb1, otherwise: bb2]
  bb1: goto -> bb3                 <- the `1` is gone
  bb2: goto -> bb3                 <- the `2` is gone
  bb3: return
}
fn main:
    _2: ty14
  bb1: call pick(const true) -> [return: _2]
  bb2: call sym999(copy _2) -> [return: _3: str]    <- a formatter is handed the Unit

pick is lowered as a Unit function with both values dropped (check_block's tail path treats the tail of an unannotated function as a statement, src/SemaCheck.w ~9103-9111), and its return slot and the caller's local are both Unit. The garbage comes from the f-string: Sema typed {pick(true)} as printable and selected a formatter, and MIR passes it a ty14 operand. Not yet proven: which function sym999 is and the exact Sema line that types the call; that is owed before the fix commit.

2. Invalid MIR

var seen: i32
fn f(p: bool):
    seen = 0
    match p:
        true => seen = 1
        false => assert(p)
fn main: f(true)

error: invalid MIR before codegen: use rvalue type is incompatible with assign destination (dest ty=3 kind=1, src ty=14 kind=4). Statement-position check_match_expr (~12564-12569) takes the first arm's type and keeps it when a later arm is incompatible; MirLower then stores a Unit arm into an i32 result place.

3. The validator stayed silent

--validate-all passes program 1 although a call argument of type Unit is passed to a non-Unit parameter. Rule to add: each call argument's MIR type must be compatible with the callee parameter's type. (A "return slot definitely assigned" rule would not catch this: the slot is Unit.) The test needs a way to hand the validator bad MIR; if none exists, that tool is part of this ticket.

Fix

One tail-typing rule used by both body spellings (D43): a block tail is a value when its arms unify, so pick infers i32 in both spellings; a statement-position match whose arms do not join is Unit, never "the first arm's type". The mixed-arm behaviour itself changes with D43, in one place.

Tests

  • pick in both spellings, asserting the returned value.
  • The invalid-MIR program: asserts D43's diagnostic, and that with check does not reach MIR validation.
  • The validator rule, against injected bad MIR.
  • test/coverage_manifest.txt: a tail-typing section with every row of the D43 matrix in both spellings.

Activity

  1. ehartford commented on Sep 18, 2026

    @ehartford
    ContributorAuthor

    Correction to item 1, from running it: the garbage is an f-string defect, not a return-slot defect. fn u(): print("x") then print(f"{u()}") compiles and prints junk (x then 1), while u().to_string() is rejected ("unknown method to_string for type Unit"). pick is consistently Unit in Sema and MIR under the current block-tail rule; the f-string interpolation path is what accepts a Unit operand and hands it to a formatter. Two fixes follow: (a) f-string interpolation of a Unit expression is a compile error; (b) D43 makes pick infer i32 in both body spellings. The validator rule in item 3 (call argument type vs parameter type) still stands: MIR passed ty14 to the formatter and --validate-all was silent.

    Also corrected from #1178: a function whose tail is an assignment is already Unit (infer_unannotated_function_return_type -> tail_expr_is_assignment); let q: i32 = set() is a type mismatch. The earlier "returns 9" observation was this same f-string garbage.

  2. ehartford commented on Sep 18, 2026

    @ehartford
    ContributorAuthor

    The validator rule (a Unit call argument is valid only when the callee parameter is Unit) found a second real defect on its first battery: fallback_expr_type in src/MirLower.w typed a subscript on a user IndexPlace as Unit (its NK_INDEX branch only knew built-in containers). let r = g[0] had a Unit-typed local and g[i] += 100 built its get result and sum in Unit-typed temporaries, then passed them to set; it ran only because codegen used the callee return type. Fixed by typing the subscript as what get returns; test/behavior/behav_index_place_user.w is the regression test (red under the rule without the fix).

  3. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): does not reproduce.

    $ cat main.w
    var seen: i32
    fn pick(x: bool):
        seen = 0
        if x: 1 else: 2
    fn main: print(f"{pick(true)}")
    $ with run main.w
    1
    
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