Skip to content

Commit

Permalink
[Sealed types] Duplicate diagnostics for illegal modifier combination (
Browse files Browse the repository at this point in the history
…#3101)

* Fixes #3100
  • Loading branch information
srikanth-sankaran authored Oct 17, 2024
1 parent 5d6d82e commit 0b34789
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2538,7 +2538,9 @@ public interface IProblem {
int SealedPermittedTypeOutsideOfPackage = TypeRelated + 1859;
/** @since 3.28 */
int SealedSealedTypeMissingPermits = TypeRelated + 1860;
/** @since 3.28 */
/** @since 3.28
* @deprecated problem no longer generated
*/
int SealedInterfaceIsSealedAndNonSealed = TypeRelated + 1861;
/** @since 3.28 */
int SealedDisAllowedNonSealedModifierInInterface = TypeRelated + 1862;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1119,17 +1119,9 @@ private Map.Entry<TypeReference, ReferenceBinding> getFirstSealedSuperTypeOrInte
}
return null;
}
// TODO: Optimize the multiple loops - defer until the feature becomes standard.

private void checkPermitsInType() {
// if (/* this.isRecordDeclaration || */this.isEnum())
// return; // handled separately
TypeDeclaration typeDecl = this.scope.referenceContext;
if (this.isInterface()) {
if (isSealed() && isNonSealed()) {
this.scope.problemReporter().sealedInterfaceIsSealedAndNonSealed(this, typeDecl);
return;
}
}
boolean hasPermittedTypes = this.permittedTypes != null && this.permittedTypes.length > 0;
if (hasPermittedTypes) {
if (!this.isSealed())
Expand Down Expand Up @@ -1170,11 +1162,13 @@ private void checkPermitsInType() {
return;
}
} else if (this.isNonSealed()) {
if (!foundSealedSuperTypeOrInterface) {
if (this.isClass() && !this.isRecord()) // record to give only illegal modifier error.
this.scope.problemReporter().sealedDisAllowedNonSealedModifierInClass(this, typeDecl);
else if (this.isInterface())
this.scope.problemReporter().sealedDisAllowedNonSealedModifierInInterface(this, typeDecl);
if (!this.isSealed()) { // lest we bark again.
if (!foundSealedSuperTypeOrInterface) {
if (this.isClass() && !this.isRecord()) // record to give only illegal modifier error.
this.scope.problemReporter().sealedDisAllowedNonSealedModifierInClass(this, typeDecl);
else if (this.isInterface())
this.scope.problemReporter().sealedDisAllowedNonSealedModifierInInterface(this, typeDecl);
}
}
}
if (foundSealedSuperTypeOrInterface) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12397,15 +12397,6 @@ public void sealedSealedTypeMissingPermits(SourceTypeBinding type, ASTNode node)
node.sourceEnd);
}

public void sealedInterfaceIsSealedAndNonSealed(SourceTypeBinding type, ASTNode node) {
String name = new String(type.sourceName());
this.handle(IProblem.SealedInterfaceIsSealedAndNonSealed,
new String[] { name },
new String[] { name },
node.sourceStart,
node.sourceEnd);
}

public void sealedDisAllowedNonSealedModifierInInterface(SourceTypeBinding type, TypeDeclaration typeDecl) {
String name = new String(type.sourceName());
this.handle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1071,11 +1071,6 @@ public void testBug563806_032() {
" public sealed non-sealed interface X {\n" +
" ^\n" +
"Sealed class or interface lacks the permits clause and no class or interface from the same compilation unit declares X as its direct superclass or superinterface\n" +
"----------\n" +
"3. ERROR in p1\\X.java (at line 2)\n" +
" public sealed non-sealed interface X {\n" +
" ^\n" +
"An interface X is declared both sealed and non-sealed\n" +
"----------\n");
}
public void testBug563806_033() {
Expand Down Expand Up @@ -6400,4 +6395,23 @@ final non-sealed class Y extends X {}
"The type Y may have only one modifier out of sealed, non-sealed, and final\n" +
"----------\n");
}

// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3100
// [Sealed types] Duplicate diagnostics for illegal modifier combination
public void testIssue3100() {
runNegativeTest(
new String[] {
"X.java",
"""
public sealed non-sealed interface X {}
final class Y implements X {}
"""
},
"----------\n" +
"1. ERROR in X.java (at line 1)\n" +
" public sealed non-sealed interface X {}\n" +
" ^\n" +
"The type X may have only one modifier out of sealed, non-sealed, and final\n" +
"----------\n");
}
}

0 comments on commit 0b34789

Please sign in to comment.