Skip to content

std.generators stages reject a generator whose arguments are views (escaping closure cannot capture ephemeral references) #1737

Closed

Description

@ehartford

Spec: §13.3 / §13.4 (the ordered stages accept a Gen[T]); §5.5 (ephemeral values).

A pipeline over a generator whose arguments are views is rejected inside std.generators:

use std.generators.{map, take, collect}
gen fn over(xs: &Vec[i64]) -> &i64:
    for x in xs:
        yield x
fn main:
    let v: Vec[i64] = [4, 5, 6]
    let firsts = over(&v) |> map(it * 2) |> take(2) |> collect[Vec]()
    print(firsts.len())
error: escaping closure cannot capture ephemeral references
 --> <embedded-std>/std/generators.w:24:26
24 |     GenStage { run: move (body: fn(U) -> bool) => g.each(x => body(f(x))) }

Same on gen-push-impl. A stage over an ephemeral generator is itself ephemeral (it holds the generator), and the chain is consumed within the statement, so the program is valid; §13.4 does not exclude ephemeral generators from the stages. Related: #1732 (pull of an ephemeral generator).

Activity

  1. ehartford commented on Sep 26, 2026

    @ehartford
    ContributorAuthor

    When this is lifted: a stage chain over a view-holding generator must keep the generator's views live while the chain runs, as #1734 (bce516e on gen-push-fixes) does for for x in g and comprehension clauses (register_gen_loop_view_borrows) — a stage closure that mutates the viewed place would otherwise race the producer.

  2. added 20 commits that reference this issue on Sep 27, 2026
  3. ehartford commented on Oct 3, 2026

    @ehartford
    ContributorAuthor

    Fixed on main by 9307016 (#1965): std.generators stages hold the generator as a field (§12.2 forbids an escaping closure over an ephemeral; §5.2 propagates), so a stage chain over a view-holding generator keeps the views live while the chain runs; compute_expr_view_origin_mask gained the NK_STRUCT_LIT case and generic free calls now record view origins (a pre-existing hole). Fixtures behav_gen_stages_view_args / err_gen_stage_view_mutated / err_gen_stage_escapes_block. 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