-
Notifications
You must be signed in to change notification settings - Fork 861
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
Comments
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 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 |
@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:
Error:
Using:
|
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.
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).
No, it's not. It does not use patterns. |
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. |
This has been fixed by 8eb478a |
Er, it will be fixed in a moment by #690, which isn't quite submitted yet |
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
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
error: expected token: 'case'; generated default instead
example:
The text was updated successfully, but these errors were encountered: