syntax of type pattern vs. local variable declaration

Stephan Herrmann stephan.herrmann at berlin.de
Fri Nov 1 21:31:59 UTC 2024


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