Skip to content

Bug: ref-capturing generators escape their referent's scope (§13.4) #442

Closed

Description

@ehartford

Summary

Generators that store captured or parameter references in their state machine must become ephemeral values. Current generator state construction records parameter and local field types but creates an ordinary struct state type, so a ref-capturing generator can escape the referent's lifetime.

Spec

docs/with-specification.md:5380-5402 forbids references across yield in ordinary generators. docs/with-specification.md:5427-5435 says a generator that captures references is itself ephemeral, cannot be stored or returned beyond the captured lifetime, and may be returned only from an ephemeral function when it captures an input reference. docs/requirements.md:5027-5030 records requirement 13.4.1.25 for this rule.

Current state

src/SemaDecl.w:1002-1015 creates the generator state as a normal TY_STRUCT. src/SemaCheck.w:592-603 records generator state fields by symbol and type. src/SemaCheck.w:606-687 collects locals used by the state machine, and src/SemaCheck.w:701-712 adds the resume slot and function parameters before collecting body fields. src/SemaCheck.w:715-725 writes those fields into the type table.

There is no corresponding step that marks the generated state type ephemeral when any state field is a reference or otherwise depends on an ephemeral origin. Existing generator tests cover ordinary yielding in test/spec/spec_ss13_4_generators.w and local reference yield rejection in test/compile_errors/err_generator_yield_local_ref.w:1, but they do not cover a generator value escaping after it captured an input reference.

The closure escape checker already has a useful model to mirror: escaping closures check captured ephemeral values at src/SemaCheck.w:7101-7110 and mutating captures at src/SemaCheck.w:7119-7133.

Implementation plan

  1. During or immediately after finalize_generator_state_type, inspect every generated state field type.
  2. If a field is &T, &mut T, a view type with origin dependency, or another ephemeral value, mark the generated generator state type ephemeral and retain enough origin information for escape diagnostics.
  3. Reuse existing ephemeral storage/return checks where possible so ref-capturing generators are rejected when stored in non-ephemeral structs, globals, or return positions that outlive the referent.
  4. Add a diagnostic that names the captured binding and says the generator value captures a reference that cannot escape.
  5. Keep non-ref generators ordinary so existing generator return/use patterns do not become over-restricted.

Test plan

Add compile-error tests for returning a generator that captures a local &Vec[T], storing such a generator in a non-ephemeral struct, and assigning it somewhere that outlives the source collection. Add positive tests for returning a generator with only owned state and for consuming a ref-capturing generator entirely within the referent's scope.

Acceptance criteria

  • A generator state containing reference-derived fields is classified as ephemeral.
  • Ref-capturing generator escape is rejected with a diagnostic that identifies the capture/source.
  • Owned-state generators keep working and remain storable/returnable where currently allowed.
  • Tests exercise both parameter references and local captured references.

Dependencies

May reuse existing ephemerality/origin infrastructure; no separate feature should be required unless the current origin model cannot attach origins to generator state fields.

Traceability

From the requirements campaign (docs/requirements.md, spec v7.1). Affected requirement IDs: 13.4.1.25.

Activity

  1. ehartford commented on Jun 19, 2026

    @ehartford
    ContributorAuthor

    Implemented in 0864ed5. Generator state types are now marked ephemeral when they carry reference-derived state, generator calls record reference origins for escape analysis, and returned escaped generators report the captured source. Added positive scoped/reference-propagation generator tests plus negative local-reference escape tests. Verification passed after rebasing onto current main: build, fixpoint, full test suite, test-green, last-green, and install-user.

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