Skip to content

Commit 9737e0c

Browse files
srawlinsCommit Queue
authored andcommitted
analyzer: Simplify the sealed supertype check
Change-Id: I4481b18f7e910338802e964bbc3c50a338b0ff74 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/439800 Auto-Submit: Samuel Rawlins <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent b207705 commit 9737e0c

File tree

1 file changed

+20
-47
lines changed

1 file changed

+20
-47
lines changed

pkg/analyzer/lib/src/generated/error_verifier.dart

Lines changed: 20 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,12 +1746,11 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
17461746
null,
17471747
);
17481748
_checkForClassUsedAsMixin(withClause);
1749-
_checkForSealedSupertypeOutsideOfLibrary(
1750-
superclass,
1751-
withClause,
1752-
implementsClause,
1753-
null,
1754-
);
1749+
_checkForSealedSupertypeOutsideOfLibrary([
1750+
if (superclass != null) superclass,
1751+
...?withClause?.mixinTypes,
1752+
...?implementsClause?.interfaces,
1753+
]);
17551754
return true;
17561755
}
17571756
return false;
@@ -5214,46 +5213,22 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
52145213
);
52155214
}
52165215

5217-
/// Check that if a direct supertype of a node is sealed, then it must be in
5218-
/// the same library.
5216+
/// Checks that every supertype which is sealed is also declared in the
5217+
/// current library.
52195218
///
52205219
/// See [CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY].
5221-
void _checkForSealedSupertypeOutsideOfLibrary(
5222-
NamedType? superclass,
5223-
WithClause? withClause,
5224-
ImplementsClause? implementsClause,
5225-
MixinOnClause? onClause,
5226-
) {
5227-
void reportErrorsForSealedClassesAndMixins(List<NamedType> namedTypes) {
5228-
for (NamedType namedType in namedTypes) {
5229-
var type = namedType.type;
5230-
if (type is InterfaceType) {
5231-
var element = type.element;
5232-
if (element is ClassElement &&
5233-
element.isSealed &&
5234-
element.library != _currentLibrary) {
5235-
diagnosticReporter.atNode(
5236-
namedType,
5237-
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY,
5238-
arguments: [element.name!],
5239-
);
5240-
}
5220+
void _checkForSealedSupertypeOutsideOfLibrary(List<NamedType> supertypes) {
5221+
for (NamedType namedType in supertypes) {
5222+
if (namedType.type case InterfaceType(:ClassElement element)) {
5223+
if (element.isSealed && element.library != _currentLibrary) {
5224+
diagnosticReporter.atNode(
5225+
namedType,
5226+
CompileTimeErrorCode.SEALED_CLASS_SUBTYPE_OUTSIDE_OF_LIBRARY,
5227+
arguments: [element.name!],
5228+
);
52415229
}
52425230
}
52435231
}
5244-
5245-
if (superclass != null) {
5246-
reportErrorsForSealedClassesAndMixins([superclass]);
5247-
}
5248-
if (withClause != null) {
5249-
reportErrorsForSealedClassesAndMixins(withClause.mixinTypes);
5250-
}
5251-
if (implementsClause != null) {
5252-
reportErrorsForSealedClassesAndMixins(implementsClause.interfaces);
5253-
}
5254-
if (onClause != null) {
5255-
reportErrorsForSealedClassesAndMixins(onClause.superclassConstraints);
5256-
}
52575232
}
52585233

52595234
/// Verify that the elements in the given set [literal] are subtypes of the
@@ -6067,12 +6042,10 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
60676042
implementsClause,
60686043
onClause,
60696044
);
6070-
_checkForSealedSupertypeOutsideOfLibrary(
6071-
null,
6072-
null,
6073-
implementsClause,
6074-
onClause,
6075-
);
6045+
_checkForSealedSupertypeOutsideOfLibrary([
6046+
...?implementsClause?.interfaces,
6047+
...?onClause?.superclassConstraints,
6048+
]);
60766049
}
60776050
}
60786051

0 commit comments

Comments
 (0)