Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser error for Java 21 switch expression #4153

Open
Bananeweizen opened this issue Apr 27, 2024 · 1 comment
Open

Parser error for Java 21 switch expression #4153

Bananeweizen opened this issue Apr 27, 2024 · 1 comment
Labels
bug Something isn't working parser-java

Comments

@Bananeweizen
Copy link
Contributor

What is the smallest, simplest way to reproduce the problem?

    @Test
    void switchCase() {
        rewriteRun(
          java(
            """
              public class A {
                Object reproduce() {
                    Object object = new Object();
                    return switch (object) {
                        case Integer i -> "1";
                        default -> null;
                    };
                }
              }
               """
          )
        );
    }

What did you expect to see?

No changes.

What did you see instead?

The Java formatter wrongly adds "default" to the case statement label.

JavaParserTest > switchCase() FAILED
    org.opentest4j.AssertionFailedError: [When parsing and printing the source code back to text without modifications, the printed source didn't match the original source code. This means there is a bug in the parser implementation itself. Please open an issue to report this, providing a sample of the code that generated this error for "A.java":
    diff --git a/A.java b/A.java
    index 7a1a443..2ab6912 100644
    --- a/A.java
    +++ b/A.java
    @@ -2,7 +2,7 @@
       Object reproduce() {
           Object object = new Object();
           return switch (object) {
    -          case Integer i -> "1";
    +          defaultcase Integer i -> "1";
               default -> null;
           };
       }
    ]
    expected: "public class A {
      Object reproduce() {
          Object object = new Object();
          return switch (object) {
              case Integer i -> "1";
              default -> null;
          };
      }
    }"
     but was: "public class A {
      Object reproduce() {
          Object object = new Object();
          return switch (object) {
              defaultcase Integer i -> "1";
              default -> null;
          };
      }
    }"

Are you interested in contributing a fix to OpenRewrite?

Maybe. Right now I have trouble getting IDEA to compile rewrite, so I can't really debug...

@Bananeweizen Bananeweizen added the bug Something isn't working label Apr 27, 2024
@timtebeek
Copy link
Contributor

Thanks for pointing this out! Indeed our Java 21 parser does not yet support all feature of Java 21, regrettably. Definitely something to improve as Java 21 becomes more mainstream. The test is already helpful, and thanks for the offer to help explore what changes are needed.

Not sure if you'd already seen that we have some detailed set up instructions specifically for openrewrite/rewrite, which might help you here to limit to just the modules you're interested in. Beyond that Slack is perhaps the best source of info for questions around expanding the parser, such that we can loop in folks with more knowledge in a more synchronous fashion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parser-java
Projects
Status: Backlog
Development

No branches or pull requests

2 participants