analyzer: Simplify the sealed supertype check

Change-Id: I4481b18f7e910338802e964bbc3c50a338b0ff74
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/439800
Auto-Submit: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Sam Rawlins
2025-07-10 09:14:17 -07:00
committed by Commit Queue
parent b207705bd5
commit 9737e0c853
@@ -1746,12 +1746,11 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
null,
);
_checkForClassUsedAsMixin(withClause);
_checkForSealedSupertypeOutsideOfLibrary(
superclass,
withClause,
implementsClause,
null,
);
_checkForSealedSupertypeOutsideOfLibrary([
if (superclass != null) superclass,
...?withClause?.mixinTypes,
...?implementsClause?.interfaces,
]);
return true;
}
return false;
@@ -5214,46 +5213,22 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
);
}
/// Check that if a direct supertype of a node is sealed, then it must be in
/// the same library.
/// Checks that every supertype which is sealed is also declared in the
/// current library.
///
/// See [CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY].
void _checkForSealedSupertypeOutsideOfLibrary(
NamedType? superclass,
WithClause? withClause,
ImplementsClause? implementsClause,
MixinOnClause? onClause,
) {
void reportErrorsForSealedClassesAndMixins(List<NamedType> namedTypes) {
for (NamedType namedType in namedTypes) {
var type = namedType.type;
if (type is InterfaceType) {
var element = type.element;
if (element is ClassElement &&
element.isSealed &&
element.library != _currentLibrary) {
diagnosticReporter.atNode(
namedType,
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY,
arguments: [element.name!],
);
}
void _checkForSealedSupertypeOutsideOfLibrary(List<NamedType> supertypes) {
for (NamedType namedType in supertypes) {
if (namedType.type case InterfaceType(:ClassElement element)) {
if (element.isSealed && element.library != _currentLibrary) {
diagnosticReporter.atNode(
namedType,
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY,
arguments: [element.name!],
);
}
}
}
if (superclass != null) {
reportErrorsForSealedClassesAndMixins([superclass]);
}
if (withClause != null) {
reportErrorsForSealedClassesAndMixins(withClause.mixinTypes);
}
if (implementsClause != null) {
reportErrorsForSealedClassesAndMixins(implementsClause.interfaces);
}
if (onClause != null) {
reportErrorsForSealedClassesAndMixins(onClause.superclassConstraints);
}
}
/// Verify that the elements in the given set [literal] are subtypes of the
@@ -6067,12 +6042,10 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
implementsClause,
onClause,
);
_checkForSealedSupertypeOutsideOfLibrary(
null,
null,
implementsClause,
onClause,
);
_checkForSealedSupertypeOutsideOfLibrary([
...?implementsClause?.interfaces,
...?onClause?.superclassConstraints,
]);
}
}