Skip to content

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

Closed

Description

@ehartford

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]())

prints 451559720 | 451561040 | 451562360 (expected 0 10 20), debug-alloc: leak count=0.

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.

Activity

  1. 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
  2. ehartford commented on Sep 23, 2026

    @ehartford
    ContributorAuthor

    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.

  3. ehartford commented on Sep 23, 2026

    @ehartford
    ContributorAuthor

    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.

  4. ehartford commented on Sep 24, 2026

    @ehartford
    ContributorAuthor

    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.

  5. ehartford commented on Sep 26, 2026

    @ehartford
    ContributorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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