Repository navigation
Description
Summary
Fix closure capture semantics where implementation and §12.4 disagree, but split the decision from the mechanical bug. Copy captures should be copied at closure creation; non-Copy bound closures are currently treated as escaping/move captures, which matches the implemented safety model but conflicts with the spec example and needs an explicit BDFL/spec decision.
Spec
§12.4 says default closure captures are by place, with these refinements: Copy values are captured by copy, non-Copy values are captured by place, and move || transfers ownership. Closure bodies infer effect summaries over captures; calling a closure applies consume/mutate/escape effects to the originating captured place.
The current spec example says:
let xs = Vec.new()
let f = || xs.push(1)
f()
If bound closures are treated as escaping values in v1, that example should be rewritten or constrained to direct non-escaping call contexts.
Current state
Capture collection is lexical: Sema.check_closure gathers outer bindings used by the body and records them as capture symbols at src/SemaCheck.w:6920-6927. Effects are saved with set_closure_capture_summary at src/SemaCheck.w:7036-7039.
Classification is at src/SemaCheck.w:7059-7102: is_non_escaping is true only when closure_direct_arg_depth > 0, direct_arg_escapes == 0, and the closure is not move || (src/SemaCheck.w:7065). Non-escaping captures are registered as borrows, including Copy values; whole-variable capture picks only BorrowKind.EXCLUSIVE or BorrowKind.SHARED at src/SemaCheck.w:7094, never “copy.” That is the Copy write-through bug: a direct non-escaping mutating closure can mutate the original Copy local.
Escaping closures, including bound closures, mark non-Copy captures as moved at src/SemaCheck.w:7129-7130. MirBuilder.lower_closure ignores explicit capture parameters and delegates closure-body compilation to codegen via CK_CLOSURE at src/MirLower.w:7625-7637. Existing behavior coverage only proves escaping Copy read capture (test/behavior/behav_closure_capture_copy.w:5-13), not mutating Copy copy-at-creation semantics.
Implementation plan
- Fix the unambiguous Copy half first: at closure creation, materialize Copy captures into closure environment slots and have the closure body read/write those slots, not the originating local. This should apply even when the closure is direct non-escaping if the captured place type is Copy.
- Preserve effect-summary propagation for non-Copy by-place captures. Direct non-escaping non-Copy closures should keep borrow registration so conflicts are still caught.
- For bound non-Copy closures, keep the current v1 rule unless BDFL rules otherwise: a closure stored in a local is escaping, so moving non-Copy captures at creation is correct. Update §12.4's
let f = || xs.push(1); f()example or file a spec-fix PR/issue before changing sema to a more complex local non-escape analysis. - If BDFL wants bound-but-local non-escaping closures to borrow by place, implement a real lifetime/use analysis: prove the closure value never escapes, never outlives captured places, and is not stored into aggregates, returned, passed to escaping params, or assigned into longer-lived bindings. Do not infer this from “let-bound” alone.
- Update
MirLower.lower_closureand codegen closure environment layout so capture mode is an explicit per-capture fact: copy, shared borrow, exclusive borrow, or move. - Add diagnostics that distinguish “escaping closure cannot borrow captured non-Copy place” from “move closure consumes captured place.”
Test plan
Add behavior tests:
test/behavior/behav_closure_capture_copy_mutation.w: mutating a capturedi32inside a closure leaves the original unchanged.test/behavior/behav_closure_capture_noncopy_direct_arg.w: direct non-escaping closure mutates original Vec by place.test/behavior/behav_closure_capture_move_noncopy.w:move ||consumes the original.
Add compile-error tests:
test/compile_errors/err_bound_closure_noncopy_capture_use_after_move.wtest/compile_errors/err_escaping_closure_mut_borrow_capture.w- If BDFL keeps v1 bound=escaping: a negative test for
let f = || xs.push(1); f()with a diagnostic that explains the escaping capture.
Acceptance criteria
- Copy captures are copied at closure creation and mutations inside the closure do not write through to the original Copy local.
- Direct non-escaping non-Copy closures still borrow the original place and propagate write/read effects.
- Bound non-Copy closure behavior is aligned with an explicit spec ruling, not accidental implementation behavior.
- Closure capture summaries remain accurate for borrow checking and effect enforcement.
with build && with build :fixpoint && with build :testpass.
Dependencies
Potential spec decision issue for §12.4's bound-closure example. #362/#477 overlap for ephemeral capture/escape provenance.
Traceability
From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 12.4.1.1, 12.4.1.2, 12.4.1.3.
Activity
Requirements triage campaign: checkbox, verdict, and issue-link every…
ehartford commentedon Jun 19, 2026
Implemented in 3552326. Closure capture modes now follow §12.4: ordinary Copy captures are value snapshots, non-Copy direct captures remain by-place, and scoped spawn workers are explicitly by-place so ScopedSend local mutation still works. Added positive coverage for mixed Copy/non-Copy captures and updated stale tests that expected Copy-by-place mutation. Verification passed before the docs-only final rebase: build, fixpoint, full test suite, and explicit test-green.