diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java index b88117af3b8..d1ac75e2e53 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java @@ -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) { @@ -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) { diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SealedTypesTests.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SealedTypesTests.java index b8e84a44987..79e82c2f64e 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SealedTypesTests.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SealedTypesTests.java @@ -6734,4 +6734,80 @@ void testMaybe(Outer.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 permits Y { + int yield () { + return this.yield(); + } + } + + sealed class Y extends X permits X, Z { + + } + + final class Z extends Y {} + """ + }, + "----------\n" + + "1. ERROR in X.java (at line 1)\n" + + " public sealed class X extends Y 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 extends X permits X, Z {\n" + + " ^\n" + + "Cycle detected: a cycle exists in the type hierarchy between Y and X\n" + + "----------\n" + + "3. ERROR in X.java (at line 11)\n" + + " final class Z extends Y {}\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 { + int yield () { + return this.yield(); + } + } + + class Y extends X { + + } + + final class Z extends Y {} + """ + }, + "----------\n" + + "1. ERROR in X.java (at line 1)\n" + + " public class X extends Y {\n" + + " ^\n" + + "The hierarchy of the type X is inconsistent\n" + + "----------\n" + + "2. ERROR in X.java (at line 7)\n" + + " class Y extends X {\n" + + " ^\n" + + "Cycle detected: a cycle exists in the type hierarchy between Y and X\n" + + "----------\n" + + "3. ERROR in X.java (at line 11)\n" + + " final class Z extends Y {}\n" + + " ^\n" + + "The hierarchy of the type Z is inconsistent\n" + + "----------\n"); + } }