Repository navigation
Java 17 preview switch with exception throw #683
Description
Activity
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:previewdoes 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
@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 moreUsing:
- spotless 6.0.0
- GJF 1.12.0
- Gradle 7.2
- Java 17.0.1-tem
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.
sis a pattern in my example, andcis a pattern in your example. You can verify that by just executingjavac EnumBug.java(without--enable-preview).Reacted by diegomrsantosThanks 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.
- added 2 commits that reference this issue
on Nov 19, 2021 This has been fixed by 8eb478a
Er, it will be fixed in a moment by #690, which isn't quite submitted yet
- added 2 commits that reference this issue
on Nov 19, 2021 - added 2 commits that reference this issue
on Jan 7, 2022
error: expected token: 'case'; generated default instead
example: