Repository navigation
Description
Summary
The capture-based scope bracket works on the current compiler, and move
closures exist with arrow syntax. The remaining issue is a specification
decision: §3.2's mut-receiver exclusivity appears to forbid the same-call
non-escaping closure capture that the compiler currently accepts.
Current facts (revalidated 2026-07-15)
This shape compiles and preserves setup/body/teardown mutations:
mut fn scope(body: fn() -> i32):
// setup
let result = body()
// teardown
result
called with a non-escaping closure that captures and mutates the same receiver.
A concrete probe produced the expected ordered state (r=11 after=111).
Move closures are also implemented as move () => expr. The former
move || spelling is rejected; §12.4 still contains pipe-form examples and
needs a syntax-hygiene update.
The unresolved contradiction is normative: §3.2 says an argument to a mut
receiver call may not retain access to the receiver's place, while the working
closure argument captures exactly that place. Working code settles the
implementation fact, not which behavior is intended.
Decision required
Choose and record one coherent model:
- Bless a narrowly defined non-escaping by-place closure capture for the
duration of a mut-receiver call, and specify the escape/effect checks that
make it sound. - Enforce §3.2 literally, reject the current capture form, and add an
ergonomic dedicated scope/cleanup construct or another receiver-passing
form. - Keep a dedicated scope construct as syntax even if the capture exception is
blessed, when it materially improves cleanup guarantees.
The decision must cover return, panic/error cleanup, non-local control flow,
and whether the closure may escape or suspend.
Concrete compiler driver
MirLower still open-codes the bracket:
let start = self.pending_reset_locals.len()
// lower body
self.flush_pending_resets_since(start)
across conditional, match, and loop lowering. Consolidating those sites is
blocked on the language ruling, not on closure implementation.
Acceptance criteria
- The chosen rule is recorded in the specification/decisions log.
- Compiler behavior matches §3.2 for escaping and non-escaping captures.
- §12.4 examples use current arrow-closure syntax.
- Setup/teardown semantics are tested for normal return and failure/control
edges. - The MirLower duplication is consolidated only after the semantic contract is
stable.
Traceability
- Specification §3.2
- Specification §12.4
- D7 receiver syntax
Activity
ehartford commentedon Jul 2, 2026
Queued (WWED): the mission can't rule on an undesigned surface — a decision brief (problem, candidate designs, compass take per design) will be delivered at the end of the current close-out campaign so the ruling can be made from concrete options.
[-]Language: no ergonomic scope-bracket (higher-order closure over a mutable receiver)[/-][+]Design and specify scope brackets over a mutable receiver[/+]ehartford commentedon Oct 25, 2026
Triage 2026-10-25,
with v0.15.3.0(main a261b65): needs a ruling, not code. §3.2 makes amut selfreceiver exclusive for the duration of the call, yet the compiler accepts a same-call non-escaping closure that captures and mutates that receiver (the scope-bracket shape in the issue). The options the issue lays out stand: (1) §3.2 admits non-escaping closures that capture the receiver (the shape that works today becomes specified); (2) the compiler refuses it and the bracket takes the receiver as an explicit argument; (3) a dedicated bracket form. No D-record decides it; the decision brief queued in the last comment is the next step.ehartford commentedon Nov 19, 2026
Ruled, 2026-11-18 (D218): option 2, refused immediately. Build the failing program (callee holds a view into
self.items, closure pushes tox.items) as the regression test. The fix-it formx.scope((s) => …)needs a closure type whose parameter ismut; iffn(mut T) -> Rcannot be written yet it lands in the same change.D218, D219 and D220 recorded: Eric's rulings of 2026-11-18 on #615, #919
ehartford commentedon Nov 19, 2026
Probe results (2026-11-18): this is a use-after-free in safe code, not only an unspecified convenience. A callee that holds
&self.items[0]acrossbody()while the closure pushes tox.itemsprints 0 where the element is 7; the same holds for a read-only receiver and for a&Tparameter of a free function. An existing same-call check refuseseach(xs, (v) => xs.push(v))but misses a mutating method call through a field (y.items.push(1)), never looks at the receiver of a method call, and a closure literal passed as an argument skips the borrow check alet f = () => …gets.fn(mut T) -> Rdoes not parse today.A closure passed to a call is judged by its capture facts, against th…
ehartford commentedon Nov 19, 2026
Layer 1 merged (#3451): the use-after-free is closed for a writing closure against the receiver (read-only or
mut), against a borrowed sibling argument through a field, and against a view the caller holds across the call. Remaining for D218: refuse a read-only capture of amutreceiver's place, with the fix-it form, which needs a closure type with amutparameter (fn(mut T) -> Rdoes not parse).ehartford commentedon Nov 19, 2026
Ruled, 2026-11-19 (D218's remaining half): the mode is part of a function type (
fn(mut T) -> R), with: the closure's own captures checked at an indirect call (let f = (mut b: Bag) => { b.n += 1; bag.n }; f(bag)is an error); nomutinextern "C"function types; the unbound form of amut fnmethod has this type and the bound form is refused for now; and the §3.2 sentence speaks of a view of any place that overlaps themutreceiver or argument, sox.items.each_mut(() => x.count)stays legal.ehartford commentedon Nov 28, 2026
Already ruled (D218, 2026-11-18 & 2026-11-19; D223) and fully implemented on main (spec v7.69–v7.71, §3.2, §3.8a, §9.3): fn(mut T) -> R function types, closure mut parameter typing and indirect-call capture checking, refusal of mut in extern "C" function types, unbound mut fn method values (bound form refused), and refusal of read/write captures overlapping a mut receiver or mut argument (while keeping disjoint fields legal). Verified on main (8d53518): all 29 tests in test/behavior/{615,d218,d223}.with and test/compile_errors/{615,d218,d223}.with pass.