Skip to content

fix: allow a final modifier on a switch pattern's type pattern#230

Open
DragonAxe wants to merge 1 commit into
tree-sitter:masterfrom
DragonAxe:final-in-switch-patterns
Open

fix: allow a final modifier on a switch pattern's type pattern#230
DragonAxe wants to merge 1 commit into
tree-sitter:masterfrom
DragonAxe:final-in-switch-patterns

Conversation

@DragonAxe

Copy link
Copy Markdown

instanceof_expression already accepts optional('final') before its pattern's type, matching JLS support for final on a pattern variable in both instanceof patterns and switch patterns (JEP 394/405, JEP 441/440). type_pattern -- the equivalent rule used inside a switch's case labels -- never got the same treatment, so case final Type x -> produced an ERROR node: the parser had no rule for a leading final keyword there, so it consumed final itself as if it were the pattern's type name and the real type as the binding identifier, leaving the actual variable name as a stray ERROR token.

case Type x -> (no modifier) and instanceof final Type x both already parsed correctly; this just brings case final Type x -> in line with them. All existing grammar tests still pass.

Fixes #229

instanceof_expression already accepts optional('final') before its
pattern's type, matching JLS support for `final` on a pattern variable in
both instanceof patterns and switch patterns (JEP 394/405, JEP 441/440).
type_pattern -- the equivalent rule used inside a switch's `case` labels
-- never got the same treatment, so `case final Type x ->` produced an
ERROR node: the parser had no rule for a leading `final` keyword there,
so it consumed `final` itself as if it were the pattern's type name and
the real type as the binding identifier, leaving the actual variable name
as a stray ERROR token.

`case Type x ->` (no modifier) and `instanceof final Type x` both already
parsed correctly; this just brings `case final Type x ->` in line with
them. All existing grammar tests still pass.
@DragonAxe

Copy link
Copy Markdown
Author

Disclaimer: This fix was found and documented by Claude while I was working on an auto-formatter for java. I apologize for any inconvenience this might cause. I, the author, am willing to make any adjustments or clarifications if needed.

@wetneb

wetneb commented Jul 15, 2026

Copy link
Copy Markdown

Hi @DragonAxe,
Since external pull requests are rarely integrated in this repository, we have formed a small group of contributors and users to maintain a fork of this grammar. I looked into integrating your change to our fork, but it turns out that this issue is already solved there.

If you encounter any issue switching to our fork as a user or contributor, I would be happy to hear what we could improve to enable that. Since you seem to be using this grammar via the Rust crate, you should hopefully be able to just switch to https://crates.io/crates/tree-sitter-java-orchard.

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.

type_pattern doesn't accept a final modifier in switch case labels, even though instanceof_expression does

2 participants