Skip to content

Commit

Permalink
Additional fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanth-sankaran committed Oct 21, 2024
1 parent c4183b6 commit 99e5404
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1117,16 +1117,20 @@ private boolean isAnUnpermittedSubtypeOf(ReferenceBinding superType) {
}

private void complainIfUnpermittedSubtyping() {
// Diagnose unauthorized subtyping: This cannot be correctly hoisted into ClassScope.{ connectSuperclass() | connectSuperInterfaces() | connectPermittedTypes() } but can be taken up now

// Diagnose unauthorized subtyping: This cannot be correctly hoisted into ClassScope.{ connectSuperclass() | connectSuperInterfaces() | connectPermittedTypes() }
// but can be taken up now

TypeDeclaration typeDecl = this.scope.referenceContext;
if (this.isAnUnpermittedSubtypeOf(this.superclass)) {
reportSealedSuperTypeDoesNotPermitProblem(typeDecl.superclass, this.superclass);
this.scope.problemReporter().sealedSupertypeDoesNotPermit(this, typeDecl.superclass, this.superclass);
}

for (int i = 0, l = this.superInterfaces.length; i < l; ++i) {
ReferenceBinding superInterface = this.superInterfaces[i];
if (this.isAnUnpermittedSubtypeOf(superInterface)) {
TypeReference superInterfaceRef = typeDecl.superInterfaces[i];
reportSealedSuperTypeDoesNotPermitProblem(superInterfaceRef, superInterface);
this.scope.problemReporter().sealedSupertypeDoesNotPermit(this, superInterfaceRef, superInterface);
}
}

Expand All @@ -1136,51 +1140,6 @@ private void complainIfUnpermittedSubtyping() {
return;
}

private void reportSealedSuperTypeDoesNotPermitProblem(TypeReference superTypeRef, TypeBinding superType) {
ModuleBinding sourceModuleBinding = this.module();
boolean isUnnamedModule = sourceModuleBinding.isUnnamed();
boolean isClass = false;
if (superType.isClass()) {
isClass = true;
}
boolean sealedSuperTypeDoesNotPermit = false;
ReferenceBinding superReferenceBinding = null;
if (superType instanceof ReferenceBinding) {
superReferenceBinding = (ReferenceBinding) superType;
if (isUnnamedModule) {
PackageBinding superTypePackage = superReferenceBinding.getPackage();
PackageBinding pkg = this.getPackage();
sealedSuperTypeDoesNotPermit = pkg!= null && pkg.equals(superTypePackage);
} else {
ModuleBinding superTypeModule = superReferenceBinding.module();
ModuleBinding mod = this.module();
sealedSuperTypeDoesNotPermit = mod!= null && mod.equals(superTypeModule);
}
}
if (sealedSuperTypeDoesNotPermit) {
if (isClass) {
this.scope.problemReporter().sealedSuperClassDoesNotPermit(this, superTypeRef, superType);
} else {
this.scope.problemReporter().sealedSuperInterfaceDoesNotPermit(this, superTypeRef, superType);
}
} else {
if (superReferenceBinding instanceof SourceTypeBinding && isUnnamedModule) {
PackageBinding superTypePackage = superReferenceBinding.getPackage();
if (isClass) {
this.scope.problemReporter().sealedSuperClassInDifferentPackage(this, superTypeRef, superType, superTypePackage);
} else {
this.scope.problemReporter().sealedSuperInterfaceInDifferentPackage(this, superTypeRef, superType, superTypePackage);
}
} else {
if (isClass) {
this.scope.problemReporter().sealedSuperClassDisallowed(this, superTypeRef, superType);
} else {
this.scope.problemReporter().sealedSuperInterfaceDisallowed(this, superTypeRef, superType);
}
}
}
}

@Override
public RecordComponentBinding[] components() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12254,75 +12254,27 @@ public void disallowedNonSealedModifier(SourceTypeBinding type, TypeDeclaration
typeDecl.sourceEnd);
}

private void sealedSuperTypeDoesNotPermit(int problem, SourceTypeBinding type, TypeReference superType, TypeBinding superTypeBinding) {
public void sealedSupertypeDoesNotPermit(SourceTypeBinding type, TypeReference superType, TypeBinding superTypeBinding) {
String name = new String(type.sourceName());
String superTypeFullName = new String(superTypeBinding.readableName());
String superTypeShortName = new String(superTypeBinding.shortReadableName());
if (superTypeShortName.equals(name)) superTypeShortName = superTypeFullName;
this.handle(
problem,
new String[] {name, superTypeFullName},
new String[] {name, superTypeShortName},
superType.sourceStart,
superType.sourceEnd);
}

public void sealedSuperTypeInDifferentPackage(int problem, SourceTypeBinding type, TypeReference curType, TypeBinding superTypeBinding, PackageBinding superPackageBinding) {
String typeName = new String(type.sourceName);
String name = new String(superTypeBinding.sourceName());
String packageName = superPackageBinding.compoundName == CharOperation.NO_CHAR_CHAR ? "default" : //$NON-NLS-1$
CharOperation.toString(superPackageBinding.compoundName);
String[] arguments = new String[] {typeName, packageName, name};
this.handle(problem,
arguments,
arguments,
curType.sourceStart,
curType.sourceEnd);
}

public void sealedSuperTypeDisallowed(int problem, SourceTypeBinding type, TypeReference curType, TypeBinding superTypeBinding) {
String typeName = new String(type.sourceName);
String name = new String(superTypeBinding.sourceName());
String[] arguments = new String[] {typeName, name};
this.handle(problem,
arguments,
arguments,
curType.sourceStart,
curType.sourceEnd);
}

public void sealedSuperClassDoesNotPermit(SourceTypeBinding type, TypeReference superType, TypeBinding superTypeBinding) {
sealedSuperTypeDoesNotPermit(IProblem.SealedSuperClassDoesNotPermit, type, superType, superTypeBinding);
}

public void sealedSuperClassInDifferentPackage(SourceTypeBinding type, TypeReference curType, TypeBinding superTypeBinding, PackageBinding superPackageBinding) {
sealedSuperTypeInDifferentPackage(IProblem.SealedSuperTypeInDifferentPackage, type, curType, superTypeBinding, superPackageBinding);
}

public void sealedSuperClassDisallowed(SourceTypeBinding type, TypeReference curType, TypeBinding superTypeBinding) {
sealedSuperTypeDisallowed(IProblem.SealedSuperTypeDisallowed, type, curType, superTypeBinding);
}

public void sealedSuperInterfaceDoesNotPermit(SourceTypeBinding type, TypeReference superType, TypeBinding superTypeBinding) {
String name = new String(type.sourceName());
String superTypeFullName = new String(superTypeBinding.readableName());
String superTypeShortName = new String(superTypeBinding.shortReadableName());
String keyword = type.isClass() ? new String(TypeConstants.IMPLEMENTS) : new String(TypeConstants.KEYWORD_EXTENDS);
if (superTypeShortName.equals(name)) superTypeShortName = superTypeFullName;
this.handle(
IProblem.SealedSuperInterfaceDoesNotPermit,
new String[] {name, superTypeFullName, keyword},
new String[] {name, superTypeShortName, keyword},
superType.sourceStart,
superType.sourceEnd);
}

public void sealedSuperInterfaceInDifferentPackage(SourceTypeBinding type, TypeReference curType, TypeBinding superTypeBinding, PackageBinding superPackageBinding) {
sealedSuperTypeInDifferentPackage(IProblem.SealedSuperTypeInDifferentPackage, type, curType, superTypeBinding, superPackageBinding);
}

public void sealedSuperInterfaceDisallowed(SourceTypeBinding type, TypeReference curType, TypeBinding superTypeBinding) {
sealedSuperTypeDisallowed(IProblem.SealedSuperTypeDisallowed, type, curType, superTypeBinding);
if (superTypeBinding.isClass()) {
this.handle(
IProblem.SealedSuperClassDoesNotPermit,
new String[] {name, superTypeFullName},
new String[] {name, superTypeShortName},
superType.sourceStart,
superType.sourceEnd);
} else {
String keyword = type.isClass() ? new String(TypeConstants.IMPLEMENTS) : new String(TypeConstants.KEYWORD_EXTENDS);
this.handle(
IProblem.SealedSuperInterfaceDoesNotPermit,
new String[] {name, superTypeFullName, keyword},
new String[] {name, superTypeShortName, keyword},
superType.sourceStart,
superType.sourceEnd);
}
}

public void missingSealedModifier(SourceTypeBinding type, ASTNode node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ public void testBug563806_010() {
"1. ERROR in p2\\Y.java (at line 2)\n" +
" public final class Y extends p1.X{}\n" +
" ^^^^\n" +
"Sealed type X and sub type Y in an unnamed module should be declared in the same package p1\n" +
"The type Y extending a sealed class X should be a permitted subtype of X\n" +
"----------\n");
}
public void testBug563806_011() {
Expand Down Expand Up @@ -660,7 +660,7 @@ public void testBug563806_012() {
"1. ERROR in p2\\Y.java (at line 2)\n" +
" public final class Y implements p1.X{}\n" +
" ^^^^\n" +
"Sealed type X and sub type Y in an unnamed module should be declared in the same package p1\n" +
"The type Y that implements a sealed interface X should be a permitted subtype of X\n" +
"----------\n");
}
public void testBug563806_013() {
Expand Down Expand Up @@ -705,7 +705,7 @@ public void testBug563806_014() {
"2. ERROR in p2\\Y.java (at line 2)\n" +
" public interface Y extends p1.X{}\n" +
" ^^^^\n" +
"Sealed type X and sub type Y in an unnamed module should be declared in the same package p1\n" +
"The type Y that extends a sealed interface X should be a permitted subtype of X\n" +
"----------\n");
}
public void testBug563806_015() {
Expand Down

0 comments on commit 99e5404

Please sign in to comment.