Skip to content

Parser/Sema: var (a, b) = ... binds immutable names — the var is dropped for destructuring patterns (§9.7) #1354

Closed

Description

@ehartford

Spec §9.7 ("let destructuring"): "All pattern forms are available in let/var bindings." A var destructuring binds immutable names.

fn lc(b: i32) -> (i32, i32): (b + 1, b + 2)
fn main:
    var (x, y) = lc(3)
    x = x + 10
    print(f"{x} {y}")

Actual (seed pinned in seed.lock, and a stage built from fix-stage-debug-info + #1349's fix): error: cannot assign to immutable variable at x = x + 10.
Expected: prints 14 5.

Exact line: Parser.parse_let_binding (src/Parser.w ~7091) computes is_mut and then, for a (/{/[ pattern, calls parse_let_pattern_rest(start, pat), which builds NK_LET_ELSE(pat, value, else_body) with no mutability anywhere; Sema's check_pattern then scope_put(sym, ty, 0) for every NK_PAT_IDENT. The var is dropped at parse time. var Name { .. } = ... (#1299 path) goes through the same parse_let_pattern_rest.

Workaround: let (a, b) = ... then var x = a.

Found while building the #1349 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