Skip to content

Miscompile: let Some(x) = e else: break inside a loop — the function returns garbage and leaks #1733

Closed

Description

@ehartford

Spec: §9.7 (let-else; D58), §13.5d. Found while writing the #1725 tests (branch gen-push-pull); reproduces on the gen-push-impl base and on the installed compiler (main).

Repro:

fn count_to(lim: i32) -> i32:
    var n = 0
    var out: Vec[i32] = Vec.new()
    while true:
        let Some(x) = (if n < lim: Some(n) else: None) else: break
        out.push(x)
        n += 1
    out.len() as i32

fn main:
    print(count_to(3))

Expected: 3. Actual: a garbage value (78200848, 5455888 — varies per run); with a Vec returned instead of its length, the returned Vec has len 0 and --debug-alloc reports its buffer leaked. The same loop written with match e: Some(x) => ... None => break is correct.

Not yet root-caused (no MIR/lldb work done); the break out of the let-else's else branch appears to skip or misdirect the loop exit — the value after the loop is not the one the body built.

Workaround used: match with a None => break arm (test/behavior/behav_gen_pull_zip.w).

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