Skip to content

Positional struct patterns: Point(x, y) (§9.7) #468

Closed

Description

@ehartford

Summary

Implement positional struct patterns such as Point(x, y) so struct matching can use declaration-order field destructuring where the type is explicit. The current compiler treats that syntax like an enum variant payload pattern and rejects it.

Spec

§9.7 says positional struct patterns match fields in declaration order and gives match point: Point(x, y) => plot(x, y) as the example (docs/with-specification.md:3597-3602).

Current state

Pattern sema handles NK_PAT_VARIANT / NK_PAT_ENUM_SHORTHAND by resolving an enum variant. If the subject is not an enum and the pattern has payload bindings, it emits variant payload pattern requires an enum subject (src/SemaCheck.w:6679-6710). Struct patterns with named fields are handled separately as NK_PAT_STRUCT (src/SemaCheck.w:6864-6889), but there is no positional struct-pattern path. The original issue's observed error matches this code path.

The parser currently has no distinct positional-struct pattern node; uppercase Name(...) in pattern position is represented as a variant pattern. The root fix is to disambiguate Name(...) against the subject type during sema and lower it as field-by-index when Name names a struct.

Implementation plan

  1. In Sema.check_pattern, when seeing NK_PAT_VARIANT with a non-enum subject and a qualifier/name that resolves to the subject's struct type, reinterpret it as a positional struct pattern instead of emitting the enum-subject diagnostic.
  2. Validate arity against the struct field count. If the pattern has too few or too many positional fields, emit struct pattern 'Point' expects N field pattern(s), found M.
  3. Bind each payload pattern against the struct field type in declaration order. Reuse pattern_child_subject_type so reference-pattern ergonomics remain consistent.
  4. In MIR lowering, extend lower_pattern_match and lower_pattern so the same NK_PAT_VARIANT/new metadata path lowers to new_field_place(struct_subject_place, field_sym_or_index, field_ty) rather than enum downcast. If adding a new AST node is cleaner, update Resolve.w, SemaCheck.w, and MirLower.w together.
  5. Keep enum variant patterns unchanged; Shape.Circle(r) and .Circle(r) must still resolve through enum variant lookup.

Test plan

Add test/behavior/behav_positional_struct_patterns.w:

  • type Point { x: i32, y: i32 }; match p: Point(x, y) => assert(x + y == ...).
  • nested positional struct pattern inside an enum or tuple.

Add test/compile_errors/err_positional_struct_pattern_arity.w for too few/too many fields.

Add test/compile_errors/err_positional_struct_pattern_wrong_type.w for Point(x, y) against a non-Point subject.

Extend test/spec/spec_ss09_7_pattern_matching.w with the spec example.

Acceptance criteria

  • Point(x, y) binds struct fields in declaration order.
  • Arity and wrong-subject diagnostics are clear.
  • Enum variant patterns are not regressed.
  • with build && with build :fixpoint && with build :test pass.

Dependencies

#518 for broader pattern-form tests.

Traceability

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

Activity

  1. ehartford commented on Jun 14, 2026

    @ehartford
    ContributorAuthor

    Implemented in 8fb5aaa. Name(...) in pattern position now resolves as a positional struct pattern when the subject shape is the named struct, validates declaration-order arity, checks nested field patterns, and lowers field-by-index bindings/tests in MIR. Added behavior coverage for direct and nested positional struct patterns, compile-error coverage for arity and wrong-subject diagnostics, and the §9.7 spec example. 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