From 0fda5de36aafd07fa81272ea2587b2242641269d Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Mon, 31 Mar 2025 11:09:38 -0700 Subject: [PATCH] Elements. Deprecate ClassElement. Change-Id: I0bf4a73bacc4917d4f61c91bcdceba41a9c18ec9 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/418903 Reviewed-by: Samuel Rawlins Reviewed-by: Paul Berry Commit-Queue: Konstantin Shcheglov --- pkg/analyzer/api.txt | 10 ++++----- pkg/analyzer/lib/dart/element/element.dart | 22 +++++++++++++++++-- .../lib/src/dart/element/element.dart | 11 ++++++++-- .../lib/src/utilities/extensions/element.dart | 12 ---------- .../rules/analyzer_use_new_elements_test.dart | 12 +++++++--- 5 files changed, 43 insertions(+), 24 deletions(-) diff --git a/pkg/analyzer/api.txt b/pkg/analyzer/api.txt index 9ac6c59b2f8..081519b7a2d 100644 --- a/pkg/analyzer/api.txt +++ b/pkg/analyzer/api.txt @@ -3015,7 +3015,7 @@ package:analyzer/dart/element/element.dart: elementModelDeprecationMsg (static getter: String) BindPatternVariableElement (class extends Object implements PatternVariableElement, deprecated): new (constructor: BindPatternVariableElement Function()) - ClassElement (class extends Object implements InterfaceElement): + ClassElement (class extends Object implements InterfaceElement, deprecated): new (constructor: ClassElement Function()) hasNonFinalField (getter: bool) isAbstract (getter: bool) @@ -3033,7 +3033,7 @@ package:analyzer/dart/element/element.dart: isExtendableIn (method: bool Function(LibraryElement)) isImplementableIn (method: bool Function(LibraryElement)) isMixableIn (method: bool Function(LibraryElement)) - ClassMemberElement (class extends Object implements Element): + ClassMemberElement (class extends Object implements Element, deprecated): new (constructor: ClassMemberElement Function()) enclosingElement3 (getter: Element) isStatic (getter: bool) @@ -3342,9 +3342,9 @@ package:analyzer/dart/element/element.dart: allSupertypes (getter: List) constructors (getter: List, deprecated) interfaces (getter: List) - mixins (getter: List) + mixins (getter: List, deprecated) name (getter: String) - supertype (getter: InterfaceType?) + supertype (getter: InterfaceType?, deprecated) thisType (getter: InterfaceType) unnamedConstructor (getter: ConstructorElement?, deprecated) getField (method: FieldElement? Function(String)) @@ -3392,7 +3392,7 @@ package:analyzer/dart/element/element.dart: typeProvider (getter: TypeProvider) typeSystem (getter: TypeSystem) units (getter: List, deprecated) - getClass (method: ClassElement? Function(String)) + getClass (method: ClassElement? Function(String), deprecated) LibraryExportElement (class extends Object implements _ExistingElement): new (constructor: LibraryExportElement Function()) combinators (getter: List) diff --git a/pkg/analyzer/lib/dart/element/element.dart b/pkg/analyzer/lib/dart/element/element.dart index 03d34f5f477..576000aa11b 100644 --- a/pkg/analyzer/lib/dart/element/element.dart +++ b/pkg/analyzer/lib/dart/element/element.dart @@ -78,6 +78,7 @@ abstract class BindPatternVariableElement implements PatternVariableElement {} /// a class body), a mixin declaration, or an enum declaration. /// /// Clients may not extend, implement or mix-in this class. +@Deprecated('Use ClassElement2 instead') abstract class ClassElement implements InterfaceElement { /// Whether the class or its superclass declares a non-final instance field. bool get hasNonFinalField; @@ -170,6 +171,13 @@ abstract class ClassElement implements InterfaceElement { /// An element that is contained within a [ClassElement]. /// /// Clients may not extend, implement or mix-in this class. +@Deprecated(''' +There is no common interface for class members in the new analyzer element +model. If you are using this class in an `is` test or a pattern match, replace +it with checks for the specific element types you are interested in (e.g., +`ConstructorElement2`, `MethodElement2`, etc.). If you are using this class as +a type annotation for a variable that could hold any kind of class member, use +`Element2` instead.''') abstract class ClassMemberElement implements Element { // TODO(brianwilkerson): Either remove this class or rename it to something // more correct. @@ -1196,7 +1204,10 @@ abstract class ExtensionTypeElement implements InterfaceElement { /// /// Clients may not extend, implement or mix-in this class. abstract class FieldElement - implements ClassMemberElement, PropertyInducingElement { + implements + // ignore:deprecated_member_use_from_same_package + ClassMemberElement, + PropertyInducingElement { @override FieldElement get declaration; @@ -1369,6 +1380,7 @@ abstract class InterfaceElement implements InstanceElement { /// safe to assume that the inheritance structure of a class does not contain /// a cycle. Clients that traverse the inheritance structure must explicitly /// guard against infinite loops. + @Deprecated(elementModelDeprecationMsg) List get mixins; @override @@ -1389,6 +1401,7 @@ abstract class InterfaceElement implements InstanceElement { /// safe to assume that the inheritance structure of a class does not contain /// a cycle. Clients that traverse the inheritance structure must explicitly /// guard against infinite loops. + @Deprecated(elementModelDeprecationMsg) InterfaceType? get supertype; @override @@ -1713,6 +1726,7 @@ abstract class LibraryElement implements _ExistingElement { /// The class defined in this library that has the given [name], or /// `null` if this library does not define a class with the given name. + @Deprecated(elementModelDeprecationMsg) ClassElement? getClass(String name); } @@ -1806,7 +1820,11 @@ abstract class LocalVariableElement implements PromotableElement { /// An element that represents a method defined within a class. /// /// Clients may not extend, implement or mix-in this class. -abstract class MethodElement implements ClassMemberElement, ExecutableElement { +abstract class MethodElement + implements + // ignore:deprecated_member_use_from_same_package + ClassMemberElement, + ExecutableElement { @override MethodElement get declaration; } diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart index 6a789b1345f..de3a73c2a64 100644 --- a/pkg/analyzer/lib/src/dart/element/element.dart +++ b/pkg/analyzer/lib/src/dart/element/element.dart @@ -140,7 +140,10 @@ class BindPatternVariableElementImpl2 extends PatternVariableElementImpl2 /// An [InterfaceElementImpl] which is a class. class ClassElementImpl extends ClassOrMixinElementImpl - implements ClassElement, ClassFragment { + implements + // ignore:deprecated_member_use_from_same_package + ClassElement, + ClassFragment { late ClassElementImpl2 augmentedInternal; /// Initialize a newly created class element to have the given [name] at the @@ -221,7 +224,7 @@ class ClassElementImpl extends ClassOrMixinElementImpl bool get hasNoSuchMethod { MethodElement? method = lookUpConcreteMethod( FunctionElement.NO_SUCH_METHOD_METHOD_NAME, library); - var definingClass = method?.enclosingElement3 as ClassElement?; + var definingClass = method?.enclosingElement3 as ClassElementImpl?; return definingClass != null && !definingClass.isDartCoreObject; } @@ -6161,6 +6164,7 @@ abstract class InterfaceElementImpl extends InstanceElementImpl getter.enclosingElement3 != this); } + @Deprecated(elementModelDeprecationMsg) ExecutableElement? lookUpInheritedConcreteMember( String name, LibraryElement library) { if (name.endsWith('=')) { @@ -6181,6 +6185,7 @@ abstract class InterfaceElementImpl extends InstanceElementImpl method.enclosingElement3 != this); } + @Deprecated(elementModelDeprecationMsg) @override PropertyAccessorElement? lookUpInheritedConcreteSetter( String setterName, LibraryElement library) { @@ -6242,6 +6247,7 @@ abstract class InterfaceElementImpl extends InstanceElementImpl /// This method should be used only for error recovery during analysis, /// when instance access to a static class member, defined in this class, /// or a superclass. + @Deprecated(elementModelDeprecationMsg) PropertyAccessorElement? lookupStaticSetter( String name, LibraryElement library) { return _implementationsOfSetter(name).firstWhereOrNull( @@ -6326,6 +6332,7 @@ abstract class InterfaceElementImpl extends InstanceElementImpl /// The setters are returned based on the depth of their defining class; if /// this class contains a definition of the setter it will occur first, if /// Object contains a definition of the setter it will occur last. + @Deprecated(elementModelDeprecationMsg) Iterable _implementationsOfSetter( String setterName) sync* { var visitedClasses = {}; diff --git a/pkg/analyzer/lib/src/utilities/extensions/element.dart b/pkg/analyzer/lib/src/utilities/extensions/element.dart index 91463829386..113031e31ba 100644 --- a/pkg/analyzer/lib/src/utilities/extensions/element.dart +++ b/pkg/analyzer/lib/src/utilities/extensions/element.dart @@ -53,18 +53,6 @@ extension BindPatternVariableElementImplExtension } } -extension ClassElement2Extension on ClassElement2 { - ClassElement get asElement { - return firstFragment as ClassElement; - } -} - -extension ClassElementExtension on ClassElement { - ClassElement2 get asElement2 { - return (this as ClassElementImpl).element; - } -} - extension ClassElementImpl2Extension on ClassElementImpl2 { ClassElementImpl get asElement { return firstFragment; diff --git a/pkg/linter/test/rules/analyzer_use_new_elements_test.dart b/pkg/linter/test/rules/analyzer_use_new_elements_test.dart index 051ba0ec83b..308ebe9a32c 100644 --- a/pkg/linter/test/rules/analyzer_use_new_elements_test.dart +++ b/pkg/linter/test/rules/analyzer_use_new_elements_test.dart @@ -42,14 +42,17 @@ class AnalyzerUseNewElementsTest extends LintRuleTest { } test_enablement_optedOut() async { - await assertDiagnostics(r''' + await assertDiagnostics( + r''' // ignore_for_file: analyzer_use_new_elements import 'package:analyzer/dart/element/element.dart'; ClassElement f() { throw 42; } -''', []); +''', + [error(HintCode.DEPRECATED_MEMBER_USE_WITH_MESSAGE, 100, 12)], + ); } test_interfaceTypeImpl_element() async { @@ -138,7 +141,10 @@ ClassElement f() { throw 42; } ''', - [lint(54, 12)], + [ + error(HintCode.DEPRECATED_MEMBER_USE_WITH_MESSAGE, 54, 12), + lint(54, 12), + ], ); }