Skip to content

Sema: a closure does not capture a local used inside a .Variant(args) shorthand — () => .Some(n) reads 0/null (§12.4) #1570

Closed

Description

@ehartford

Found by the closure cells added to tools/drop_audit.w for #1486 (run_unit(() => a = .Some(mk(2, slot))) passed mk a null slot and crashed in R.drop).

check_closure collects captures with expr_uses_symbol(body, sym), and that walk does not descend into the arguments of an NK_VARIANT_SHORTHAND (.Some(n)). The symbol is not captured, the closure environment has no slot for it, and MirLower lowers the use to a zero constant. The explicit spelling Some(n) is captured correctly.

Reproduces on the installed compiler (v0.15.2.1) and a stage1 of origin/main f1e3bd2:

fn take(o: Option[i32]) -> i32: o.unwrap_or(0)
fn run(f: fn() -> Option[i32]) -> Option[i32]: f()
fn main:
    let n = 5
    print(take(run(() => .Some(n))))      // 0   expected 5
    print(take(run(() => Some(n))))       // 5
    let g: fn() -> Option[i32] = () => .Some(n)
    print(take(g()))                      // 0   expected 5

No diagnostic. §12.4: a closure captures the variables its body uses; a .Variant(args) argument is an ordinary expression (#933 made Sema check those arguments against the payload types, but the capture walk was not updated). A raw-pointer capture (slot: *mut i32) becomes null the same way, which is how it surfaced as a segfault.

Workaround in the audit cell: spell Some(...); the fix can restore .Some(...).

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