Skip to content

Design and specify scope brackets over a mutable receiver #615

Done

Description

@ehartford

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:

  1. 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.
  2. Enforce §3.2 literally, reject the current capture form, and add an
    ergonomic dedicated scope/cleanup construct or another receiver-passing
    form.
  3. 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

  1. ehartford commented on Jul 2, 2026

    @ehartford
    ContributorAuthor

    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.

  2. changed the title [-]Language: no ergonomic scope-bracket (higher-order closure over a mutable receiver)[/-] [+]Design and specify scope brackets over a mutable receiver[/+] on Jul 15, 2026
  3. ehartford commented on Oct 25, 2026

    @ehartford
    ContributorAuthor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): needs a ruling, not code. §3.2 makes a mut self receiver 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.

  4. added this to the post-v0.16.0 milestone on Oct 28, 2026
  5. ehartford commented on Nov 19, 2026

    @ehartford
    ContributorAuthor

    Ruled, 2026-11-18 (D218): option 2, refused immediately. Build the failing program (callee holds a view into self.items, closure pushes to x.items) as the regression test. The fix-it form x.scope((s) => …) needs a closure type whose parameter is mut; if fn(mut T) -> R cannot be written yet it lands in the same change.

  6. ehartford commented on Nov 19, 2026

    @ehartford
    ContributorAuthor

    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] across body() while the closure pushes to x.items prints 0 where the element is 7; the same holds for a read-only receiver and for a &T parameter of a free function. An existing same-call check refuses each(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 a let f = () => … gets. fn(mut T) -> R does not parse today.

  7. ehartford commented on Nov 19, 2026

    @ehartford
    ContributorAuthor

    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 a mut receiver's place, with the fix-it form, which needs a closure type with a mut parameter (fn(mut T) -> R does not parse).

  8. ehartford commented on Nov 19, 2026

    @ehartford
    ContributorAuthor

    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); no mut in extern "C" function types; the unbound form of a mut fn method 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 the mut receiver or argument, so x.items.each_mut(() => x.count) stays legal.

  9. ehartford commented on Nov 28, 2026

    @ehartford
    ContributorAuthor

    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.

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

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions