named pattern variable in pattern list

Stephan Herrmann stephan.herrmann at berlin.de
Tue Jan 28 17:49:15 UTC 2025


Our tests reveal a difference between compilers regarding this program:

	public class X {
		record Point (int x, int y) {}
		static void foo(Object o) {
		switch (o) {
			case Integer _, Point(int x, int _), String _  :
			    System.out.println("Integer");
			default:
			    System.out.println("Object");
		}
		}
		public static void main(String [] args) {
			foo("");
		}
	}

We expect an error to be raised against x in the record pattern, because it 
seems to violate this from JLS §14.11.1:

"It is a compile-time error for a case label to have more than one case pattern 
and declare any pattern variables (other than those declared by a guard 
associated with the case label)."

This program is accepted by javac. Am I right to assume that this is a bug in javac?

thanks,
Stephan


More information about the compiler-dev mailing list