Skip to content

Commit

Permalink
[Sealed types] Extra and spurious error messages with faulty type sea…
Browse files Browse the repository at this point in the history
…ling (#3150)

* Fixes #3007
  • Loading branch information
srikanth-sankaran authored Oct 24, 2024
1 parent 983369c commit 05c3001
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12222,16 +12222,12 @@ public void duplicatePermittedType(SourceTypeBinding type, TypeReference referen
}

public void sealedClassNotDirectSuperClassOf(ReferenceBinding type, TypeReference reference, SourceTypeBinding superType) {
this.handle(
IProblem.SealedNotDirectSuperClass,
new String[] {
new String(type.sourceName()),
new String(superType.readableName())},
new String[] {
new String(type.sourceName()),
new String(superType.readableName())},
reference.sourceStart,
reference.sourceEnd);
if ((type.tagBits & TagBits.HierarchyHasProblems) == 0 && (superType.tagBits & TagBits.HierarchyHasProblems) == 0) {
this.handle(IProblem.SealedNotDirectSuperClass,
new String[] { new String(type.sourceName()), new String(superType.readableName()) },
new String[] { new String(type.sourceName()), new String(superType.readableName()) },
reference.sourceStart, reference.sourceEnd);
}
}

public void permittedTypeOutsideOfModule(ReferenceBinding permType, ReferenceBinding sealedType, ASTNode node, ModuleBinding moduleBinding) {
Expand Down Expand Up @@ -12266,16 +12262,12 @@ public void sealedTypeMissingPermits(SourceTypeBinding type, ASTNode node) {
}

public void sealedInterfaceNotDirectSuperInterfaceOf(ReferenceBinding type, TypeReference reference, SourceTypeBinding superType) {
this.handle(
IProblem.SealedNotDirectSuperInterface,
new String[] {
new String(type.sourceName()),
new String(superType.readableName())},
new String[] {
new String(type.sourceName()),
new String(superType.readableName())},
reference.sourceStart,
reference.sourceEnd);
if ((type.tagBits & TagBits.HierarchyHasProblems) == 0 && (superType.tagBits & TagBits.HierarchyHasProblems) == 0) {
this.handle(IProblem.SealedNotDirectSuperInterface,
new String[] { new String(type.sourceName()), new String(superType.readableName()) },
new String[] { new String(type.sourceName()), new String(superType.readableName()) },
reference.sourceStart, reference.sourceEnd);
}
}

public void localTypeMayNotBePermittedType(SourceTypeBinding type, TypeReference superclass, TypeBinding superTypeBinding) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6734,4 +6734,80 @@ void testMaybe(Outer<String>.Maybe<?> maybe) {
"Void methods cannot return a value\n" +
"----------\n");
}

// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3007
// [Sealed types] Extra and spurious error messages with faulty type sealing
public void testIssue3007() {
runNegativeTest(
new String[] {
"X.java",
"""
public sealed class X extends Y<String> permits Y {
int yield () {
return this.yield();
}
}
sealed class Y<T> extends X permits X, Z {
}
final class Z extends Y<String> {}
"""
},
"----------\n" +
"1. ERROR in X.java (at line 1)\n" +
" public sealed class X extends Y<String> permits Y {\n" +
" ^\n" +
"The hierarchy of the type X is inconsistent\n" +
"----------\n" +
"2. ERROR in X.java (at line 7)\n" +
" sealed class Y<T> extends X permits X, Z {\n" +
" ^\n" +
"Cycle detected: a cycle exists in the type hierarchy between Y<T> and X\n" +
"----------\n" +
"3. ERROR in X.java (at line 11)\n" +
" final class Z extends Y<String> {}\n" +
" ^\n" +
"The hierarchy of the type Z is inconsistent\n" +
"----------\n");
}

// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3007
// [Sealed types] Extra and spurious error messages with faulty type sealing
public void testIssue3007_2() {
runNegativeTest(
new String[] {
"X.java",
"""
public class X extends Y<String> {
int yield () {
return this.yield();
}
}
class Y<T> extends X {
}
final class Z extends Y<String> {}
"""
},
"----------\n" +
"1. ERROR in X.java (at line 1)\n" +
" public class X extends Y<String> {\n" +
" ^\n" +
"The hierarchy of the type X is inconsistent\n" +
"----------\n" +
"2. ERROR in X.java (at line 7)\n" +
" class Y<T> extends X {\n" +
" ^\n" +
"Cycle detected: a cycle exists in the type hierarchy between Y<T> and X\n" +
"----------\n" +
"3. ERROR in X.java (at line 11)\n" +
" final class Z extends Y<String> {}\n" +
" ^\n" +
"The hierarchy of the type Z is inconsistent\n" +
"----------\n");
}
}

0 comments on commit 05c3001

Please sign in to comment.