Skip to content

Closures and views: callable parameters are views; stores into a receiver are tracked (#1698 #1783) - #1842

Merged
ehartford merged 3 commits into
impl-d76from
fix-closures
Sep 28, 2026
Merged

ehartford merged 3 commits into
impl-d76from
fix-closures

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Stack created with GitHub Stacks CLI • Give Feedback 💬

@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on the stack top ecccc1e1 (#1839 → #1844, on main 355c20b; docs-only commits since), driven by the pinned seed cffa4a40: :gate rc=0 in 648 s; :battery-checks (seed-driver, fixpoint, test-with-audits incl. drop and move audits and every test lane, user-programs-safe, last-green) rc=0 in 1680 s. The previous run's one red (behav_trie, a false positive of the closure layer's store rule) is fixed in #1842 (e3ba9f5), with a full behavior/compile-error corpus comparison against main. Merge: gh stack merge 1844 --yes --squash.

Eric Hartford added 3 commits September 28, 2026 15:15
… method that stores its argument's view into the receiver says so in its summary (§21.1, D22)

Root cause. Sema recorded a stored view on its storage only when the
receiver was a bare identifier (SemaCheck.w, the #625 viral store in
check_method_call_parts: `else if self.ast.kind(expr) == NodeKind.NK_IDENT`)
or a std.sync shared receiver (#1778). Every other store was invisible:
`h.v.push(&n)`, `h.r = &n`, `self.v.push(x)` / `self.r = Some(x)` inside a
method, and a method call `h.keep(&n)` that stores its argument into the
receiver. The receiver's binding never learned the origin, so a use of it
after the origin died, its implicit drop (Rule 7), and a return of it all
passed. The fact is the store, not the receiver's spelling (D65).

Fix, in Sema (the owner of view origins):
- note_view_store / note_view_store_into_root: one store primitive for a
  method-call store, a field/element assignment and the guarded `with`
  write-back, keyed on the target's place root. Storage the body owns
  carries the origins on its binding (and becomes an ephemeral value, Rule
  7). The receiver of a `fn`/`mut fn` method is the caller's place (D21):
  a parameter's view stored there is published as the new
  EFF_STORE_IN_RECEIVER effect; a local's view is refused ("this call
  stores a view of `n` into `self`, and `self` outlives this call"). A
  `&H` parameter keeps the #1778 refusal. Inside a closure body the
  parameter frame is the capture frame (closure_body_depth), never a
  receiver.
- propagate_method_call_param_effects: a callee argument whose parameter
  carries EFF_STORE_IN_RECEIVER is stored into the call's receiver
  (`h.keep(x)` and `T.keep(h, x)`), so the caller ties `h` to `x`'s origins.
  A method that may store a view is checked before its callers, as a
  view-returning one is (#1473 ordering).
- Rule 7 (check_live_views_for_origin): a Drop holder's recorded origins
  count, not only its initializer's.
- A block tail that returns a binding publishes its parameter origins while
  the binding is in scope (the body-level call ran after teardown and found
  none), so `var out = h; out.v.push(x); out` ties the caller's result to
  `x`.

Evidence (stage1 of this tree vs base; the matrix cells, `with check`):
  B02 h.keep(&n) in a block, h read after      compiled (5) -> view `h` may originate from `n`
  B03 loop local stored through mut fn          20 20 20     -> refused
  B04 self.r = x through `mut fn set`           compiled     -> refused
  B05 Drop holder, keep(&n), n declared after   end|drop 1   -> Rule 7 refused
  B06 same with h.v.push(&n)                    end|drop 1   -> Rule 7 refused
  B07 keep() into an owned param's field, returned  compiled -> returned ephemeral value may outlive 'n'
  B08 self.inner.v.push(x)                      compiled     -> refused
  B09 keep2 -> keep transitive                  compiled     -> refused
  B10 move fn stores and returns self           compiled     -> refused
  B12 &str view through mut fn                  compiled     -> refused
  B14 h.v.push(&n) directly                     compiled     -> refused
  B15 self.r = Some(x) in a loop                compiled     -> refused
  C04/E01/E02 view stored into a returned local compiled     -> refused
  C05 h.r = &n                                  compiled     -> refused
  D04 method stores its own local into self     compiled (5) -> refused
  A10 non-move closure assigned into c.f        20           -> refused
  B01 / B13 (no use after the origin dies; holder after origin) still compile.
Each err_1783_* fixture fails on the base compiler (5b6830b) and passes
here; behav_1783_mut_fn_stores_view_scoped pins the legal shapes. The #1778
fixtures (err_mutex_*, err_rwlock_*, behav_mutex_set_view_scoped) are
unchanged and pass.

Fixes #1783
Refs #1698
… returning it inside any carrier is an escape the call site refuses for a non-move closure (§12.4, D63 (3))

Root cause. §12.4 / D63 (3): "A non-`move` closure passed as an argument
is ephemeral in the callee exactly as a `&T` parameter is (Rule 8, §22.1):
it may be invoked and passed on, and may not be stored, returned, or
captured by a `move ||` closure." The call-site check exists
(finalize_closure_arg_checks: a by-place closure argument to a parameter
with EFF_ESCAPE_VALUE), but the callee's summary only recorded
ESCAPE_VALUE when the parameter itself was the returned place
(note_returned_place_effect: `fn keep(f) -> fn: f`). A `fn(A) -> R`
parameter is not an ephemeral type, so Sema never saw it as a view: moved
into a struct literal, `Some(f)`, a tuple, a pushed Vec, the receiver, or a
local later returned, it left the callee with no effect, and a non-move
closure over the caller's frame came back out (SemaCheck.w
expr_is_ephemeral_value / collect_expr_view_deps /
compute_expr_view_origin_mask had no case for it).

Fix, in Sema:
- callable_param_is_view: a `fn(A) -> R` parameter of the checked function
  is an ephemeral value whose origin is the parameter itself (not inside a
  closure body, whose parameter frame is the capture frame); it is not a
  stack-local origin (it views the caller's argument).
- Carriers are transparent (§21.1 rule 10): a tuple, a variant (literal,
  shorthand or constructor call) holding an ephemeral value is one; a
  `move` closure capturing a callable parameter or a view binding carries
  it; a binding of a callable-holding type records the origins
  (type_holds_callable keeps the recording off Copy reads through `&T`).
- note_param_effect: a callable parameter noted ESCAPE_VIEW (returned
  inside a carrier) or STORE_IN_RECEIVER (#1783) is the callable escaping:
  ESCAPE_VALUE, which the call-site check reads and the effect fixpoint
  carries to relaying callers.
- A `move ||` closure that escapes may not capture a binding that views a
  stack local of this frame (a non-move closure, or a value holding one).

Evidence (matrix, `with check`, base 5b6830b -> this tree):
  A05 callee pushes f into a returned Vec     compiled, printed 0 -> refused at the call
  A06 callee returns Cnt { f: f }             compiled, printed 0 -> refused
  A08 callee returns Some(f)                  compiled, printed 0 -> refused
  A09 callee returns (1, f)                   compiled            -> refused
  A16 mut fn add(f): self.fs.push(f), loop    20 20 20            -> refused
  A20 let g = f; Some(g)                      compiled            -> refused
  A22 fn add(fs, f) pushes into returned Vec  20                  -> refused
  A25 fn add(r: Reg, f) stores into r, returns r  20              -> refused
  A07 move closure captures non-move closure binding, returned  compiled -> refused
  B11/B16 closure stored through mut fn / Drop holder           compiled -> refused
  A01, A19, A21, A23, A24, D02 still compile and run (leak count=0).
Diagnostic at the call: "closure argument holds `xs` by place — a view of
this frame — and `wrap` stores or returns its parameter (§12.4); pass an
owning closure: `move () => ...`". Each err_1698_* fixture fails on the
base compiler and on the #1783 commit alone, and passes here, except
err_1698_callable_param_captured_by_returned_move_closure, which pins the
case that already worked. behav_1698_callable_param_view_tied pins the
owning spellings through storing callees and a non-move closure through an
invoking/relaying callee.

Fixes #1698
…view; an auto-referenced place stored into a `&T` slot is its own origin (§21.1, §3.8)

Root cause. note_view_store (SemaCheck.w) recorded the stored
expression's view deps whenever its callers' gate passed, and the gate was
`type_is_ephemeral_value(value_ty) or expr_is_ephemeral_value(value)`.
expr_is_ephemeral_value answers for the expression it was derived from:
`*v` of `v: &i32` is "ephemeral" because `v` is, though it is a Copy read
producing an independent i32. So `seen[count] = *v` (test/behavior/
behav_trie.w, red in the stack battery) made `seen: [i32; 3]` view `v`,
and the read of `seen` after the loop was refused. The same held for
`xs.push(*r)` (refused on main too, by the old #625 path), a struct field,
a nested Vec, and a `mut fn` doing `self.xs.push(*x)`, whose receiver was
then tied to the caller's argument.

The inverse hole sat on the same gate: an auto-referenced place reaching
a `&T` slot (`h.keep(n)` for `keep(x: &i32)`, `h.v.push(n)` into
`Vec[&i32]`) has a value type (`i32`) that holds no view, so the gate
dropped the store and the receiver never learned `n` — accepted on main
and on the previous commit.

Fix, at the owner: note_view_store decides from types, not from the
expression's derivation. It takes the slot's type (the parameter, the
element, the field) beside the value's, and records a store only when one
of them can hold a view (type_can_carry_view: an ephemeral type, or one
that can hold a callable), or the value is an ephemeral task. When only
the slot can (auto-referencing), the place's storage root is the origin.
The call-site gates are gone; the three sites pass the slot type.

Evidence (`with check`; before = release binary of the previous commit,
main = installed v0.15.3.0-g355c20b16, after = stage1 of this tree):
  behav_trie `seen[count] = *v`         before refused   main ok       after ok
  array  `seen[0] = *r`                 refused          ok            ok
  Vec    `got.push(*r)`                 refused          refused       ok
  field  `a.last = *r`                  refused          ok            ok
  nested `b.xs.push(*r)`                refused          ok            ok
  mut fn `self.xs.push(*x)`, `c.add(&n)` refused         ok            ok
  Option[&i32] elimination, `picked[0] = *v` refused     ok            ok
  `h.keep(n)` auto-ref, h read after n  accepted         accepted      refused
  `h.v.push(n)` into Vec[&i32], h read after n  accepted accepted      refused
behav_1783_copy_read_store_is_owned pins the Copy-read shapes (fails
before, passes after); err_1783_autoref_method_arg_stored and
err_1783_autoref_store_into_field_path pin the auto-ref stores (compile
before, refused after). Every err_1783_* / err_1698_* / #1778 fixture is
unchanged and passes.

Refs #1783
@ehartford
ehartford merged commit 1cc26f8 into main Sep 28, 2026
@ehartford
ehartford deleted the fix-closures branch September 28, 2026 19:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant