Skip to content

Sema: returning a closure whose environment lives in this frame is a compile error (#1567; D63 implementation follows) - #1592

Merged
ehartford merged 1 commit into
fix-1481-let-bound-by-placefrom
fix-1567-returned-closure-env
Sep 24, 2026
Merged

ehartford merged 1 commit into
fix-1481-let-bound-by-placefrom
fix-1567-returned-closure-env

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

For #1567 (stays open for the environment-ownership design). Stacked on the #1481 PR (base fix-1481-let-bound-by-place).

Spec §12.2: "A closure is escaping if stored, returned, or sent to another thread. Escaping closures may NOT capture ephemeral values." §12.4: a non-move closure holds a non-Copy capture by place — a view of that local; returning it is a view escaping its frame.

Root cause gen_closure builds the environment as a stack slot of the creating function and returns {fn, env} pointing at it; a returned closure reads the dead frame (fn mk(x: i32) -> fn() -> i32: () => x * 10 gave 451559720 … in a loop once not inlined; no diagnostic). Closure values are Copy (is_copy: TY_FN), so a heap environment has no owner; until closure values own their environment the return is refused loudly.

Fix SemaCheck.check_returned_closure_env at the three return sites (expression body, body block tail, return):

  • by-place capture: returned closure captures xsby place and may outlive it (§12.2, §12.4); a closure that leaves its frame takes its captures by value:move () => ...``;
  • any other capturing closure (Copy captures, move): returned closure keeps its captures in this call's frame, which dies at return (#1567); a capturing closure cannot be returned until closure values own their environment.

Note on the fix-it: move () => ... moves the by-place capture into the environment, but that environment is still a frame slot, so the moved form hits the second error — no returned capturing closure can work until the ownership design lands. A let-bound closure is recognized through its binding only while the binding carries by-place origins, so a returned binding holding a Copy-only closure is not caught (noted on #1567).

Fixtures err_closure_returned_by_place_capture.w, err_closure_returned_by_place_capture_binding.w, err_closure_returned_copy_capture.w. Stage1 check src/main.w / build.w ok (nothing in the tree returns a capturing closure); audits unchanged from the layer below. Iterate tier only — no battery.

@ehartford
ehartford added this pull request to stack #1585 September 23, 2026 23:35
@ehartford
ehartford force-pushed the fix-1567-returned-closure-env branch from bbabe37 to 17032c3 Compare September 24, 2026 03:24
@ehartford ehartford changed the title Sema: returning a closure whose environment lives in this frame is a compile error (#1567) Sema: returning a closure whose environment lives in this frame is a compile error (#1567; D63 implementation follows) Sep 24, 2026
@ehartford

Copy link
Copy Markdown
Contributor Author

Reworked: the by-place message's fix-it now points at #1567 ("a returned closure must own its environment (move ||, #1567, not yet implemented)"); a returned move closure gets its own message (err_closure_returned_move_env.w); under D62 the #1567 repro's Copy parameter is a by-place capture (err_closure_returned_copy_capture.w updated). D63 (owning move || environments, the callable type) lands as the next layer on this stack.

…compile error — a by-place capture would outlive its local, a `move` closure its frame slot (#1567)

Spec §12.2: "A closure is escaping if stored, returned, or sent to
another thread. Escaping closures may NOT capture ephemeral values."
§12.4 (D62): captures are by place regardless of whether the type is
Copy — a non-move closure's capture is a view of that local; returning
the closure is a view escaping its frame, rejected like any returned
view of a local. D63 (§12.4 "The callable type", Eric 2026-09-23) rules
that a `move ||` closure OWNS its environment (inline when every
capture is Copy, otherwise a heap cell owned by the closure value);
that is not implemented in this layer, so a returned `move` closure is
refused loudly too instead of leaving a dangling environment.

Root cause: gen_closure builds the environment as a stack slot of the
creating function and returns a fat pointer {fn, env} to it; a returned
closure points at the dead frame. `fn mk(x: i32) -> fn() -> i32: () =>
x * 10` printed `10 20` only because -O1 inlined mk; in a loop with
inlining off it printed stack garbage (451559720 …), leak count=0, no
diagnostic.

Fix: SemaCheck.check_returned_closure_env at the three return sites (a
function's expression body, the body block's tail, `return`):
- a non-move closure with a by-place capture: "returned closure captures
  `x` by place and may outlive it (§12.2, §12.4); a returned closure
  must own its environment (`move ||`, #1567, not yet implemented)";
- a `move` closure with captures: "returned `move` closure keeps its
  environment in this call's frame, which dies at return; a returned
  closure must own its environment (#1567, not yet implemented)".
A let-bound closure is recognized through its binding only while the
binding carries the closure's by-place origins (binding_closure_nodes is
keyed by name and outlives its scope), so a returned binding holding a
capturing `move` closure is not caught here — noted on #1567, which
the D63 layer implements next.

Fixtures: err_closure_returned_by_place_capture.w (expression-body
return of a Vec capture), err_closure_returned_by_place_capture_binding.w
(`return f`), err_closure_returned_copy_capture.w (the #1567 repro: a
Copy parameter is by place too), err_closure_returned_move_env.w (the
`move` form). Stage1 `check src/main.w` and build.w: ok — nothing in the
tree returns a capturing closure. drop_audit/move_audit unchanged from
the layer below (188 / 73 cells, 0 regressions).
@ehartford
ehartford force-pushed the fix-1567-returned-closure-env branch from 77a5d4d to 73f2c89 Compare September 24, 2026 14:59
@ehartford
ehartford merged commit 63ce6d0 into main Sep 24, 2026
@ehartford
ehartford deleted the fix-1567-returned-closure-env branch September 24, 2026 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant