You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Non-move closure capturing a local by reference, stored in a returned struct field: garbage / segfault, no diagnostic #1638
Found while verifying #1635 (the alias-call fix) under --debug-alloc. Independent of it: the DIRECT call c.f(1) is wrong too, on the pinned seed as well as a stage1 built from main + the #1635 fix.
Repro
type Cnt { name: str, f: fn(i32) -> i32 }
fn mk(k: i32) -> Cnt:
let extra = k
Cnt { name: "n", f: x => x + extra }
fn direct(c: &Cnt) -> i32: c.f(1) + c.f(2)
fn main:
let c = mk(100)
print(direct(&c))
Expected: either 203, or a Sema diagnostic that the closure captures extra by reference and escapes its frame (§12.4 / closure capture rules).
Actual: prints garbage (1033 on a main stage1, -189500189 on the pinned seed); with more calls (let r = &c.f; r(1)) rc=139. --debug-alloc is silent (the dangling capture is a stack slot, not a heap block).
Spelling the closure move x => x + k gives 203 through every call shape (direct, let r = &c.f, let r = c.f, owned), leak count=0.
Why it matters
A non-move closure that captures a local by reference and is stored into a returned aggregate is a use-after-return that neither Sema nor the allocator catches. Either the escape must be diagnosed (a view into the frame cannot leave it — the same rule §21.1 applies to returned &T), or the capture must be by copy for a Copy local. Which one is Eric's ruling; the compiler must not stay silent.
Fixed on main by #1701 (3d5dc47): capture-set completeness, stored-view refusal, calls through &fn; battery green and pinned by the tests named in the PR.
Activity
MirLower: a call through a let-alias of a non-Copy fn field calls the…
MirLower: a call through a let-alias of a non-Copy fn field calls the…
Closures: every capture is found, a stored view is refused, a call th…
ehartford commentedon Sep 26, 2026
Fixed on main by #1701 (3d5dc47): capture-set completeness, stored-view refusal, calls through
&fn; battery green and pinned by the tests named in the PR.