Skip to content

Unannotated-return tail if/if-let/match: spelling-dependent typing, rejected Unit bodies, and a silent garbage return #1178

Closed

Description

@ehartford

A function with no return annotation whose body ends in an if/if let/match is typed three different ways depending on spelling. One spelling silently returns garbage.

Found while adding the #1172 return-cleanup regression fixture (if let Some(v) = f(): assert(...) as a whole function body is rejected). Verified on the installed compiler (~/.local/bin/with), out/release/bin/with, and a fresh stage2; none of it comes from the #1172 change.

Silent wrong value

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

Prints 67682320. Expected 1. Without the seen = 0 line it prints 1. The match spelling of the same body prints the right value.

Matrix (all in a function with no return annotation)

body single-statement body same, with one statement in front
if let Some(v) = f(): assert(v == 7) error: match expressions do not establish one compatible owned result type ok
if let Some(v) = f(): seen = v ok ok
if let Some(v) = f(), v == 7: assert(p) error: if expressions do not establish ... ok
if p: seen = 1 / else: assert(p) error: if expressions do not establish ... ok
match p: true => seen = 1, false => assert(p) error: match expressions do not establish ... error: invalid MIR before codegen: use rvalue type is incompatible with assign destination (dest ty=3 kind=1, src ty=14 kind=4)
if x: 1 else: 2 returns 1 returns garbage
match x: true => 1, false => 2 returns the value returns the value

Exact lines

  1. src/Parser.w parse_if_let: a missing else is desugared to NK_INT_LIT 0 (else_body = self.pool.add_node(NodeKind.NK_INT_LIT, ...)). The wildcard arm is therefore i32. It joins with an assignment body (an assignment is typed as its target, check_assign returns target_type) and fails against a Unit body such as assert. lldb at Sema.merge_contextual_owned_join_types: w1 = 14 (Unit), w2 = 3 (i32), called from resolve_contextual_join <- check_match_expr <- check_fn_body_with_sig_at. Log: return-temps-iflet-unit-join-lldb.log. The guard-let desugar in the same file already uses an empty NK_BLOCK.
  2. src/SemaCheck.w check_fn_body_with_sig_at calls check_expr(body) directly. A single-statement body is the bare if/match node, not a block, so the block-tail rule in check_block (ret_is_void = current_return_type == ty_void or current_return_type == 0 -> statement context) never runs for it. The same source is a value in one spelling and a statement in the other.
  3. That block-tail rule is itself wrong for an unannotated return: it checks if x: 1 else: 2 in statement context, so the if is typed Unit while the function's inferred return is i32. That is the garbage value above.
  4. check_match_expr, statement position (match_is_value == false): result_type takes the first arm's type and keeps it when a later arm is incompatible. MirLower then assigns a Unit arm into an i32 result place (the invalid-MIR row).

Assignment-as-value is working surface (a = b = 3 runs, let z = (a = 5) yields 5), so the fix is not to retype assignment.

Needs a ruling

What an unannotated function means when its tail arms disagree (seen = 1 is i32, assert(p) is Unit). Candidates: the tail is a value when the arms join to one type and Unit otherwise; or disagreeing arms are an error in every spelling. Either way both spellings must agree and rows 1-5 of the matrix follow from the choice.

Workaround in use

None committed. The #1172 fixture needs defect 1 fixed to compile.

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