Skip to content

D69: the body of for x in g may mutate a place the generator views (aliasing accepted) #1734

Closed

Description

@ehartford

Spec: §13.4 (D69 push generators), §21.1 (views), §2 (exactly as safe as Rust).

The body of for x in g may mutate a place the generator value views. Sema rejects the mutation between creating the generator and the loop (cannot mutate lines while g is a live view into it), but not inside the loop body, which runs while the generator is iterating that very place:

gen fn each_line(lines: &Vec[str]) -> &str:
    for line in lines:
        yield line

fn main:
    var lines: Vec[str] = ["a".clone(), "b".clone()]
    var n = 0
    for l in each_line(&lines):
        n += 1
        if n < 40:
            lines.push(f"line {n}")   // accepted; the producer's loop still walks the old buffer
    print(n)

with check says ok (gen-push-impl, and gen-push-fixes). The equivalent over a Vec is rejected: for p in ps: ps.push(...) → "cannot mutate ps while p is a live view into it". The same hole applies to a generator method's borrowed receiver (#1724: for x in c.walk(): c.items.push(..)).

Expected: the generator value's view dependencies (record_generator_call_ref_origins) are live borrows for the whole loop, as the implicit .iter() borrow of a for over a collection is; a mutation of a viewed place in the body is a compile error.

Found while fixing #1724 (branch gen-push-fixes).

Activity

  1. added 5 commits that reference this issue on Sep 26, 2026
    bce516e
    38869a7
    c03d8a4
    fa59b3c
    ed31ca7
  2. added a commit that references this issue on Sep 27, 2026
    f50c708
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