Repository navigation
D69: generators lower as calls — push generators, std.generators pipelines - #1731
Merged
Merged
Conversation
ehartford
force-pushed
the
gen-push-impl
branch
from
September 26, 2026 19:21
6a072c5 to
d06b5bc
Compare
ehartford
force-pushed
the
gen-push-impl
branch
3 times, most recently
from
September 27, 2026 02:39
b9b97d5 to
296d7c2
Compare
added 3 commits
September 26, 2026 23:10
…ody at each yield §13.4 (D69). A `gen fn f(p) -> T` is three functions: `f` evaluates its arguments into the generator value (a compiler-generated struct of the parameters that implements the new std trait Gen[T]); the value's `move fn each(body: fn(T) -> bool)` hands them to the producer; the producer is f's body lowered with a last parameter `body`, where each `yield e` calls `body(e)` and, when it answers false, leaves the generator as if by `return` — defers, scopes and statement temporaries released on that path. #1534 (a `for` in a generator lost its counter) is gone with the state machine. `for x in g` over a Gen[T] (Sema decides: resolve_gen_for records T, the body's callable type, the `each` callee and its specialization) lowers the loop body to a closure passed to `each` (lower_for_gen): finishing or `continue` answers true, `break` answers false; `return`, `?`, cancellation and a break/continue of an outer label answer false after setting the owning frame's flag, which acts on it once `each` returns — Go 1.23's range-over-func rewrite with the stop automatic. A returned value is written straight into the owning function's return slot through a capture; nested generator loops and a `yield` inside a loop over another generator (recursion) compose. Yielded views of the generator's own locals are call arguments; the no-references-across- yield rule and the `.await` prohibition are retired, and a generator whose body may suspend makes its consuming loop may-suspend (Sema and MirSuspendCheck, which now counts a suspending closure passed to a call). Retired: Sema's state-struct collection (finalize_generator_state_type, generator_collect_state_fields), MirLower's next-body transform and its save/restore/blank helpers, TK_YIELD, MirCore's is_generator_next exemption, codegen's generator field side tables, AsyncMir's Generator flavor and Yield suspension. Loud errors for what is not implemented: `g.pull()` (#1725), generic gen fns and gen methods (#1724), a comprehension clause over a generator (#1727, which hung at run time before). Found on the way and fixed at the owner: - closure captures are resolved by the creating body's local id (anonymous_capture_sources), not by name at codegen: a closure inside a closure captured the capture's pointer slot (writes were lost, a str capture crashed), and a same-named later local could be taken; a capture of a view binding with no local goes through a &T of its place; - the ownership validator's leak rule no longer counts a closure's capture (the creating frame owns it), which rejected any closure that reassigns a captured str; - `(self.run)(body)` reads the callable in place like `self.run(...)`; - a closure with unannotated parameters passed to a generic function is typed from its parameter once the other arguments bind (`map(g, it*2)`, `apply(v, it > 4)` typed `it` as i32 before), fn(A) -> R parameters bind type parameters, a method's callable parameter types a closure argument (user methods defaulted it to i32), and an `impl Trait` parameter's concrete type is part of the specialization key; - a break/continue whose target MIR cannot find is a phase bug, not a silently dropped jump.
The ordered pipeline stages take any Gen[T] and are Gens themselves (GenStage[T] owns the stage before it inside the callable that runs it), so `g |> map(f) |> filter(p) |> take(n) |> collect[Vec]()` is as lazy as the generator it starts from, and `take` stops the whole chain at the generator's yield by answering false. Until §18.2's fallback tier lands (#752) the stages are imported: `use std.generators.{map, filter, take, collect}`.
…ines, await New: #1534's repro and loops over a range, a Vec and a slice inside a generator; break/return/?/labeled break/continue from the consumer, each stopping a generator that holds an owned resource and a defer (debug allocator: released once, no leak); nested generator loops with returns and labels; views of the generator's own locals and a view retained past the body (rejected); a generator value stored in a struct and consumed later; a recursive walk; pipeline stages; .await in a generator and in the consuming body; the no_suspend check through a generator; nested closure by-place captures; loud errors for pull (#1725), generic gen fns (#1724) and comprehensions over generators (#1727). Rewritten to the D69 rules (the spec changed): spec_ss13_4_generators stepped generators with next(), which D69 moves to g.pull() — it now consumes them with `for` and checks the same values plus #1534 and an early break; spec_ss13_4_no_references_across_yield pinned the retired rule and becomes spec_ss13_4_generator_views (views of the generator's own locals and of its arguments). err_generator_await and err_generator_yield_local_ref pinned the retired prohibitions and are removed; behav_gen_push_await and spec_ss13_4_generator_views are their positive successors.
ehartford
force-pushed
the
gen-push-impl
branch
from
September 27, 2026 03:10
296d7c2 to
65e798f
Compare
Contributor
Author
|
Stack battery GREEN on the stack top 5fc8160 (on main 2e8defa; host darwin-aarch64, pinned seed), run once for all six layers with
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements spec v7.3 §13.4 (D69, #1723). Fixes #1534.
gen fnlowers to a constructor + producer +Gen[T].each; eachyieldcalls the consumer's body, and a stop leaves the generator at itsyieldas if byreturn(defers, scope drops, temporaries released once — debug-alloc tests).for x in g:over aGen[T](Sema's decision) lowers its body to a closure;break/continue/return/?/labeled jumps are carried acrosseach..awaitis allowed in generator bodies; the old state machine,TK_YIELD,is_generator_nextand codegen's generator side tables are removed.std.generators:map,filter,take,collectoverGen[T].Still NON-COMPLIANT (loud errors with issues):
g.pull()#1725; generic/methodgen fn#1724; comprehensions over generators #1727; goto out of a generator loop #1730. Pipelines needuse std.generators.{...}until #752.Local gate green (build, build.w, lib/std/build.w, abi-hash-check, all corpora, examples). Drop-scheduling change: battery alone with :drop-audit and :move-audit. Draft until green.