Skip to content

Tests: ephemeral iterators coverage (§13) #497

Closed

Description

@ehartford

Tests: ephemeral iterators coverage (§13)

Summary

§13.1's two practical rules — you cannot return an opaque lazy iterator that borrows its inputs, but you CAN return a concrete ephemeral struct that implements Iter — are implemented behavior with no test coverage. Verified by hand: the concrete-ephemeral-struct pattern compiles and runs correctly today, storage of the ephemeral iterator is rejected, and the opaque -> dyn Iter[T] return form does not parse. This is a write-tests-and-close issue; the survey notes ("no opaque iter return form exists; ephemeral Iter struct return untested") are confirmed accurate.

Spec

§13.1 (docs/with-specification.md:5102-5130):

  • 13.1.1.3: "What this means in practice: You cannot return an opaque lazy iterator that borrows from its inputs (e.g., -> dyn Iter[StrView])."
  • 13.1.1.4: "However, you CAN return a concrete ephemeral struct that implements Iter:"
// IMPOSSIBLE: opaque return type hides the ephemerality
fn find_matches(text: &str, pat: &str) -> dyn Iter[StrView]

// POSSIBLE: concrete ephemeral struct — caller inherits restriction
type MatchIter = ephemeral { text: StrView, pat: StrView, pos: usize }
impl Iter[StrView] for MatchIter: ...
fn find_matches(text: &str, pat: &str) -> MatchIter:
    MatchIter { text: text.as_view(), pat: pat.as_view(), pos: 0 }
// Caller's binding is ephemeral — cannot store, must use in this scope

The concrete approach works because the caller sees the ephemeral type and inherits the restriction (Rule 8, §22.1); the opaque approach fails because trait objects erase ephemerality.

Current state

Verified against ./out/bin/with-stage2 (2026-06-10):

  • 13.1.1.4 positive — works, untested: a type MIter ephemeral { ... } with impl Iter[i64] for MIter, returned by value from a constructor function and driven via .next() in the caller, builds and runs correctly.
  • Storage negative — works, untested: declaring a non-ephemeral struct with an MIter field is rejected: error: ephemeral type 'MIter' cannot be stored in non-ephemeral struct.
  • 13.1.1.3 — vacuously enforced, untested: a bare -> dyn Iter[i64] return type does not parse (error: expected ':' or '{' to introduce body at the [); only &dyn Trait exists in the type grammar. So the opaque-borrowing-iterator form is unreturnable by construction. The test should lock in the rejection in whatever form it occurs so a future grammar extension cannot silently make opaque borrowing returns legal without revisiting §13.1.
  • Known checker hole (out of scope here, do not write a failing test): heap-container storage of ephemeral values (Vec[MIter] annotation) is NOT rejected today — §5.2 container propagation is Make ephemerality provenance-tracked and modular #362's phase 5; reference it in a comment instead.
  • Existing nearby coverage: test/spec/spec_ss13_2_iterator_borrowing.w (borrowing/collect-owned patterns), test/spec/spec_ss05_5_ephemeral_structs.w (ephemeral struct basics) — neither covers the return-an-ephemeral-iterator pattern or the opaque rejection.

Implementation plan

Tests only; no compiler or stdlib changes.

  1. Add test/spec/spec_ss13_1_ephemeral_iter_return.w — the 13.1.1.4 positive, modeled on the spec's find_matches:
    • A concrete ephemeral struct implementing Iter[T] over borrowed-ish data (use the i64-state shape that works today; a StrView-borrowing variant if as_view surface allows).
    • A constructor fn returning it by value; the caller drives it with while let Some(x) = iter.next(): (or match-loop) and asserts the yielded sequence.
    • Pass the iterator to a helper as a parameter (ephemeral params are legal) and return it through one more propagating function layer (Rule 8 inheritance).
  2. Add test/compile_errors/err_ephemeral_iter_struct_storage.w — //! expect-check-fail: ephemeral type — caller tries to store the returned iterator in a non-ephemeral struct field ("caller inherits restriction").
  3. Add test/compile_errors/err_opaque_borrowing_iter_return.w — fn find(xs: &Vec[i64]) -> dyn Iter[i64]: ... with the appropriate expect directive for the current parse rejection (//! expect-check-fail: matching the emitted error). Include a comment citing §13.1 13.1.1.3 explaining the test's intent: bare dyn returns must never become a legal spelling for borrowing iterators without ephemerality propagation.
  4. Optional breadth (same files): an escaping-closure capture of the ephemeral iterator → rejected (shares machinery with err_escaping_bound_closure_captures_ref.w); a comment cross-referencing Make stdlib guards and borrowed-data iterators canonically ephemeral (§5.3) #477, which will make the stdlib iterators (VecIter et al.) ephemeral and is where stdlib-iterator storage negatives belong.
  5. Wire the files into the test suite the same way the existing test/spec and test/compile_errors fixtures are discovered (directive-driven; no harness changes expected).

Test plan

The issue is the test plan; concretely:

  • test/spec/spec_ss13_1_ephemeral_iter_return.w — //! expect-stdout: ok; positive: construct → return → iterate → assert values; propagate through param and return layers.
  • test/compile_errors/err_ephemeral_iter_struct_storage.w — //! expect-check-fail: ephemeral type 'MatchIter' cannot be stored in non-ephemeral struct (exact message verified modulo type name).
  • test/compile_errors/err_opaque_borrowing_iter_return.w — //! expect-check-fail: on the -> dyn Iter[T] spelling.

Acceptance criteria

  • 13.1.1.4 has a passing positive spec test (ephemeral Iter struct returned and consumed).
  • The caller-inherits-restriction storage rejection has a compile-error test.
  • 13.1.1.3's opaque-return impossibility is locked in by a compile-error test with a spec-citing comment.
  • No compiler/stdlib changes; all existing tests still green.
  • with build && with build :fixpoint && with build :test pass (and with build :test-green recorded).

Dependencies

Traceability

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

Activity

  1. ehartford commented on Jul 3, 2026

    @ehartford
    ContributorAuthor

    Closing — user ephemeral Iter[T] returned/driven/propagated (spec_ss13_1_ephemeral_iterator) + opaque dyn Iter return rejected (err_opaque_iter_return), pinned 13f4f88; ephemeral-storage negative already covered by err_ephemeral_struct_in_struct / err_vec_iter_struct_storage. §13.1.

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