Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
srikanth-sankaran opened this issue Jul 17, 2024 · 3 comments · Fixed by #3041
Assignees
Milestone

Comments

@srikanth-sankaran
Copy link
Contributor

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 #2714

@srikanth-sankaran srikanth-sankaran self-assigned this Jul 17, 2024
@nlisker
Copy link

nlisker commented Jul 17, 2024

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
Copy link
Contributor Author

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 #2720 with the test case.

@srikanth-sankaran srikanth-sankaran added this to the 4.34 M2 milestone Oct 1, 2024
@srikanth-sankaran
Copy link
Contributor Author

srikanth-sankaran commented Oct 2, 2024

More useful test case with some twists that is faultily rejected by ECJ, but accepted by javac:

public interface X {

  static <T extends Object & I1 & I2> Integer get(T object) {
    return switch (object) {
      case AB ignored -> 42;
      case BA ignored -> 420;
    };
  }

  public abstract sealed interface I1 permits A, AB, BA {
  }
  
  public abstract sealed interface I2 permits B, AB, BA {
  }

  
  final class A implements I1 {}
  final class B implements I2 {}
  final class AB implements I1, I2 {}
  final class BA implements I1, I2 {}

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants