Skip to content

Sema: a callable parameter or field suspends only when some caller hands it a callable that may (#2511) - #2538

Merged
ehartford merged 3 commits into
generic-recursionfrom
may-suspend-params
Oct 21, 2026
Merged

ehartford merged 3 commits into
generic-recursionfrom
may-suspend-params

Conversation

@ehartford

@ehartford ehartford commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

#2511 (§14.3 INVARIANT 5): a callable parameter or field suspends only when some caller or store hands it a callable that may. settle_callable_param_facts runs inside Sema's suspend fixpoint: every call Sema can attribute to a declared fn (direct, resolved generic or std.traits adapter, pipeline stage) marks (fn, parameter) for each callable argument that may suspend; every struct literal or field assignment marks (owner, field). A parameter or field answers that mark at a call site; a fn whose name escapes as a value, or any unattributed call that passes a may-suspend callable, keeps the old answer (may) for everything. Measured (-O3, 4M i64): fold through a fn parameter 8.4 ms → 1.49 ms (the concrete-loop floor); std.traits map|filter|fold 52 ms → 4.4 ms, within 5% of the retired intrinsics' 4.2 ms. 26 suspension fixtures green; gate green (515 s). Layer 2 of stack #2528; the D103 layer follows.

The ListIter layer's parity with the retired intrinsics (second commit)

The first battery on this stack went red on ten fixtures, every one a place where the std adapters now stand where the list-iterator intrinsics stood. The commit closes each gap and the compiler holes the intrinsics had hidden:

  • a closure whose body calls a named function took that callee's return type over the expected -> Unit (the partial-application guess outranked the demand); for_each(n => sink(n)) and a user generic fn(&T) -> Unit callback refused it on main too;
  • collect[HashMap|BTreeMap|String] over an Iter[T] implementor, the method spelling it.collect[List[i32]](), peekable's §13.3 wording, in over a view (D139), flat_map over a built collection (IntoIter), enumerate+collect and unzip over views, by-value comparators for min_by/max_by over views, and _views twins for every predicate adapter so the predicate sees &T, never &&T;
  • a closure holding self by place passed to an adapter that stores it only in its ephemeral result is not an escape;
  • a T at an Option[T] demand converts when T is itself an Option (a generic body's instance);
  • the suspend walk: a field-held callable answers for the body's own fact, a generic method resolved by symbol maps to its template, a return's value is read from d0 (nothing under return was walked, on main too), and loop:/do-while are entered. Fixture da_loop_field_callable_cancel.w resumes a cancelled caller on main.

Filed: #2541 (an Option payload matched out of a field binds a copy; a mutation on it is lost).

… store hands it a callable that may (#2511, section 14.3 INVARIANT 5)

A call through a callable parameter was a may-suspend site whatever flowed
into it, and MIR polled with_fiber_wait_cancelled after every such call
(4.5x on a fold). settle_callable_param_facts runs inside the suspend
fixpoint: every call Sema can attribute to a declared fn (direct, resolved
generic or std.traits adapter, pipeline stage) marks (fn, parameter) for
each callable argument that may suspend; every struct literal or field
assignment marks (owner, field) the same way. A parameter or field answers
that mark at a call site; a fn whose name escapes as a value, or any
unattributed call that passes a may-suspend callable, keeps the old answer
(may) for everything. Measured (-O3, 4M i64): fold through a fn parameter
8.4 ms -> 1.49 ms (the concrete-loop floor); std.traits map|filter|fold
52 ms -> 4.4 ms, within 5% of the retired intrinsics' 4.2 ms. 26 suspension
fixtures green.
@ehartford
ehartford added this pull request to stack #2539 October 21, 2026 17:20
@ehartford ehartford changed the title may suspend params Sema: a callable parameter or field suspends only when some caller hands it a callable that may (#2511) Oct 21, 2026
Eric Hartford added 2 commits October 21, 2026 16:33
…end walk it exposed

The battery's ten reds all came from the std adapters standing where the
list-iterator intrinsics stood. Each is a gap the intrinsic had covered, or
a compiler hole it had hidden:

- A closure whose body calls a named function took that callee's return
  type over the expected `-> Unit` (the partial-application guess outranked
  the demand), so `for_each(n => sink(n))` and a generic `fn(&T) -> Unit`
  callback refused a closure returning a value; the demand now wins
  (behav_closure_call_body_under_unit_callback).
- `collect[HashMap]`, `collect[BTreeMap]` and `collect[String]` over an
  Iter[T] implementor: std builders (iter_collect_hashmap/btreemap/string
  with `_views` twins), the element shape judged at the call with the
  diagnostics the fixtures expect; the method spelling
  `it.collect[List[i32]]()` dispatches to std as the pipeline one does.
- `peekable` on an Iter implementor says "not implemented yet" (§13.3), as
  it does on the built-in iterators.
- A closure holding `self` by place passed to `iter_map`: the callee stores
  it only in its ephemeral result (a Mapped over a view of this frame),
  which cannot outlive the captures, so it is not an escape; a callee that
  writes a global still is.
- `in` reads a view of a Copy value through every layer (D139), in Sema
  and in the array-literal lowering.
- `flat_map` over a function that builds a collection (`s.split(" ")`):
  iter_flat_map_into over IntoIter; both flat-map adapters hold the
  iterator being drained with explicit ownership (an Option payload
  matched out of a field is a copy, #2541) and live in std.collections.
- `enumerate() |> collect[List]()` and `unzip` over views store values:
  iter_collect_pair_views, iter_unzip_views; min_by/max_by over views take
  the comparator by value (D111).
- `filter`, `take_while`, `drop_while`, `any`, `all`, `none`, `find`,
  `position` over an iterator of views hand the predicate the view itself
  (`_views` twins), never a view of the view.
- A `T` at an `Option[T]` demand converts when `T` is itself an Option
  (`ListIntoIter[Option[i32]].next`); a bare `None` never wraps (D141).
- audit:resolution records a D103-converted operand with the Option Sema
  demanded.

The #2019 cancellation fixture then found the suspend walk itself:
- a callable held in a field answers from the stores for the body's own
  fact (a parameter's caller-answers rule has no caller for a field);
- a generic fn or method resolved by its own symbol (`i.next()` on an
  `I: Iter[T]` receiver) maps to its template declaration for the fact;
- the walk reads a `return`'s value from d0 (it read d1, NK_UNARY's slot,
  so nothing under `return` was walked), and enters `loop:` and `do-while`
  (da_loop_field_callable_cancel: main resumes a cancelled caller past
  `return (self.f)(x)`).
`x in list` is `list.contains(x)`, whose parameter observes the key: a
view is already the argument it takes. The D139 read-through added for
`*x in [2, 4]` (an array literal compares values) also applied there and
handed the call a value where it expected a reference (an ICE in MIR,
behav_2009_membership_view_operand). The read is now limited to the
array-literal, range and str lowerings.
@ehartford
ehartford marked this pull request as ready for review October 21, 2026 20:53
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on 9642def (stack #2539 top: #2526, #2536, #2537, #2538): gate, uat and battery-checks under the pinned seed, logs in out/battery/.

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