Skip to content

Sema: a move inside a let-else's diverging else body marks the value moved after the let-else #1383

Closed

Description

@ehartford

The else body of a let-else diverges (§9.7), so a value it consumes is still live on the continuing path. Sema's check_let_else (src/SemaCheck.w ~15295) checks the else body in the enclosing move state and never restores it, so a later use is rejected as use-after-move.

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 consume(s: str) -> i32: s.len() as i32
fn h(k: i32) -> i32:
    let keep = "keep".clone()
    let Ok(v) = vec_or_err(k) else: return consume(keep) - 100
    keep.len() as i32 + v.len() as i32
fn main:
    print(f"{h(0)} {h(1)}")

Actual (v0.15.2.1 and origin/main 8c859a7): error: ... a moved value cannot be used again at keep.len().
Expected: -96 5. The equivalent if spelling (if ...: return consume(keep) - 100) is accepted.

MIR lowering is path-correct after #1365 (the else body is lowered under a saved move state that is restored for the continuation), so once Sema accepts this the program must run clean under --debug-alloc; add it to the #1365 fixture test/debug_alloc/da_let_else_fail_owned_payload.w when this lands.

Found while building the #1365 let-else drop 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