Repository navigation
Sema: returning a closure whose environment lives in this frame is a compile error (#1567; D63 implementation follows) - #1592
Merged
ehartford merged 1 commit intoSep 24, 2026
Conversation
ehartford
force-pushed
the
fix-1567-returned-closure-env
branch
from
September 24, 2026 03:24
bbabe37 to
17032c3
Compare
Contributor
Author
|
Reworked: the by-place message's fix-it now points at #1567 ("a returned closure must own its environment ( |
ehartford
force-pushed
the
fix-1567-returned-closure-env
branch
from
September 24, 2026 13:50
17032c3 to
77a5d4d
Compare
ehartford
marked this pull request as ready for review
September 24, 2026 14:48
…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
force-pushed
the
fix-1567-returned-closure-env
branch
from
September 24, 2026 14:59
77a5d4d to
73f2c89
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_closurebuilds 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 * 10gave451559720 …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_envat the three return sites (expression body, body block tail,return):returned closure capturesxsby place and may outlive it (§12.2, §12.4); a closure that leaves its frame takes its captures by value:move () => ...``;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. Stage1check src/main.w/build.wok (nothing in the tree returns a capturing closure); audits unchanged from the layer below. Iterate tier only — no battery.