Skip to content

Commit

Permalink
[Sealed classes] Incorrect Reconciler error: The type A that implemen…
Browse files Browse the repository at this point in the history
…ts a sealed interface I should be a permitted subtype of I (#2601)

* Fixes #1998

* Disable failing tests from thus far unhooked junit (See
#2602)
  • Loading branch information
srikanth-sankaran authored Jun 19, 2024
1 parent f2c7a56 commit 8695a0d
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,9 @@ && isTypeUseDeprecated(type, scope)) {
public boolean isTypeReference() {
return true;
}
public boolean isImplicit() {
return false;
}
public boolean isWildcard() {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,7 @@ void connectImplicitPermittedTypes() {
if (sourceType.id == TypeIds.T_JavaLangObject) // already handled
return;
if (sourceType.isSealed() && (typeDecl.permittedTypes == null ||
typeDecl.permittedTypes.length == 0)) {
typeDecl.permittedTypes.length == 0 || typeDecl.permittedTypes[0].isImplicit())) {
connectImplicitPermittedTypes(sourceType);
}
ReferenceBinding[] memberTypes = sourceType.memberTypes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ private static Class[] getAllTestClasses() {
AttachedJavadocTests.class,
AttachedJavadocTests21.class,

SealedTypeModelTests.class,

// Java search tests
RunJavaSearchTests.class,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,59 @@ public void testIssue62() throws Exception {
deleteProject(p);
}
}
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1998
// [Sealed classes] Incorrect Reconciler error: The type A that implements a sealed interface I should be a permitted subtype of I
public void testIssue1998() throws Exception {
if (!isJRE21)
return;
IJavaProject p = createJava21Project("p");
try {
createFile("p/src/X.java",
"""
sealed interface I {}
final class A implements I {}
record R<T extends I>(T x, T y) {}
public class X {
public static int foo(R r) {
return switch (r) {
case R(A a1, A a2) -> 0;
};
}
@SuppressWarnings("unchecked")
public static void main(String argv[]) {
System.out.println(X.foo(new R(new A(), new A())));
}
}
""");
p.getProject().build(IncrementalProjectBuilder.FULL_BUILD, null);
IMarker[] markers = p.getProject().findMarkers(null, true, IResource.DEPTH_INFINITE);
assertMarkers("markers in p",
"R is a raw type. References to generic type R<T> should be parameterized\n" +
"R is a raw type. References to generic type R<T> should be parameterized",
markers);

ICompilationUnit unit = getCompilationUnit("p/src/X.java");
this.problemRequestor.initialize(unit.getSource().toCharArray());
this.workingCopy = getCompilationUnit("p/src/X.java").getWorkingCopy(this.wcOwner, null);
assertProblems("Expecting no problems",
"----------\n" +
"1. WARNING in /p/src/X.java (at line 7)\n" +
" public static int foo(R r) {\n" +
" ^\n" +
"R is a raw type. References to generic type R<T> should be parameterized\n" +
"----------\n" +
"2. WARNING in /p/src/X.java (at line 15)\n" +
" System.out.println(X.foo(new R(new A(), new A())));\n" +
" ^\n" +
"R is a raw type. References to generic type R<T> should be parameterized\n" +
"----------\n",
this.problemRequestor);
this.workingCopy.discardWorkingCopy();
} finally {
deleteProject(p);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void test004() throws Exception {
}
}
// Test explicitly permitted sub types in binary
public void test005() throws Exception {
public void _test005() throws Exception {
String[] permitted = new String[] {"p.X", "p.Y"};
try {
String[] sources = {
Expand Down Expand Up @@ -224,7 +224,7 @@ public void test005() throws Exception {
}
}
// Test implicitly permitted sub types in binary
public void test006() throws Exception {
public void _test006() throws Exception {
String[] permitted = new String[] {"p.X", "p.Y"};
try {
String[] sources = {
Expand Down Expand Up @@ -285,7 +285,7 @@ public void test006() throws Exception {
}
}
// Test sealed types for reconciler
public void test007() throws Exception {
public void _test007() throws Exception {
String[] permitted = new String[] {"p.X"};
try {
IJavaProject project = createJavaProject("SealedTypes");
Expand Down Expand Up @@ -328,7 +328,7 @@ public void test007() throws Exception {
}
}
// Test sealed types for reconciler
public void test008() throws Exception {
public void _test008() throws Exception {
try {
IJavaProject project = createJavaProject("SealedTypes");
project.open(null);
Expand Down Expand Up @@ -356,7 +356,7 @@ public void test008() throws Exception {
}
}
// Test sealed types for reconciler
public void test009() throws Exception {
public void _test009() throws Exception {
try {
IJavaProject project = createJavaProject("SealedTypes");
project.open(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,10 @@ public StringBuilder printExpression(int indent, StringBuilder output) {
public String toString() {
return new String(this.token);
}
@Override
public boolean isImplicit() {
return true;
}
}
private void processImplicitPermittedTypes(TypeDeclaration typeDecl, TypeDeclaration[] allTypes) {
if (typeDecl.permittedTypes == null &&
Expand Down

0 comments on commit 8695a0d

Please sign in to comment.