Skip to content

D69: g.pull() of a generator whose arguments are views (an ephemeral generator value) #1732

Closed

Description

@ehartford

Spec: §13.4 Pulling (D69) forbids pulling a generator that yields views of its own locals; a generator whose arguments are views (gen fn lex(src: &str) -> Token, gen fn nonempty(lines: &Vec[str]) -> &str) may be pulled, and the pulled iterator is then as ephemeral as the generator value (§5.5).

The first g.pull() (#1725, branch gen-push-pull) implements pull as std.task.gen_pull(g) -> Pulled[T]: a heap PullCore whose closure owns the generator value until the coroutine consumes it. When the generator value is ephemeral, that closure is an escaping closure capturing an ephemeral value ("escaping closure cannot capture ephemeral references" inside std.task), and Pulled[T] is not ephemeral, so nothing would stop the pulled iterator from outliving the viewed argument.

Needed: a pulled iterator whose ephemerality follows the generator value's (Sema decides it at the g.pull() call: the result type is ephemeral exactly when the generator value is), and a gen_pull body the escape check accepts for that case.

Until then g.pull() on an ephemeral generator value is a compile error naming this issue.

Repro:

gen fn nonempty(lines: &Vec[str]) -> &str:
    for line in lines:
        if line.len() > 0:
            yield line

fn main:
    let v: Vec[str] = ["a".clone(), "".clone(), "c".clone()]
    var p = nonempty(&v).pull()
    print(p.next().unwrap())

Expected: prints a. Actual: compile error citing this issue.

Activity

  1. ehartford commented on Oct 3, 2026

    @ehartford
    ContributorAuthor

    Fixed on main by 9307016 (#1965): g.pull() of a generator whose arguments are views is allowed; the pulled iterator carries the generator's origins (gen_pull_view_nodes) and Pulled[&T].next() views the receiver's origins, so the pulled iterator is as ephemeral as the generator value (§13.4, §5.5). Fixtures behav_gen_pull_view_args / err_gen_pull_view_escapes / err_gen_pull_view_mutated. Battery green.

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