Error messages technically true but that does not help if several cases have the same erasure

Remi Forax forax at univ-mlv.fr
Wed Jul 7 17:42:32 UTC 2021


When compiling this code:

 Object o = "";
 switch (o) {
   case List l -> System.out.println("list2");
   case List<?> l -> System.out.println("list1");

   default -> System.out.println("default");
 }
I get
PatternMatchingWrongDominated.java:8: error: this case label is dominated by a preceding case label
      case List<?> l -> System.out.println("list1");
           ^

instead of an error saying that two cases have the same erasure.
The funny thing, is that permuting the two first cases give the same result.

The same is true for 
  List<?> list =  null;
  switch (list) {
    case List l -> System.out.println("list2");
    case List<?> l -> System.out.println("list1");
  }

which reports
PatternMatchingWrongDominated.java:18: error: duplicate total pattern
      case List<?> l -> System.out.println("list1");
           ^


I think javac should have a specific error if two cases have the same erasure.

regards,
Rémi


More information about the compiler-dev mailing list