Repository navigation
fix(grammar): allow a qualified type name in a record pattern - #231
akd-devvine wants to merge 1 commit into
Conversation
`record_pattern` only accepted `identifier`, `_reserved_identifier` and
`generic_type`, so a record pattern that names its record through an
enclosing type or package fails to parse:
case Outer.Created(String id) -> ...
if (o instanceof Outer.Created(String id)) { }
Because the prefix syntax is identical to a method call, the parser
commits to `method_invocation` and emits an ERROR inside its
`argument_list` rather than a `record_pattern`. The unqualified form
parses correctly, which makes the failure easy to miss.
JLS 21 §14.30.1 defines a record pattern's type as a ReferenceType, which
admits a qualified name, so this is valid Java that the grammar rejects.
Adding `scoped_type_identifier` to the existing choice fixes it. No new
conflicts are introduced -- the `[$.argument_list, $.record_pattern_body]`
and `scoped_type_identifier` conflict entries already cover the ambiguity
-- and the full corpus passes with a new test covering both the
`instanceof` and `switch` forms.
Found while parsing a Java 21 sealed-interface hierarchy whose switch
arms name nested records through their enclosing type.
|
Hi @akd-devvine, Thanks for working on this. This repository isn't actively maintained, so we have been maintaining a fork which has already integrated many improvements, including the one you are proposing here. If you encounter any issue switching to our fork, I would be happy to know what needs improving on our side. |
|
We hit this in real Java 25 code (record patterns have been standard since Java 21): |
record_patternonly acceptedidentifier,_reserved_identifierandgeneric_type, so a record pattern that names its record through an enclosing type or package fails to parse:Because the prefix syntax is identical to a method call, the parser commits to
method_invocationand emits an ERROR inside itsargument_listrather than arecord_pattern. The unqualified form parses correctly, which makes the failure easy to miss.JLS 21 §14.30.1 defines a record pattern's type as a ReferenceType, which admits a qualified name, so this is valid Java that the grammar rejects.
Adding
scoped_type_identifierto the existing choice fixes it. No new conflicts are introduced -- the[$.argument_list, $.record_pattern_body]andscoped_type_identifierconflict entries already cover the ambiguity -- and the full corpus passes with a new test covering both theinstanceofandswitchforms.Found while parsing a Java 21 sealed-interface hierarchy whose switch arms name nested records through their enclosing type.