Skip to content

fix(grammar): allow a qualified type name in a record pattern - #231

Open
akd-devvine wants to merge 1 commit into
tree-sitter:masterfrom
akd-devvine:fix/qualified-record-patterns
Open

fix(grammar): allow a qualified type name in a record pattern#231
akd-devvine wants to merge 1 commit into
tree-sitter:masterfrom
akd-devvine:fix/qualified-record-patterns

Conversation

@akd-devvine

Copy link
Copy Markdown

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.

`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.
@wetneb

wetneb commented Aug 2, 2026

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants