Repository navigation
Description
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
src/Parser.wparse_if_let: a missingelseis desugared toNK_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_assignreturnstarget_type) and fails against a Unit body such asassert. lldb atSema.merge_contextual_owned_join_types:w1 = 14(Unit),w2 = 3(i32), called fromresolve_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 emptyNK_BLOCK.src/SemaCheck.wcheck_fn_body_with_sig_atcallscheck_expr(body)directly. A single-statement body is the bareif/matchnode, not a block, so the block-tail rule incheck_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.- That block-tail rule is itself wrong for an unannotated return: it checks
if x: 1 else: 2in statement context, so theifis typed Unit while the function's inferred return is i32. That is the garbage value above. check_match_expr, statement position (match_is_value == false):result_typetakes 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.
Activity
D43: infer an unannotated return from the tail under one rule; fix #1179