Skip to content

let-else: the pattern's bindings are in scope inside the else branch; check accepts it and MIR lowering then fails #1476

Closed

Description

@ehartford

Spec: §9.7 let ... else — the else branch runs when the pattern does not match, so nothing the pattern binds exists there.

fn vec_or_err(k: i32) -> Result[Vec[str], str]:
    if k == 0: return Err("none".clone())
    var v: Vec[str] = Vec.new()
    v.push("a".clone())
    v
fn h(k: i32) -> i32:
    let Ok(v) = vec_or_err(k) else: return v.len() as i32
    v.len() as i32
fn main:
    print(f"{h(0)}")

Actual (stage1 of origin/main ba2d5d1): with check says ok; with run fails in MIR lowering: error: MIR lowering failed for function 'h' ... AST codegen was removed, so this function cannot be compiled.

Expected: a Sema error at the v in the else branch (the name is not bound on that path), the same diagnostic as any unbound name.

Mechanism (from reading, not yet traced): check_let_else (src/SemaCheck.w) binds the pattern's names with check_pattern into the enclosing scope before it checks the else body, so the else body resolves them.

Found while building the #1383 let-else move matrix.

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