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
A closure returned from a function reads its environment from the dead callee frame: fn mk(x: i32) -> fn() -> i32: () => x * 10 gives garbage once mk is not inlined (§12.2 escaping closures) #1567
Found while fixing #1471. gen_closure builds the capture environment as a stack slot of the creating function and returns a fat pointer {fn, env} to it. A closure returned from the function (escaping, §12.2 "stored, returned, or sent") then points at the callee's dead frame. At -O1 the one-liner mk is inlined into main, so the two-call form prints 10 20 by luck; in a loop with inlining off it prints stack garbage (origin/main f1e3bd2, stage1):
@[noinline]
fn mk(x: i32) -> fn() -> i32: () => x * 10
fn main:
var fs: Vec[fn() -> i32] = Vec.new()
for i in 0..3: fs.push(mk(i))
for k in 0..3: print(fs[k]())
No diagnostic is emitted: x is a Copy capture, so §12.2 (no ephemeral captures) is not violated — the environment itself is what dies.
This needs a design ruling before a fix: an escaping closure's environment must be owned by the closure value. fn values are Copy today (Sema.is_copy: TY_FN), so a heap environment has no owner to free it (leaking is a defect, docs/mission.md), and a non-Copy capturing-closure type or a refcounted environment both change what a fn() -> T value is. Related: #1481 (what a let-bound closure does with a non-Copy capture) and the direct-argument escape classification issue filed alongside this one. #1471's fix (an environment per evaluation inside a loop, still on the creating frame) does not cover this case.
changed the title [-]A closure returned from a function reads its environment from the dead callee frame: gives garbage once mk is not inlined (§12.2 escaping closures)[/-][+]A closure returned from a function reads its environment from the dead callee frame: `fn mk(x: i32) -> fn() -> i32: () => x * 10` gives garbage once mk is not inlined (§12.2 escaping closures)[/+]on Sep 23, 2026
Stack #1585 layer PR #1592 refuses the return loudly (by-place capture: escape of a view, fix-it move () => ...; Copy/move captures: the frame-slot environment, which move does not fix). Kept open for the environment-ownership design: a returned binding that holds a Copy-only capturing closure is not caught, and no returned capturing closure can work until closure values own their environment.
Ruled (Eric, 2026-09-23; spec §12.4 / D62, #1593): move || closures own their environment — inline when every capture is Copy, otherwise a heap cell owned by the closure value, freed on drop, captured Drop values destroyed then; never the caller's frame. Returnable, storable, sendable when every capture is Send. Non-move closures stay frame views; returning one is the located error from #1592 with the move || fix-it. Blocked on one brief before code: the type of a returned closure (fn(i32) -> i32 carries no environment) — impl-Fn style, a named Closure[...], or a trait, which also decides trait objects.
Type ruled (Eric, 2026-09-23; spec §12.4 "The callable type" / D63, #1595): one callable type fn(A) -> R, environment ownership compiler-tracked like may_suspend; not Copy (bare functions included); call-once crosses a signature (bundle boundary rejected until a once annotation); views flow like &T; specialization commitment. Implementation is the fifth layer of stack #1585.
Fixed on main: a returned non-move closure is refused — error: returned closure captures xby place and may outlive it (§12.2, §12.4); a returned closure owns its environment:move () => ...`` — pinned by test/compile_errors/err_closure_returned_by_place_capture.w and err_closure_returned_copy_capture.w; the move form of this repro prints `0 10 20` with leak count=0 under --debug-alloc on the pinned seed (D62/D63 owned environment). Tracked under #1698 for the remaining closure holes.
Activity
[-]A closure returned from a function reads its environment from the dead callee frame: gives garbage once mk is not inlined (§12.2 escaping closures)[/-][+]A closure returned from a function reads its environment from the dead callee frame: `fn mk(x: i32) -> fn() -> i32: () => x * 10` gives garbage once mk is not inlined (§12.2 escaping closures)[/+]Codegen: a closure created in a loop gets its own environment per ite…
ehartford commentedon Sep 23, 2026
Stack #1585 layer PR #1592 refuses the return loudly (by-place capture: escape of a view, fix-it
move () => ...; Copy/move captures: the frame-slot environment, whichmovedoes not fix). Kept open for the environment-ownership design: a returned binding that holds a Copy-only capturing closure is not caught, and no returned capturing closure can work until closure values own their environment.spec §12.4 / D62: captures are by place regardless of Copy; three cap…
ehartford commentedon Sep 23, 2026
Ruled (Eric, 2026-09-23; spec §12.4 / D62, #1593):
move ||closures own their environment — inline when every capture is Copy, otherwise a heap cell owned by the closure value, freed on drop, captured Drop values destroyed then; never the caller's frame. Returnable, storable, sendable when every capture is Send. Non-move closures stay frame views; returning one is the located error from #1592 with themove ||fix-it. Blocked on one brief before code: the type of a returned closure (fn(i32) -> i32 carries no environment) — impl-Fn style, a named Closure[...], or a trait, which also decides trait objects.spec §12.4 / D63: one callable type — fn(A) -> R carries compiler-tra…
ehartford commentedon Sep 24, 2026
Type ruled (Eric, 2026-09-23; spec §12.4 "The callable type" / D63, #1595): one callable type
fn(A) -> R, environment ownership compiler-tracked like may_suspend; not Copy (bare functions included); call-once crosses a signature (bundle boundary rejected until aonceannotation); views flow like&T; specialization commitment. Implementation is the fifth layer of stack #1585.Codegen: a closure created in a loop gets its own environment per ite…
Sema/Codegen: every capture of a non-move closure is by place, Copy o…
Sema: returning a closure whose environment lives in this frame is a …
Codegen: a closure created in a loop gets its own environment per ite…
Sema/Codegen: every capture of a non-move closure is by place, Copy o…
Sema: returning a closure whose environment lives in this frame is a …
Codegen: a closure created in a loop gets its own environment per ite…
Sema/Codegen: every capture of a non-move closure is by place, Copy o…
Sema: returning a closure whose environment lives in this frame is a …
Closures: a move closure owns its environment (inline when every capt…
Codegen: a closure created in a loop gets its own environment per ite…
Sema/Codegen: every capture of a non-move closure is by place, Copy o…
Sema: returning a closure whose environment lives in this frame is a …
Closures: a move closure owns its environment; fn(A) -> R is not Copy…
ehartford commentedon Sep 26, 2026
Fixed on main: a returned non-
moveclosure is refused —error: returned closure capturesxby place and may outlive it (§12.2, §12.4); a returned closure owns its environment:move () => ...`` — pinned by test/compile_errors/err_closure_returned_by_place_capture.w and err_closure_returned_copy_capture.w; themoveform of this repro prints `0 10 20` with leak count=0 under --debug-alloc on the pinned seed (D62/D63 owned environment). Tracked under #1698 for the remaining closure holes.