Skip to content

D69: g.pull() — fiber-backed Iter[T] over a generator #1725

Closed

Description

@ehartford

Spec: §13.4 Pulling (D69).

g.pull() returns an owned Iter[T] that runs the generator on its own fiber (§14.18) and resumes it at each next(); dropping the pulled iterator early stops the generator exactly as a consumer's break does. pull() is unavailable in no_runtime builds, and a generator that yields views of its own locals or whose body may suspend cannot be pulled (a compile error naming the yield or the suspension point).

The D69 push lowering (branch gen-push-impl) does not implement it: the runtime has no symmetric caller-driven fiber handoff (resume the producer fiber at next(), park it at each yield, and unwind it on drop), and Sema has no pull-eligibility check yet. Until this lands, g.pull() on a generator value is a compile error citing this issue.

Repro:

gen fn upto(n: i32) -> i32:
    for i in 0..n: yield i
fn main:
    var steps = upto(3).pull()
    print(steps.next().unwrap())

Expected: prints 0. Actual: compile error "g.pull() ... is not implemented yet (#this)".

Also needed for: zip/peekable over generators (they take Iter[T]).

Activity

  1. 8 remaining items

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