eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
156 stars 123 forks source link

[Switch expression + Sealed Types] Suspect diagnostic about switch expression being inexhaustive #2719

Open srikanth-sankaran opened 1 month ago

srikanth-sankaran commented 1 month ago

This program compiles with javac, but ECJ complains about A switch expression should have a default case

public interface X {

  static <T extends Object & AbstractSealedInterface> Integer get(T object) {
    return switch (object) {
      case ClassC ignored -> 42;
    };
  }

  public abstract sealed interface AbstractSealedInterface permits InterfaceB {
  }

  public sealed interface InterfaceB extends AbstractSealedInterface permits ClassC {
  }

  final class ClassC implements InterfaceB {}

  public static void main(String[] args) {
   System.out.println(get(new ClassC()));
  }
}

ECJ behavior is suspect - this is a follow up from https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2714

nlisker commented 1 month ago

I've just ran into what seems to be the same problem:

sealed interface I {

    enum E implements I {
        A, B, C;
    }
} 

class Test {

    void d(I i) {
        switch (i) { // error: An enhanced switch statement should be exhaustive; a default label expected
            case I.E.A -> {}
            case I.E.B -> {}
            case I.E.C -> {}
        }
    }
}

But there are no other valid cases (I must be an E, which must be A, B or C).

Didn't test with javac.

Version: 2024-06 (4.32) Build id: I20240601-0610

srikanth-sankaran commented 1 month ago

I've just ran into what seems to be the same problem:

No, this is a different problem with the same symptom - I have raised https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2720 with the test case.