Skip to content

Sema: a closure does not capture a local used only inside an unsafe block — () => unsafe { *p = 42 } stores through the return slot and segfaults (§12.4) #1598

Closed

Description

@ehartford

Found while implementing the §12.4 by-place ruling in stack #1585; same class as #1570 (.Some(n)).

check_closure collects captures with expr_uses_symbol(body, sym), and that walk does not descend into an NK_UNSAFE_BLOCK (either unsafe { ... } or unsafe: block form). A local used only inside it is not captured, the closure has no local for it, and MirLower lowers the use to _0 — the closure's return local:

fn sym($anonymous$…) {
  locals:
    _0: ty3  // return [mut]
  bb0: {
    _0.* = const 42ty3;      // `*p = 42` with p uncaptured
    _0 = const 0ty3;
    return;
  }
}

LLVM: %2 = load ptr, ptr %1 (the i32 return slot read as a pointer), store i32 42, ptr %2 → SIGSEGV. Reproduces on the pinned seed (nightly-20260922), the installed v0.15.2.1 and a stage1 of origin/main 02301f8:

fn run(f: fn() -> i32) -> i32: f()
fn main:
    var slot: i32 = 0
    let p = &raw mut slot
    run(() =>
        unsafe { *p = 42 }
        0
    )
    print(slot)            // expected 42; exit 139

No diagnostic. The same use outside the unsafe block is captured correctly. Not fixed in #1585.

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