Skip to content

Generalize @[iter_of_self] to user iterator constructors #376

Closed

Description

@ehartford

Summary

Premise appears stale. The current compiler already records @[iter_of_self] on user functions and impl methods, and method-call borrowing consults that marker. The remaining useful work is to add focused regression coverage proving a non-stdlib iterator constructor retains access to self.

Spec

docs/with-specification.md:5189-5199 says @[iter_of_self] is an explicit function attribute for iterator constructors that return a view over their receiver, and that any library can use it. docs/with-specification.md:5202-5207 requires two live next() results from the same iterator to be accepted while mutable access to the source collection remains forbidden. docs/with-specification.md:5209-5213 says unannotated custom constructors may use the conservative fallback.

Current state

src/Parser.w:518-520 recognizes the iter_of_self attribute. Ordinary function declarations apply the pending marker at src/Parser.w:1133-1135, and impl methods apply it at src/Parser.w:2857-2859. src/Ast.w:1229-1235 stores and queries the marked function node.

The borrow checker path is also not stdlib-only: src/SemaCheck.w:13192-13206 first checks the builtin iterator allowlist, then resolves the method function and asks ast.is_iter_of_self_fn_node(fn_node). Method calls register the retained receiver borrow at src/SemaCheck.w:10413-10419, and normal calls do the same for marked callees at src/SemaCheck.w:8071-8078.

Existing tests cover stdlib iterators: test/compile_errors/err_iter_of_self_vec_iter.w:1 and test/compile_errors/err_iter_of_self_hashmap_keys.w:1 reject mutable access while iterator views are live, and test/behavior/behav_iter_of_self_independent.w verifies independent values are not over-restricted. test/spec/spec_ss13_2_iterator_borrowing.w does not include a user-defined @[iter_of_self] constructor, so the stale premise is not guarded by a direct test.

Implementation plan

  1. Do not rewrite the compiler unless a new repro contradicts the current source.
  2. Add a non-stdlib type with an impl method annotated @[iter_of_self] that returns a custom iterator over self.
  3. Add one compile-error test that keeps a yielded view live and then mutates the receiver, expecting the same retained-access diagnostic as the stdlib tests.
  4. Add one positive test showing an unannotated owned iterator constructor remains usable without retained receiver borrowing.

Test plan

Run the new compile-error test through with build :test and confirm the failure text includes the retained-access diagnostic. Run the positive behavior/spec test to confirm user-defined iterators still iterate normally. Then run the normal full checklist required by the repository before closing.

Acceptance criteria

  • A user-defined @[iter_of_self] impl method is covered by tests.
  • The test fails on a build where method_is_iter_of_self_fn ignores user markers.
  • No stdlib-only special case is introduced.
  • The issue can be closed as premise-stale if the tests pass without compiler changes.

Dependencies

None. This is a verification/regression-coverage cleanup for existing behavior.

Activity

  1. ehartford commented on Jun 13, 2026

    @ehartford
    ContributorAuthor

    Implemented in 9d4d829. User @[iter_of_self] constructors now survive impl-method parsing and AST comptime cloning, and semantic checks consult the resolved function symbol as well as builtin method names. Added a positive unannotated user iterator test and a negative user @[iter_of_self] retained-receiver conflict test. Verification passed: build, fixpoint, full test suite, and test-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