From 22fe1947d24e51c2c57235a0dc1b60ddb6976221 Mon Sep 17 00:00:00 2001 From: Konstantin Shcheglov Date: Mon, 13 Jan 2025 11:30:19 -0800 Subject: [PATCH] Elements. Migrate ThisLookup and LexicalLookup. Change-Id: I29d36ed43907871a0ffed8bd71619ef55a336530 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/404240 Commit-Queue: Konstantin Shcheglov Reviewed-by: Phil Quitslund --- .../lib/src/dart/element/element.dart | 35 ++++++++++++-- .../lib/src/dart/resolver/lexical_lookup.dart | 18 ++++--- .../resolver/property_element_resolver.dart | 6 +-- .../lib/src/dart/resolver/this_lookup.dart | 10 ++-- .../lib/src/utilities/extensions/element.dart | 48 +++++++++---------- 5 files changed, 71 insertions(+), 46 deletions(-) diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart index 4efd68c9edb8..8d4c8c67a5c3 100644 --- a/pkg/analyzer/lib/src/dart/element/element.dart +++ b/pkg/analyzer/lib/src/dart/element/element.dart @@ -1661,6 +1661,11 @@ class ConstructorElementImpl2 extends ExecutableElementImpl2 @override ElementKind get kind => ElementKind.CONSTRUCTOR; + @override + ConstructorElementImpl get lastFragment { + return super.lastFragment as ConstructorElementImpl; + } + @override Element2 get nonSynthetic2 { if (isSynthetic) { @@ -3855,6 +3860,17 @@ abstract class ExecutableElementImpl2 extends FunctionTypedElementImpl2 return firstFragment.hasModifier(Modifier.INVOKES_SUPER_SELF); } + ExecutableElementImpl get lastFragment { + var result = firstFragment as ExecutableElementImpl; + while (true) { + if (result.nextFragment case ExecutableElementImpl nextFragment) { + result = nextFragment; + } else { + return result; + } + } + } + @override LibraryElement2 get library2 { var firstFragment = this.firstFragment as ExecutableElementImpl; @@ -5601,9 +5617,7 @@ abstract class InstanceElementImpl2 extends ElementImpl2 @override List get typeParameters2 => - firstFragment.typeParameters - .map((fragment) => fragment.element) - .toList(); + firstFragment.typeParameters.map((fragment) => fragment.element).toList(); @override String displayString2( @@ -8124,6 +8138,11 @@ class MethodElementImpl2 extends ExecutableElementImpl2 @override ElementKind get kind => ElementKind.METHOD; + @override + MethodElementImpl get lastFragment { + return super.lastFragment as MethodElementImpl; + } + @override String? get lookupName { if (name3 == '-' && formalParameters.isEmpty) { @@ -9762,6 +9781,11 @@ abstract class PropertyAccessorElementImpl2 extends ExecutableElementImpl2 @override bool get isExternal => firstFragment.isExternal; + @override + PropertyAccessorElementImpl get lastFragment { + return super.lastFragment as PropertyAccessorElementImpl; + } + @override String? get name3 => firstFragment.name2; @@ -10320,6 +10344,11 @@ class TopLevelFunctionElementImpl extends ExecutableElementImpl2 @override ElementKind get kind => ElementKind.FUNCTION; + @override + FunctionElementImpl get lastFragment { + return super.lastFragment as FunctionElementImpl; + } + @override LibraryElement2 get library2 { return firstFragment.library2!; diff --git a/pkg/analyzer/lib/src/dart/resolver/lexical_lookup.dart b/pkg/analyzer/lib/src/dart/resolver/lexical_lookup.dart index 5d64d65e6e50..fd8e0a450bcf 100644 --- a/pkg/analyzer/lib/src/dart/resolver/lexical_lookup.dart +++ b/pkg/analyzer/lib/src/dart/resolver/lexical_lookup.dart @@ -2,9 +2,7 @@ // 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. -// ignore_for_file: analyzer_use_new_elements - -import 'package:analyzer/dart/element/element.dart'; +import 'package:analyzer/dart/element/element2.dart'; import 'package:analyzer/dart/element/scope.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/src/dart/element/extensions.dart'; @@ -21,8 +19,8 @@ class LexicalLookup { /// a getter. If a matching element is found, a [LexicalLookupResult] is /// returned. Otherwise `null` is returned. static LexicalLookupResult? resolveGetter(ScopeLookupResult scopeResult) { - var scopeGetter = scopeResult.getter; - var scopeSetter = scopeResult.setter; + var scopeGetter = scopeResult.getter2; + var scopeSetter = scopeResult.setter2; if (scopeGetter != null || scopeSetter != null) { if (scopeGetter != null) { return LexicalLookupResult(requested: scopeGetter); @@ -39,10 +37,10 @@ class LexicalLookup { /// a setter. If a matching element is found, a [LexicalLookupResult] is /// returned. Otherwise `null` is returned. static LexicalLookupResult? resolveSetter(ScopeLookupResult scopeResult) { - var scopeGetter = scopeResult.getter; - var scopeSetter = scopeResult.setter; + var scopeGetter = scopeResult.getter2; + var scopeSetter = scopeResult.setter2; if (scopeGetter != null || scopeSetter != null) { - if (scopeGetter is VariableElement) { + if (scopeGetter is VariableElement2) { return LexicalLookupResult(requested: scopeGetter); } if (scopeSetter != null) { @@ -58,8 +56,8 @@ class LexicalLookup { } class LexicalLookupResult { - final Element? requested; - final Element? recovery; + final Element2? requested; + final Element2? recovery; /// The type, usually [FunctionType] referenced with `call`. final DartType? callFunctionType; diff --git a/pkg/analyzer/lib/src/dart/resolver/property_element_resolver.dart b/pkg/analyzer/lib/src/dart/resolver/property_element_resolver.dart index 5e99d32d3ecd..f7de9083e07f 100644 --- a/pkg/analyzer/lib/src/dart/resolver/property_element_resolver.dart +++ b/pkg/analyzer/lib/src/dart/resolver/property_element_resolver.dart @@ -266,7 +266,7 @@ class PropertyElementResolver with ScopeHelpers { ); } - readElementRequested = readLookup?.requested; + readElementRequested = readLookup?.requested.asElement; if (readElementRequested is PropertyAccessorElement && !readElementRequested.isStatic) { var unpromotedType = readElementRequested.returnType; @@ -285,8 +285,8 @@ class PropertyElementResolver with ScopeHelpers { if (hasWrite) { var writeLookup = LexicalLookup.resolveSetter(scopeLookupResult) ?? _resolver.thisLookupSetter(node); - writeElementRequested = writeLookup?.requested; - writeElementRecovery = writeLookup?.recovery; + writeElementRequested = writeLookup?.requested.asElement; + writeElementRecovery = writeLookup?.recovery.asElement; AssignmentVerifier(errorReporter).verify( node: node, diff --git a/pkg/analyzer/lib/src/dart/resolver/this_lookup.dart b/pkg/analyzer/lib/src/dart/resolver/this_lookup.dart index dba10f8b210c..222d81193944 100644 --- a/pkg/analyzer/lib/src/dart/resolver/this_lookup.dart +++ b/pkg/analyzer/lib/src/dart/resolver/this_lookup.dart @@ -2,8 +2,6 @@ // 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. -// ignore_for_file: analyzer_use_new_elements - import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/src/dart/resolver/lexical_lookup.dart'; import 'package:analyzer/src/generated/resolver.dart'; @@ -51,11 +49,11 @@ class ThisLookup { ); } - var getterElement = propertyResult.getter; + var getterElement = propertyResult.getter2; if (getterElement != null) { return LexicalLookupResult(requested: getterElement); } else { - return LexicalLookupResult(recovery: propertyResult.setter); + return LexicalLookupResult(recovery: propertyResult.setter2); } } @@ -80,11 +78,11 @@ class ThisLookup { nameErrorEntity: node, ); - var setterElement = propertyResult.setter; + var setterElement = propertyResult.setter2; if (setterElement != null) { return LexicalLookupResult(requested: setterElement); } else { - return LexicalLookupResult(recovery: propertyResult.getter); + return LexicalLookupResult(recovery: propertyResult.getter2); } } } diff --git a/pkg/analyzer/lib/src/utilities/extensions/element.dart b/pkg/analyzer/lib/src/utilities/extensions/element.dart index a3662124a75e..0980624c1288 100644 --- a/pkg/analyzer/lib/src/utilities/extensions/element.dart +++ b/pkg/analyzer/lib/src/utilities/extensions/element.dart @@ -51,7 +51,7 @@ extension ConstructorElement2Extension on ConstructorElement2 { if (this case ConstructorMember member) { return member; } - return baseElement.firstFragment as ConstructorElement; + return (this as ConstructorElementImpl2).lastFragment; } } @@ -142,46 +142,46 @@ extension Element2OrNullExtension on Element2? { switch (self) { case null: return null; - case ConstructorElementImpl2(): - return self.firstFragment as Element; + case ConstructorElementImpl2 element2: + return element2.asElement; case DynamicElementImpl2(): return self.firstFragment; - case ExecutableMember(): - return self.declaration as Element; - case ExtensionElementImpl2(): - return self.firstFragment as Element; - case FieldElementImpl2(): - return self.firstFragment as Element; + case ExecutableMember element2: + return element2.asElement; + case ExtensionElementImpl2 element2: + return element2.asElement; + case FieldElementImpl2 element2: + return element2.asElement; case FieldMember(): return self.declaration as Element; case FormalParameterElement element2: return element2.asElement; - case GetterElementImpl(): - return self.firstFragment as Element; + case GetterElementImpl element2: + return element2.asElement; case LabelElementImpl2 element2: return element2.asElement; case LibraryElementImpl(): return self as Element; case LibraryImportElementImpl(): return self as Element; - case LocalFunctionElementImpl(): - return self.wrappedElement as Element; + case LocalFunctionElementImpl element2: + return element2.asElement; case LocalVariableElementImpl2(): return self.wrappedElement as Element; - case MethodElementImpl2(): - return self.firstFragment as Element; + case MethodElementImpl2 element2: + return element2.asElement; case MultiplyDefinedElementImpl2 element2: return element2.asElement; case NeverElementImpl2(): return NeverElementImpl.instance; case PrefixElement2 element2: return element2.asElement; - case SetterElementImpl(): - return self.firstFragment as Element; - case TopLevelFunctionElementImpl(): - return self.firstFragment as Element; - case TopLevelVariableElementImpl2(): - return self.firstFragment as Element; + case SetterElementImpl element2: + return element2.asElement; + case TopLevelFunctionElementImpl element2: + return element2.asElement; + case TopLevelVariableElementImpl2 element2: + return element2.asElement; case TypeDefiningElement2(): return self.firstFragment as Element; default: @@ -454,7 +454,7 @@ extension MethodElement2Extension on MethodElement2 { if (this case MethodMember member) { return member; } - return baseElement.firstFragment as MethodElement; + return (this as MethodElementImpl2).lastFragment; } } @@ -511,7 +511,7 @@ extension PropertyAccessorElement2Extension on PropertyAccessorElement2 { if (this case PropertyAccessorMember member) { return member; } - return firstFragment as PropertyAccessorElement; + return (this as PropertyAccessorElementImpl2).lastFragment; } } @@ -527,7 +527,7 @@ extension PropertyAccessorElementExtension on PropertyAccessorElement { extension TopLevelFunctionElementExtension on TopLevelFunctionElement { FunctionElement get asElement { - return firstFragment as FunctionElement; + return (this as TopLevelFunctionElementImpl).lastFragment; } }