Skip to content

Full pattern language in for-loop bindings (§13.5) #439

Closed

Description

@ehartford

Summary

For-loop bindings are specified as full patterns, including refutable patterns that skip non-matching elements. The semantic checker already has a pattern path for NK_FOR; the parser currently restricts ordinary for bindings to identifiers, a special tuple/index path, or parenthesized patterns.

Spec

docs/with-specification.md:5440-5489 defines for pattern in expr: body. docs/with-specification.md:5450-5458 explicitly says the binding position is a full pattern and gives tuple, struct, and enum-pattern examples. docs/with-specification.md:5481-5489 says refutable patterns are allowed, non-matching elements are skipped, and normal reference-pattern ergonomics apply.

docs/requirements.md maps this to requirement IDs 13.5.1.1, 13.5.1.6, and 13.5.1.8.

Current state

src/Parser.w:5122-5153 parses for. If the binding starts with (, it calls parse_pattern(); otherwise it requires expect_ident() and has a special comma form for index/item binding at src/Parser.w:5133-5138. Struct patterns such as for { name, age, .. } in users:, enum shorthand patterns such as for .Some(x) in xs:, and slice patterns are rejected before sema.

The rest of the compiler is closer to the spec. src/Ast.w:1484 documents NK_FOR as d0=binding(sym) or pattern(node). src/SemaCheck.w:5240-5244 already distinguishes pattern bindings from simple symbols and calls check_pattern(binding, elem_type) for pattern bindings. src/Parser.w:5603-5864 implements the general pattern parser used elsewhere, including literals, enum shorthand, tuple patterns, struct patterns, and slice patterns.

Implementation plan

  1. Replace the identifier-first parsing in parse_for with a normal binding-pattern parse, preserving the current index/item shorthand only if the language still wants that sugar.
  2. Ensure ast.for_binding_is_pattern returns true for every non-symbol pattern form and false only for a simple identifier binding.
  3. Implement refutable-pattern skip semantics during lowering. The body should run only for elements that match; non-matches should continue the loop without entering the body.
  4. Keep irrefutable pattern bindings on the fast path where possible, but do not special-case away correctness.
  5. Verify reference-pattern ergonomics over .iter() output so bindings over borrowed elements do not require users to write redundant &.

Test plan

Add behavior/spec tests for tuple destructuring, struct destructuring, enum Some(item) filtering, enum shorthand filtering, slice/rest patterns if supported by the existing pattern language, and reference binding over a collection iterator. Add a compile-error test where a pattern is syntactically invalid in for-binding position. Add a refutable-pattern test that proves non-matching elements are skipped, not treated as a runtime failure.

Acceptance criteria

  • for accepts the same pattern language as let and match where the spec says it should.
  • Refutable patterns skip non-matching elements and continue iteration.
  • Existing simple for item in xs: and indexed/tuple forms continue to work or are intentionally migrated with tests.
  • New tests fail against the current parser restriction and pass after the implementation.

Dependencies

No hard dependency. Overlaps #468 for positional struct patterns and #474 for slice/rest pattern lowering if those pattern forms are still incomplete outside for.

Traceability

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

Activity

  1. ehartford commented on Jun 14, 2026

    @ehartford
    ContributorAuthor

    Implemented in 145c9c8. For-loop bindings now route full pattern starts through the shared pattern parser, and MIR lowering gives refutable for-patterns a skip edge to the loop continue path. Added behavior coverage for struct patterns, enum shorthand and qualified variant filtering, slice rest bindings, and refutable range patterns over ranges/Vecs, plus a parse-negative test. Verification passed: build, fixpoint, full test suite, and test-green using the project seed/release compiler path.

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