Skip to content

Commit

Permalink
[switch][sealed types] ECJ fails to signal a completely dominated case
Browse files Browse the repository at this point in the history
arm

* Fixes eclipse-jdt#3035
  • Loading branch information
srikanth-sankaran committed Oct 14, 2024
1 parent 11f45e6 commit 549d673
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,11 @@ public boolean dominates(Pattern p) {
return false;
if (p.resolvedType == null || this.resolvedType == null)
return false;
return p.resolvedType.erasure().isSubtypeOf(this.resolvedType.erasure(), false);

if (p.resolvedType.isSubtypeOf(this.resolvedType, false))
return true;

return p.resolvedType.erasure().findSuperTypeOriginatingFrom(this.resolvedType.erasure()) != null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8864,4 +8864,48 @@ final class A<T> implements I<X> {
"Type I<X> cannot be safely cast to B<X>\n" +
"----------\n");
}

// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3035
// [switch][sealed types] ECJ fails to signal a completely dominated case arm
public void testIssue3035() {
runNegativeTest(
new String[] {
"X.java",
"""
abstract sealed class J<T1, T2> permits X.S, A {
}
final class A extends J<Integer, String> {
}
public class X {
sealed class S<T, U> extends J<T, U> permits SS {
}
final class SS<T, U> extends S<U, T> {}
int testExhaustive(J<Integer, String> ji) {
return switch (ji) { // Exhaustive!
case A a -> 42;
case S<Integer, String> e -> 4200;
case SS<String, Integer> e -> 420;
};
}
public static void main(String[] args) {
S<Integer, String> xs = null;
System.out.println(new X().testExhaustive(new X().new S<Integer, String>()));
J<Integer, String> ji = new X().new SS<String, Integer>();
}
}
"""
},
"----------\n" +
"1. ERROR in X.java (at line 18)\n" +
" case SS<String, Integer> e -> 420;\n" +
" ^^^^^^^^^^^^^^^^^^^^^\n" +
"This case label is dominated by one of the preceding case labels\n" +
"----------\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public static Class[] getAllTestClasses() {
NullAnnotationTests21.class,
ComplianceDiagnoseTest.class,
InstanceofExpressionTest.class,
PrimitiveInPatternsTest.class,
PrimitiveInPatternsTestSH.class,
};
}

Expand Down

0 comments on commit 549d673

Please sign in to comment.