syntax of type pattern vs. local variable declaration

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Mon Nov 11 11:42:10 UTC 2024


There is some complex disambiguation logic between constant expression 
and patterns when we see "case" in javac parser. The analysis recognizes 
certain tokens and uses them to disambiguate. I wouldn't be surprised if 
"." was missed.

Filed:
https://bugs.openjdk.org/browse/JDK-8343932

Thanks
Maurizio

On 01/11/2024 21:31, Stephan Herrmann wrote:
> We have this test case:
>
> -------------------
> abstract sealed class J<T1, T2> permits X.S, A {}
>
> final class A extends J<Integer, String> {}
>
> public class X<T> {
>
>     final class S<U> extends J<T, U> {}
>
>     int testExhaustive(J<Integer, String> ji) {
>         return switch (ji) {
>             case A a -> 42;
>             case X<Integer>.S<String> e -> 4200;
>         };
>     }
>     public static void main(String[] args) {
>         X<Integer>.S<String> xs = null;
>         System.out.println(new X<Integer>().testExhaustive(new 
> X<Integer>().new S<String>()));
>     }
> }
> -------------------
>
> In comparing with javac we noticed that javac reports this error:
>
> X.java:12: error: illegal start of type
>      case X<Integer>.S<String> e -> 4200;
>                                ^
>
> The grammar states that the syntax for TypePattern is the same as 
> LocalVariableDeclaration. Now see that in main() a local variable with 
> the same shape is accepted by javac, so it should also accept the type 
> pattern, right?
>
> regards,
> Stephan


More information about the compiler-dev mailing list