Skip to content

Sema: a live closure's mutate capture is not exclusive — the enclosing scope may read/write the captured place between two calls (§12.4, D62 non-compliance) #1691

Closed

Description

@ehartford

Spec §12.4 (D62): "A non-move closure holds a view of each captured place while it is alive, under the ordinary exclusivity rules (§5): mutating a place in the enclosing scope while a closure holding a read capture of it is alive is an error." A mutate capture is the symmetric case: while the closure is alive, other access to the captured place is an exclusivity error.

Repro (installed compiler 7456c37b…, main 1e839443) — compiles and runs, prints 1 then 2:

use std.builtins.print_i32
fn main:
    var body: Vec[i32] = Vec.new()
    let sink = k => body.push(k)
    sink(1)
    print_i32(body.len() as i32)   // read of `body` while `sink` (mutate capture) is alive
    sink(2)

Same through an ephemeral holding &sink (type Keeper = ephemeral { f: &fn(i32) -> Unit }; keep.f(1); body.len(); keep.f(2)).

Expected: refused — body is held by a mutate view (sink) that is still alive (used again below). Actual: accepted.

Why it matters now: D66's retained variadic pairs (#1652, #1679) define the retention of a callback's userdata as this same view, held by the resource until reset/destroy/last callback-capable operation (Eric's ruling, 2026-09-25). Refusing a use of the sink inside that window depends on Sema enforcing capture exclusivity at all; today it enforces neither the plain case above nor the retained one.

Not a spec question: §12.4 already states the rule.

Activity

  1. ehartford commented on Sep 25, 2026

    @ehartford
    ContributorAuthor

    Narrowed: mutation against a live closure view is enforced for both capture modes (n = 5 after let g = () => n += 1 → "cannot mutate n while g is a live view into it"; same for a read capture). The gap is reads only: g(); let seen = n; g() with g = () => n += 1 is accepted, and so is sink(1); body.len(); sink(2). Fix: a read check against live EXCLUSIVE views (check_mutation_against_views only consults SHARED ones).

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

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions