Repository navigation
Description
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
- In
Sema.check_pattern, when seeingNK_PAT_VARIANTwith 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. - 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. - Bind each payload pattern against the struct field type in declaration order. Reuse
pattern_child_subject_typeso reference-pattern ergonomics remain consistent. - In MIR lowering, extend
lower_pattern_matchandlower_patternso the sameNK_PAT_VARIANT/new metadata path lowers tonew_field_place(struct_subject_place, field_sym_or_index, field_ty)rather than enum downcast. If adding a new AST node is cleaner, updateResolve.w,SemaCheck.w, andMirLower.wtogether. - 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 :testpass.
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
ehartford commentedon Jun 14, 2026
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.