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

Java 17 preview switch with exception throw #683

Closed
wangxufire opened this issue Nov 4, 2021 · 6 comments
Closed

Java 17 preview switch with exception throw #683

wangxufire opened this issue Nov 4, 2021 · 6 comments

Comments

@wangxufire
Copy link

error: expected token: 'case'; generated default instead

example:

  interface Test {}

  static class Test1 implements Test{}
  static class Test2 implements Test{}

  public static void main(String[] args) {
    Test test = new Test1();
    switch (test) {
      case Test1 test1 -> {}
      case Test2 test2 -> {}
      default -> throw new IllegalStateException("Unexpected value: " + test);
    }
  }
@lorenzleutgeb
Copy link

lorenzleutgeb commented Nov 17, 2021

Looks like the fix could be similar to #492 which was fixed by @cushon.

An even smaller reproduction (on 1.12.0):

class Preview {
    public static String f(Object o) {
        return switch (o) {
            case String s -> s;
            default -> throw new RuntimeException();
        };
    }
}
> javac --enable-preview -source 17 -Xlint:preview Preview.java
Preview.java:4: warning: [preview] patterns in switch statements are a preview feature and may be removed in a future release.
                        case String s -> s;
                             ^
Preview.java:3: warning: [preview] patterns in switch statements are a preview feature and may be removed in a future release.
                return switch (o) {
                              ^
3 warnings
> test -f Preview.class ; echo $?
0
> java --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED    \
>      --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED   \
>      --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
>      --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED   \
>      --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED   \
>      -jar google-java-format-1.12.0-all-deps.jar \
>      Preview.java
Preview.java:4:26: error: expected token: 'case'; generated default instead

(Note that presence/absence of -Xlint:preview does not matter.)

In case this is because GJF does not support preview features, then this fact should be documented.

Additional Information
>  uname -sorv
Linux 5.10.75 #1-NixOS SMP Wed Oct 20 09:45:06 UTC 2021 GNU/Linux
> nixos-version
21.11.20211021.34ad3ff (Porcupine)
> javac -version
javac 17.0.1

@diegomrsantos
Copy link

@lorenzleutgeb could you please clarify how this issue is different from #492? You said GJF does not support preview features, but isn't #492 already using a preview feature?

The code bellow also fails:

sealed interface MyInterface {}
final class Child implements MyInterface {}

public class A {
    public static MyInterface test(MyInterface o) {
        return switch (o) {
            case Child c -> c;
        };
    }
}

Error:

Caused by: com.google.googlejavaformat.java.FormatterException: 9:14: error: expected token: 'case'; generated default instead
        at com.google.googlejavaformat.java.Formatter.getFormatReplacements(Formatter.java:293)
        at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:267)
        at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:233)
        ... 142 more

Using:

  • spotless 6.0.0
  • GJF 1.12.0
  • Gradle 7.2
  • Java 17.0.1-tem

@lorenzleutgeb
Copy link

lorenzleutgeb commented Nov 17, 2021

Right, so this also fails:

class Preview {
    public static String f(Object o) {
        return switch (o) {
            case String s -> s;
        };
    }
}

I thought that it has something to do with throwing an exception, so I missed that the reproduction could be even smaller. Sorry. It appears to really be about patterns.

You said GJF does not support preview features, [...]

That's a misunderstanding. And it's my fault. What I meant to say was that if it is the case that GJF does not support preview features then I think this should be documented, because I did not realize it before (and I am a user and would like to be informed about such constraints).

[...], but isn't #492 already using a preview feature?

No, it's not. It does not use patterns. s is a pattern in my example, and c is a pattern in your example. You can verify that by just executing javac EnumBug.java (without --enable-preview).

@diegomrsantos
Copy link

diegomrsantos commented Nov 17, 2021

Thanks for the quick and comprehensive answer. I get it now.

You are right, seems that #492 was about switch expressions (which was not a preview feature) and this issue is about pattern matching with a switch expression as you said.

Agree on being more explicit about preview features, I'm still not sure if it is not supported or current issue is a bug.

copybara-service bot pushed a commit that referenced this issue Nov 19, 2021
for AST changes in Java 17, in particular the addition of CaseTree#getLabels,
which returns a single `DefaultCaseLabelTree` to represent the default case.

#683
#684

PiperOrigin-RevId: 411074295
copybara-service bot pushed a commit that referenced this issue Nov 19, 2021
for AST changes in Java 17, in particular the addition of CaseTree#getLabels,
which returns a single `DefaultCaseLabelTree` to represent the default case.

#683
#684

PiperOrigin-RevId: 411074295
@cushon
Copy link
Collaborator

cushon commented Nov 19, 2021

This has been fixed by 8eb478a

@cushon cushon closed this as completed Nov 19, 2021
@cushon
Copy link
Collaborator

cushon commented Nov 19, 2021

Er, it will be fixed in a moment by #690, which isn't quite submitted yet

copybara-service bot pushed a commit that referenced this issue Nov 19, 2021
for AST changes in Java 17, in particular the addition of CaseTree#getLabels,
which returns a single `DefaultCaseLabelTree` to represent the default case.

#683
#684

PiperOrigin-RevId: 411074295
copybara-service bot pushed a commit that referenced this issue Nov 19, 2021
for AST changes in Java 17, in particular the addition of CaseTree#getLabels,
which returns a single `DefaultCaseLabelTree` to represent the default case.

#683
#684

PiperOrigin-RevId: 411146137
fawind pushed a commit to palantir/palantir-java-format that referenced this issue Jan 7, 2022
for AST changes in Java 17, in particular the addition of CaseTree#getLabels,
which returns a single `DefaultCaseLabelTree` to represent the default case.

google/google-java-format#683
google/google-java-format#684

PiperOrigin-RevId: 411146137
fawind pushed a commit to palantir/palantir-java-format that referenced this issue Jan 10, 2022
for AST changes in Java 17, in particular the addition of CaseTree#getLabels,
which returns a single `DefaultCaseLabelTree` to represent the default case.

google/google-java-format#683
google/google-java-format#684

PiperOrigin-RevId: 411146137
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

No branches or pull requests

4 participants