Skip to content

Java 17 preview switch with exception throw #683

Description

@wangxufire

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);
    }
  }

Activity

  1. lorenzleutgeb commented on Nov 17, 2021

    @lorenzleutgeb

    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
  2. diegomrsantos commented on Nov 17, 2021

    @diegomrsantos

    @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
  3. lorenzleutgeb commented on Nov 17, 2021

    @lorenzleutgeb

    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).

  4. diegomrsantos commented on Nov 17, 2021

    @diegomrsantos

    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.

  5. cushon commented on Nov 19, 2021

    @cushon
    Collaborator

    This has been fixed by 8eb478a

  6. cushon commented on Nov 19, 2021

    @cushon
    Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions