Skip to content

Fix parsing of unnamed patterns#1579

Open
mrjameshamilton wants to merge 2 commits intopalantir:developfrom
mrjameshamilton:mrjameshamilton/fix-unnamed-pattern
Open

Fix parsing of unnamed patterns#1579
mrjameshamilton wants to merge 2 commits intopalantir:developfrom
mrjameshamilton:mrjameshamilton/fix-unnamed-pattern

Conversation

@mrjameshamilton
Copy link

Before this PR

I came across FormatException of the form error: expected token: '_'; generated ) instead when using unnamed patterns, like the following (from the JEP):

class UnnamedPattern {
    public static void main(String[] args) {
        var cp = new ColoredPoint(new Point(1, 2), Color.RED);

        // The colour doesn't matter, so use an unnamed pattern
        // (as opposed to the unnamed type pattern `var _`).
        if (cp instanceof ColoredPoint(Point(int x, int y), _)) {
            System.out.println(x + "," + y);
        }

        if (cp instanceof ColoredPoint(_, var c)) {
            System.out.println(c);
        }
    }
}

record Point(int x, int y) { }
enum Color { RED, GREEN, BLUE }
record ColoredPoint(Point p, Color c) { }

I resolved this by overriding visitAnyPattern(AnyPatternTree node, Void unused) in the existing Java21InputAstVisitor.

However, AnyPatternTree is annotated as a preview feature in Java 21 so I changed Java versions from 21 to 23.

Before changing the version, I tried adding --enable-preview but then I got an error message that --enable-preview is not compatible with Java 11 source.

I also noticed Java 23 mentioned in the root build.gradle so I changed it to 23 rather than 22, 24, 25.

One thing I found in this change was that getImports() now returns a JCImportBase, so to ensure backwards compatibility I added an instanceof check that (and module imports support would need to be implemented in the future).

After this PR

==COMMIT_MSG==
Fix parsing of unnamed patterns
==COMMIT_MSG==

Possible downsides?

I'm not sure if changing Java versions from 21 to 23 (runtime in root build.gradle, target in palantir-java-format-spi/build.gradle and target in palantir-java-format/build.gradle) will have any negative effect? And if another solution should be found to resolve the issue.

@palantirtech
Copy link
Member

Thanks for your interest in palantir/palantir-java-format, @mrjameshamilton! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request.

@changelog-app
Copy link

changelog-app bot commented Feb 15, 2026

Generate changelog in changelog/@unreleased

Type (Select exactly one)

  • Feature (Adding new functionality)
  • Improvement (Improving existing functionality)
  • Fix (Fixing an issue with existing functionality)
  • Break (Creating a new major version by breaking public APIs)
  • Deprecation (Removing functionality in a non-breaking way)
  • Migration (Automatically moving data/functionality to a new system)

Description

Fix parsing of unnamed patterns

Check the box to generate changelog(s)

  • Generate changelog entry

@mrjameshamilton mrjameshamilton force-pushed the mrjameshamilton/fix-unnamed-pattern branch from 28bb365 to d046436 Compare February 15, 2026 17:28
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