diff --git a/pkg/analyzer/analyzer_use_new_elements.txt b/pkg/analyzer/analyzer_use_new_elements.txt index 7f1aae11ea19..0116bbe889dd 100644 --- a/pkg/analyzer/analyzer_use_new_elements.txt +++ b/pkg/analyzer/analyzer_use_new_elements.txt @@ -98,7 +98,6 @@ lib/src/dart/resolver/typed_literal_resolver.dart lib/src/dart/resolver/variable_declaration_resolver.dart lib/src/dart/resolver/yield_statement_resolver.dart lib/src/diagnostic/diagnostic_factory.dart -lib/src/error/annotation_verifier.dart lib/src/error/assignment_verifier.dart lib/src/error/base_or_final_type_verifier.dart lib/src/error/best_practices_verifier.dart diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart index 448e19161033..ca33253c1f75 100644 --- a/pkg/analyzer/lib/src/dart/element/element.dart +++ b/pkg/analyzer/lib/src/dart/element/element.dart @@ -4230,7 +4230,7 @@ class FieldElementImpl2 extends PropertyInducingElementImpl2 ElementKind get kind => ElementKind.FIELD; @override - String get name3 => firstFragment.name; + String? get name3 => firstFragment.name2; @override SetterElement? get setter2 => firstFragment.setter?.element as SetterElement?; diff --git a/pkg/analyzer/lib/src/dart/element/extensions.dart b/pkg/analyzer/lib/src/dart/element/extensions.dart index 980b16d36774..d1cf93d08a90 100644 --- a/pkg/analyzer/lib/src/dart/element/extensions.dart +++ b/pkg/analyzer/lib/src/dart/element/extensions.dart @@ -18,6 +18,28 @@ extension DartTypeExtension on DartType { } extension Element2Extension on Element2 { + /// Return `true` if this element is an instance member of a class or mixin. + /// + /// Only [MethodElement2]s, [GetterElement]s, and [SetterElement]s are + /// supported. + /// + /// We intentionally exclude [ConstructorElement2]s - they can only be + /// invoked in instance creation expressions, and [FieldElement2]s - they + /// cannot be invoked directly and are always accessed using corresponding + /// [GetterElement]s or [SetterElement]s. + bool get isInstanceMember { + assert(this is! PropertyInducingElement2, + 'Check the GetterElement or SetterElement instead'); + var this_ = this; + var enclosing = this_.enclosingElement2; + if (enclosing is InterfaceElement2) { + return this_ is MethodElement2 && !this_.isStatic || + this_ is GetterElement && !this_.isStatic || + this_ is SetterElement && !this_.isStatic; + } + return false; + } + /// Whether this element is a wildcard variable. bool get isWildcardVariable { return name3 == '_' && diff --git a/pkg/analyzer/lib/src/error/annotation_verifier.dart b/pkg/analyzer/lib/src/error/annotation_verifier.dart index 35c45f9c7061..08b56d811c37 100644 --- a/pkg/analyzer/lib/src/error/annotation_verifier.dart +++ b/pkg/analyzer/lib/src/error/annotation_verifier.dart @@ -2,12 +2,14 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -import 'package:analyzer/dart/element/element.dart'; +import 'package:analyzer/dart/element/element2.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/error/listener.dart'; import 'package:analyzer/src/dart/ast/ast.dart'; import 'package:analyzer/src/dart/element/extensions.dart'; import 'package:analyzer/src/error/codes.dart'; +import 'package:analyzer/src/utilities/extensions/ast.dart'; +import 'package:analyzer/src/utilities/extensions/object.dart'; import 'package:analyzer/src/utilities/extensions/string.dart'; import 'package:analyzer/src/workspace/workspace.dart'; import 'package:meta/meta_meta.dart'; @@ -17,14 +19,15 @@ class AnnotationVerifier { final ErrorReporter _errorReporter; /// The current library. - final LibraryElement _currentLibrary; + final LibraryElement2 _currentLibrary; /// The [WorkspacePackage] in which [_currentLibrary] is declared. final WorkspacePackage? _workspacePackage; /// Whether [_currentLibrary] is part of its containing package's public API. late final bool _inPackagePublicApi = _workspacePackage != null && - _workspacePackage.sourceIsInPublicApi(_currentLibrary.source); + _workspacePackage + .sourceIsInPublicApi(_currentLibrary.firstFragment.source); AnnotationVerifier( this._errorReporter, @@ -115,12 +118,13 @@ class AnnotationVerifier { /// `@internal` annotation. void _checkInternal(Annotation node) { var parent = node.parent; - var parentElement = parent is Declaration ? parent.declaredElement : null; + var parentElement = + parent.ifTypeOrNull()?.declaredFragment?.element; var parentElementIsPrivate = parentElement?.isPrivate ?? false; if (parent is TopLevelVariableDeclaration) { for (var variable in parent.variables.variables) { - var element = variable.declaredElement as TopLevelVariableElement; - if (Identifier.isPrivateName(element.name)) { + var element = variable.declaredTopLevelVariableElement; + if (element.isPrivate) { _errorReporter.atNode( variable, WarningCode.INVALID_INTERNAL_ANNOTATION, @@ -129,8 +133,8 @@ class AnnotationVerifier { } } else if (parent is FieldDeclaration) { for (var variable in parent.fields.variables) { - var element = variable.declaredElement as FieldElement; - if (Identifier.isPrivateName(element.name)) { + var element = variable.declaredFieldElement; + if (element.isPrivate) { _errorReporter.atNode( variable, WarningCode.INVALID_INTERNAL_ANNOTATION, @@ -138,7 +142,8 @@ class AnnotationVerifier { } } } else if (parent is ConstructorDeclaration) { - var class_ = parent.declaredElement!.enclosingElement3; + var element = parent.declaredFragment!.element; + var class_ = element.enclosingElement2; if (class_.isPrivate || parentElementIsPrivate) { _errorReporter.atNode( node.name, @@ -162,10 +167,10 @@ class AnnotationVerifier { var kinds = element.targetKinds; if (kinds.isNotEmpty) { if (!_isValidTarget(parent, kinds)) { - var invokedElement = element.element!; - var name = invokedElement.name; - if (invokedElement is ConstructorElement) { - var className = invokedElement.enclosingElement3.name; + var invokedElement = element.element2!; + var name = invokedElement.name3; + if (invokedElement is ConstructorElement2) { + var className = invokedElement.enclosingElement2.name3; if (name!.isEmpty) { name = className; } else { @@ -243,16 +248,16 @@ class AnnotationVerifier { /// Reports a warning at [node] if its parent is not a valid target for a /// `@reopen` annotation. void _checkReopen(Annotation node) { - ClassElement? classElement; - InterfaceElement? superElement; + ClassElement2? classElement; + InterfaceElement2? superElement; var parent = node.parent; if (parent is ClassDeclaration) { - classElement = parent.declaredElement; - superElement = classElement?.supertype?.element; + classElement = parent.declaredFragment?.element; + superElement = classElement?.supertype?.element3; } else if (parent is ClassTypeAlias) { - classElement = parent.declaredElement; - superElement = classElement?.supertype?.element; + classElement = parent.declaredFragment?.element; + superElement = classElement?.supertype?.element3; } else { // If `parent` is neither of the above types, then `_checkKinds` will // report a warning. @@ -262,7 +267,7 @@ class AnnotationVerifier { if (classElement == null) { return; } - if (superElement is! ClassElement) { + if (superElement is! ClassElement2) { return; } if (classElement.isFinal || @@ -274,7 +279,7 @@ class AnnotationVerifier { ); return; } - if (classElement.library != superElement.library) { + if (classElement.library2 != superElement.library2) { _errorReporter.atNode( node.name, WarningCode.INVALID_REOPEN_ANNOTATION, @@ -319,7 +324,7 @@ class AnnotationVerifier { if (name != null) { var parameterName = undefinedParameter is SimpleStringLiteral ? undefinedParameter.value - : undefinedParameter.staticParameterElement?.name; + : undefinedParameter.correspondingParameter?.name3; _errorReporter.atNode( undefinedParameter, WarningCode.UNDEFINED_REFERENCED_PARAMETER, @@ -354,11 +359,10 @@ class AnnotationVerifier { if (parent is TopLevelVariableDeclaration) { for (VariableDeclaration variable in parent.variables.variables) { - var variableElement = - variable.declaredElement as TopLevelVariableElement; + var variableElement = variable.declaredTopLevelVariableElement; - var variableName = variableElement.name; - if (Identifier.isPrivateName(variableName)) { + var variableName = variableElement.name3; + if (variableName != null && Identifier.isPrivateName(variableName)) { reportInvalidAnnotation(variableName); } @@ -369,25 +373,24 @@ class AnnotationVerifier { } } else if (parent is FieldDeclaration) { for (VariableDeclaration variable in parent.fields.variables) { - var fieldElement = variable.declaredElement as FieldElement; + var fieldElement = variable.declaredFieldElement; if (parent.isStatic && element.isVisibleForOverriding) { reportInvalidVisibleForOverriding(); } - var fieldName = fieldElement.name; - if (Identifier.isPrivateName(fieldName)) { + var fieldName = fieldElement.name3; + if (fieldName != null && Identifier.isPrivateName(fieldName)) { reportInvalidAnnotation(fieldName); } } - } else if (parent.declaredElement != null) { - var declaredElement = parent.declaredElement!; + } else if (parent.declaredFragment?.element case var declaredElement?) { if (element.isVisibleForOverriding && (!declaredElement.isInstanceMember || - declaredElement.enclosingElement3 is ExtensionTypeElement)) { + declaredElement.enclosingElement2 is ExtensionTypeElement2)) { reportInvalidVisibleForOverriding(); } - var name = declaredElement.name; + var name = declaredElement.name3; if (name != null && Identifier.isPrivateName(name)) { reportInvalidAnnotation(name); } @@ -425,14 +428,14 @@ class AnnotationVerifier { return; } - InterfaceElement? declaredElement; + InterfaceElement2? declaredElement; switch (containedDeclaration.parent) { case ClassDeclaration classDeclaration: - declaredElement = classDeclaration.declaredElement; + declaredElement = classDeclaration.declaredFragment?.element; case EnumDeclaration enumDeclaration: - declaredElement = enumDeclaration.declaredElement; + declaredElement = enumDeclaration.declaredFragment?.element; case MixinDeclaration mixinDeclaration: - declaredElement = mixinDeclaration.declaredElement; + declaredElement = mixinDeclaration.declaredFragment?.element; default: reportError(); return; @@ -443,7 +446,7 @@ class AnnotationVerifier { return; } - for (var annotation in declaredElement.metadata) { + for (var annotation in declaredElement.metadata2.annotations) { if (annotation.isVisibleForTemplate) { return; } diff --git a/pkg/analyzer/lib/src/error/best_practices_verifier.dart b/pkg/analyzer/lib/src/error/best_practices_verifier.dart index 379960f6f4c9..c0af0f258344 100644 --- a/pkg/analyzer/lib/src/error/best_practices_verifier.dart +++ b/pkg/analyzer/lib/src/error/best_practices_verifier.dart @@ -64,7 +64,7 @@ class BestPracticesVerifier extends RecursiveAstVisitor { final InheritanceManager3 _inheritanceManager; /// The current library. - final LibraryElement _currentLibrary; + final LibraryElementImpl _currentLibrary; final AnnotationVerifier _annotationVerifier; diff --git a/pkg/analyzer/lib/src/utilities/extensions/ast.dart b/pkg/analyzer/lib/src/utilities/extensions/ast.dart index 56317905c159..ae5df27afec1 100644 --- a/pkg/analyzer/lib/src/utilities/extensions/ast.dart +++ b/pkg/analyzer/lib/src/utilities/extensions/ast.dart @@ -5,6 +5,7 @@ import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/ast/token.dart'; import 'package:analyzer/dart/element/element.dart'; +import 'package:analyzer/src/dart/element/element.dart'; extension AstNodeExtension on AstNode { /// Returns all tokens, from [beginToken] to [endToken] including. @@ -179,3 +180,13 @@ extension CompilationUnitExtension on CompilationUnit { }; } } + +extension VariableDeclarationExtension on VariableDeclaration { + FieldElementImpl2 get declaredFieldElement { + return declaredFragment!.element as FieldElementImpl2; + } + + TopLevelVariableElementImpl2 get declaredTopLevelVariableElement { + return declaredFragment!.element as TopLevelVariableElementImpl2; + } +} diff --git a/pkg/analyzer/test/src/summary/elements/class_test.dart b/pkg/analyzer/test/src/summary/elements/class_test.dart index 678bc973fd3b..8fd752a3f2a6 100644 --- a/pkg/analyzer/test/src/summary/elements/class_test.dart +++ b/pkg/analyzer/test/src/summary/elements/class_test.dart @@ -14903,7 +14903,7 @@ library type: Object getter: ::@class::C::@getter::a#element setter: ::@class::C::@setter::a#element - + firstFragment: ::@class::C::@field::0 type: Object getter: ::@class::C::@getter::0#element diff --git a/pkg/analyzer/test/src/summary/elements/enum_test.dart b/pkg/analyzer/test/src/summary/elements/enum_test.dart index 76bb500f5539..279961b23cef 100644 --- a/pkg/analyzer/test/src/summary/elements/enum_test.dart +++ b/pkg/analyzer/test/src/summary/elements/enum_test.dart @@ -6358,7 +6358,7 @@ library firstFragment: ::@enum::E::@field::v type: E getter: ::@enum::E::@getter::v#element - static const + static const firstFragment: ::@enum::E::@field::0 type: E getter: ::@enum::E::@getter::0#element diff --git a/pkg/analyzer/test/src/summary/elements/extension_type_test.dart b/pkg/analyzer/test/src/summary/elements/extension_type_test.dart index 0e78d96edaec..c6e9bbc6013c 100644 --- a/pkg/analyzer/test/src/summary/elements/extension_type_test.dart +++ b/pkg/analyzer/test/src/summary/elements/extension_type_test.dart @@ -2524,7 +2524,7 @@ library primaryConstructor: ::@extensionType::A::@constructor::new#element typeErasure: InvalidType fields - final + final firstFragment: ::@extensionType::A::@field:: type: InvalidType getter: ::@extensionType::A::@getter::#element