From 9737e0c853043453e383d6d3f034e31533c37cd5 Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Thu, 10 Jul 2025 09:14:17 -0700 Subject: [PATCH] analyzer: Simplify the sealed supertype check Change-Id: I4481b18f7e910338802e964bbc3c50a338b0ff74 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/439800 Auto-Submit: Samuel Rawlins Commit-Queue: Samuel Rawlins Reviewed-by: Konstantin Shcheglov Commit-Queue: Konstantin Shcheglov --- .../lib/src/generated/error_verifier.dart | 67 ++++++------------- 1 file changed, 20 insertions(+), 47 deletions(-) diff --git a/pkg/analyzer/lib/src/generated/error_verifier.dart b/pkg/analyzer/lib/src/generated/error_verifier.dart index e413d7a27cd..f2238b7c193 100644 --- a/pkg/analyzer/lib/src/generated/error_verifier.dart +++ b/pkg/analyzer/lib/src/generated/error_verifier.dart @@ -1746,12 +1746,11 @@ class ErrorVerifier extends RecursiveAstVisitor 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 ); } - /// 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 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 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 implementsClause, onClause, ); - _checkForSealedSupertypeOutsideOfLibrary( - null, - null, - implementsClause, - onClause, - ); + _checkForSealedSupertypeOutsideOfLibrary([ + ...?implementsClause?.interfaces, + ...?onClause?.superclassConstraints, + ]); } }