Skip to content

Commit 3672b4e

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate NamedTypeResolver.
Change-Id: I310115b805f97bfdb384cb14b2bf9ab7ef0c898f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/408920 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent e6df549 commit 3672b4e

File tree

6 files changed

+73
-65
lines changed

6 files changed

+73
-65
lines changed

pkg/analyzer/lib/src/dart/ast/ast.dart

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10004,15 +10004,11 @@ final class ImportPrefixReferenceImpl extends AstNodeImpl
1000410004
@experimental
1000510005
@override
1000610006
Element2? get element2 {
10007-
var element = this.element;
10008-
if (element case PrefixElementImpl element) {
10009-
return element.element2;
10010-
} else if (element case Fragment fragment) {
10011-
return fragment.element;
10012-
} else if (element case Element2 element) {
10013-
return element;
10014-
}
10015-
return null;
10007+
return element?.asElement2;
10008+
}
10009+
10010+
set element2(Element2? value) {
10011+
element = value?.asElement;
1001610012
}
1001710013

1001810014
@override

pkg/analyzer/lib/src/dart/resolver/named_type_resolver.dart

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// ignore_for_file: analyzer_use_new_elements
6-
75
import 'package:analyzer/dart/analysis/features.dart';
86
import 'package:analyzer/dart/ast/token.dart';
9-
import 'package:analyzer/dart/element/element.dart';
7+
import 'package:analyzer/dart/element/element2.dart';
108
import 'package:analyzer/dart/element/scope.dart';
119
import 'package:analyzer/dart/element/type.dart';
1210
import 'package:analyzer/error/error.dart';
@@ -22,7 +20,6 @@ import 'package:analyzer/src/dart/resolver/flow_analysis_visitor.dart';
2220
import 'package:analyzer/src/diagnostic/diagnostic_factory.dart';
2321
import 'package:analyzer/src/error/codes.dart';
2422
import 'package:analyzer/src/generated/scope_helpers.dart';
25-
import 'package:analyzer/src/utilities/extensions/element.dart';
2623

2724
/// Helper for resolving types.
2825
///
@@ -41,7 +38,7 @@ class NamedTypeResolver with ScopeHelpers {
4138

4239
/// If not `null`, the element of the [ClassDeclaration], or the
4340
/// [ClassTypeAlias] being resolved.
44-
InterfaceElementImpl? enclosingClass;
41+
InterfaceElementImpl2? enclosingClass;
4542

4643
/// If not `null`, a direct child of an [ExtendsClause], [WithClause],
4744
/// or [ImplementsClause].
@@ -74,9 +71,9 @@ class NamedTypeResolver with ScopeHelpers {
7471
}) : typeSystem = libraryElement.typeSystem;
7572

7673
bool get _genericMetadataIsEnabled =>
77-
enclosingClass!.library.featureSet.isEnabled(Feature.generic_metadata);
74+
enclosingClass!.library2.featureSet.isEnabled(Feature.generic_metadata);
7875

79-
bool get _inferenceUsingBoundsIsEnabled => enclosingClass!.library.featureSet
76+
bool get _inferenceUsingBoundsIsEnabled => enclosingClass!.library2.featureSet
8077
.isEnabled(Feature.inference_using_bounds);
8178

8279
/// Resolve the given [NamedType] - set its element and static type. Only the
@@ -92,16 +89,16 @@ class NamedTypeResolver with ScopeHelpers {
9289
if (importPrefix != null) {
9390
var prefixToken = importPrefix.name;
9491
var prefixName = prefixToken.lexeme;
95-
var prefixElement = nameScope.lookup(prefixName).getter;
96-
importPrefix.element = prefixElement;
92+
var prefixElement = nameScope.lookup(prefixName).getter2;
93+
importPrefix.element2 = prefixElement;
9794

9895
if (prefixElement == null) {
9996
_resolveToElement(node, null, dataForTesting: dataForTesting);
10097
return;
10198
}
10299

103-
if (prefixElement is InterfaceElement ||
104-
prefixElement is TypeAliasElement) {
100+
if (prefixElement is InterfaceElement2 ||
101+
prefixElement is TypeAliasElement2) {
105102
_rewriteToConstructorName(
106103
node: node,
107104
importPrefix: importPrefix,
@@ -111,7 +108,7 @@ class NamedTypeResolver with ScopeHelpers {
111108
return;
112109
}
113110

114-
if (prefixElement is PrefixElement) {
111+
if (prefixElement is PrefixElement2) {
115112
var nameToken = node.name2;
116113
var element = _lookupGetter(prefixElement.scope, nameToken);
117114
_resolveToElement(node, element, dataForTesting: dataForTesting);
@@ -171,13 +168,15 @@ class NamedTypeResolver with ScopeHelpers {
171168

172169
/// We are resolving the [NamedType] in a redirecting constructor of the
173170
/// [enclosingClass].
174-
InterfaceTypeImpl _inferRedirectedConstructor(InterfaceElementImpl element,
175-
{required TypeConstraintGenerationDataForTesting? dataForTesting,
176-
required AstNodeImpl? nodeForTesting}) {
171+
InterfaceTypeImpl _inferRedirectedConstructor(
172+
InterfaceElementImpl2 element, {
173+
required TypeConstraintGenerationDataForTesting? dataForTesting,
174+
required AstNodeImpl? nodeForTesting,
175+
}) {
177176
if (element == enclosingClass) {
178177
return element.thisType;
179178
} else {
180-
var typeParameters = element.asElement2.typeParameters2;
179+
var typeParameters = element.typeParameters2;
181180
if (typeParameters.isEmpty) {
182181
return element.thisType;
183182
} else {
@@ -202,27 +201,27 @@ class NamedTypeResolver with ScopeHelpers {
202201
}
203202
}
204203

205-
TypeImpl _instantiateElement(NamedTypeImpl node, Element element,
204+
TypeImpl _instantiateElement(NamedTypeImpl node, Element2 element,
206205
{required TypeConstraintGenerationDataForTesting? dataForTesting}) {
207206
var nullability = _getNullability(node);
208207

209208
var argumentList = node.typeArguments;
210209
if (argumentList != null) {
211-
if (element is InterfaceElementImpl) {
210+
if (element is InterfaceElementImpl2) {
212211
var typeArguments = _buildTypeArguments(
213212
node,
214213
argumentList,
215-
element.typeParameters.length,
214+
element.typeParameters2.length,
216215
);
217216
return element.instantiate(
218217
typeArguments: typeArguments,
219218
nullabilitySuffix: nullability,
220219
);
221-
} else if (element is TypeAliasElementImpl) {
220+
} else if (element is TypeAliasElementImpl2) {
222221
var typeArguments = _buildTypeArguments(
223222
node,
224223
argumentList,
225-
element.typeParameters.length,
224+
element.typeParameters2.length,
226225
);
227226
var type = element.instantiate(
228227
typeArguments: typeArguments,
@@ -232,13 +231,13 @@ class NamedTypeResolver with ScopeHelpers {
232231
} else if (_isInstanceCreation(node)) {
233232
_ErrorHelper(errorReporter).reportNewWithNonType(node);
234233
return InvalidTypeImpl.instance;
235-
} else if (element is DynamicElementImpl) {
234+
} else if (element is DynamicElementImpl2) {
236235
_buildTypeArguments(node, argumentList, 0);
237236
return DynamicTypeImpl.instance;
238-
} else if (element is NeverElementImpl) {
237+
} else if (element is NeverElementImpl2) {
239238
_buildTypeArguments(node, argumentList, 0);
240239
return _instantiateElementNever(nullability);
241-
} else if (element is TypeParameterElementImpl) {
240+
} else if (element is TypeParameterElementImpl2) {
242241
_buildTypeArguments(node, argumentList, 0);
243242
return element.instantiate(
244243
nullabilitySuffix: nullability,
@@ -249,10 +248,10 @@ class NamedTypeResolver with ScopeHelpers {
249248
}
250249
}
251250

252-
if (element is InterfaceElementImpl) {
251+
if (element is InterfaceElementImpl2) {
253252
if (identical(node, withClause_namedType)) {
254253
for (var mixin in enclosingClass!.mixins) {
255-
if (mixin.element == element) {
254+
if (mixin.element3 == element) {
256255
return mixin;
257256
}
258257
}
@@ -264,23 +263,23 @@ class NamedTypeResolver with ScopeHelpers {
264263
}
265264

266265
return typeSystem.instantiateInterfaceToBounds2(
267-
element: element.asElement2,
266+
element: element,
268267
nullabilitySuffix: nullability,
269268
);
270-
} else if (element is TypeAliasElementImpl) {
271-
var type = typeSystem.instantiateTypeAliasToBounds(
269+
} else if (element is TypeAliasElementImpl2) {
270+
var type = typeSystem.instantiateTypeAliasToBounds2(
272271
element: element,
273272
nullabilitySuffix: nullability,
274273
);
275274
return _verifyTypeAliasForContext(node, element, type);
276275
} else if (_isInstanceCreation(node)) {
277276
_ErrorHelper(errorReporter).reportNewWithNonType(node);
278277
return InvalidTypeImpl.instance;
279-
} else if (element is DynamicElementImpl) {
278+
} else if (element is DynamicElementImpl2) {
280279
return DynamicTypeImpl.instance;
281-
} else if (element is NeverElementImpl) {
280+
} else if (element is NeverElementImpl2) {
282281
return _instantiateElementNever(nullability);
283-
} else if (element is TypeParameterElementImpl) {
282+
} else if (element is TypeParameterElementImpl2) {
284283
return element.instantiate(
285284
nullabilitySuffix: nullability,
286285
);
@@ -294,18 +293,18 @@ class NamedTypeResolver with ScopeHelpers {
294293
return NeverTypeImpl.instance.withNullability(nullability);
295294
}
296295

297-
Element? _lookupGetter(Scope scope, Token nameToken) {
296+
Element2? _lookupGetter(Scope scope, Token nameToken) {
298297
var scopeLookupResult = scope.lookup(nameToken.lexeme);
299298
reportDeprecatedExportUseGetter(
300299
scopeLookupResult: scopeLookupResult,
301300
nameToken: nameToken,
302301
);
303-
return scopeLookupResult.getter;
302+
return scopeLookupResult.getter2;
304303
}
305304

306-
void _resolveToElement(NamedTypeImpl node, Element? element,
305+
void _resolveToElement(NamedTypeImpl node, Element2? element,
307306
{required TypeConstraintGenerationDataForTesting? dataForTesting}) {
308-
node.element2 = element.asElement2;
307+
node.element2 = element;
309308

310309
if (element == null) {
311310
node.type = InvalidTypeImpl.instance;
@@ -315,7 +314,7 @@ class NamedTypeResolver with ScopeHelpers {
315314
return;
316315
}
317316

318-
if (element is MultiplyDefinedElement) {
317+
if (element is MultiplyDefinedElement2) {
319318
node.type = InvalidTypeImpl.instance;
320319
return;
321320
}
@@ -327,12 +326,12 @@ class NamedTypeResolver with ScopeHelpers {
327326
}
328327

329328
/// We parse `foo.bar` as `prefix.Name` with the expectation that `prefix`
330-
/// will be a [PrefixElement]. But when we resolved the `prefix` it turned
331-
/// out to be a [ClassElement], so it is probably a `Class.constructor`.
329+
/// will be a [PrefixElement2]. But when we resolved the `prefix` it turned
330+
/// out to be a [ClassElement2], so it is probably a `Class.constructor`.
332331
void _rewriteToConstructorName({
333332
required NamedTypeImpl node,
334333
required ImportPrefixReferenceImpl importPrefix,
335-
required Element importPrefixElement,
334+
required Element2 importPrefixElement,
336335
required Token nameToken,
337336
}) {
338337
var constructorName = node.parent;
@@ -356,7 +355,7 @@ class NamedTypeResolver with ScopeHelpers {
356355
name2: importPrefix.name,
357356
typeArguments: null,
358357
question: null,
359-
)..element2 = importPrefixElement.asElement2;
358+
)..element2 = importPrefixElement;
360359
if (identical(node, redirectedConstructor_namedType)) {
361360
redirectedConstructor_namedType = namedType;
362361
}
@@ -421,7 +420,7 @@ class NamedTypeResolver with ScopeHelpers {
421420

422421
TypeImpl _verifyTypeAliasForContext(
423422
NamedType node,
424-
TypeAliasElement element,
423+
TypeAliasElement2 element,
425424
TypeImpl type,
426425
) {
427426
// If a type alias that expands to a type parameter.
@@ -517,7 +516,7 @@ class _ErrorHelper {
517516
return false;
518517
}
519518

520-
void reportNullOrNonTypeElement(NamedType node, Element? element) {
519+
void reportNullOrNonTypeElement(NamedType node, Element2? element) {
521520
if (node.name2.isSynthetic) {
522521
return;
523522
}
@@ -610,14 +609,12 @@ class _ErrorHelper {
610609
return;
611610
}
612611

613-
if (element is LocalVariableElement ||
614-
(element is FunctionElement &&
615-
element.enclosingElement3 is ExecutableElement)) {
612+
if (element is LocalVariableElement2 || element is LocalFunctionElement) {
616613
errorReporter.reportError(
617614
DiagnosticFactory().referencedBeforeDeclaration(
618615
errorReporter.source,
619616
nameToken: node.name2,
620-
element: element!,
617+
element2: element!,
621618
),
622619
);
623620
return;
@@ -659,7 +656,7 @@ class _ErrorHelper {
659656
var firstToken = node.name2;
660657
var importPrefix = node.importPrefix;
661658
if (importPrefix != null) {
662-
if (!skipImportPrefix || importPrefix.element is! PrefixElement) {
659+
if (!skipImportPrefix || importPrefix.element2 is! PrefixElement2) {
663660
firstToken = importPrefix.name;
664661
}
665662
}

pkg/analyzer/lib/src/dart/resolver/resolution_visitor.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
286286
void visitClassDeclaration(covariant ClassDeclarationImpl node) {
287287
ClassElementImpl element = _elementWalker!.getClass();
288288
node.declaredElement = element;
289-
_namedTypeResolver.enclosingClass = element;
289+
_namedTypeResolver.enclosingClass = element.asElement2;
290290

291291
_setOrCreateMetadataElements(element, node.metadata);
292292

@@ -328,7 +328,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
328328
void visitClassTypeAlias(covariant ClassTypeAliasImpl node) {
329329
ClassElementImpl element = _elementWalker!.getClass();
330330
node.declaredElement = element;
331-
_namedTypeResolver.enclosingClass = element;
331+
_namedTypeResolver.enclosingClass = element.asElement2;
332332

333333
_setOrCreateMetadataElements(element, node.metadata);
334334

@@ -519,7 +519,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
519519
void visitEnumDeclaration(covariant EnumDeclarationImpl node) {
520520
EnumElementImpl element = _elementWalker!.getEnum();
521521
node.declaredElement = element;
522-
_namedTypeResolver.enclosingClass = element;
522+
_namedTypeResolver.enclosingClass = element.asElement2;
523523

524524
_setOrCreateMetadataElements(element, node.metadata);
525525

@@ -586,7 +586,7 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
586586
) {
587587
var element = _elementWalker!.getExtensionType();
588588
node.declaredElement = element;
589-
_namedTypeResolver.enclosingClass = element;
589+
_namedTypeResolver.enclosingClass = element.asElement2;
590590

591591
_setOrCreateMetadataElements(element, node.metadata);
592592

@@ -1686,8 +1686,9 @@ class ResolutionVisitor extends RecursiveAstVisitor<void> {
16861686
var type = namedType.typeOrThrow;
16871687

16881688
var enclosingElement = _namedTypeResolver.enclosingClass;
1689-
if (enclosingElement is ExtensionTypeElementImpl) {
1690-
_verifyExtensionElementImplements(enclosingElement, namedType, type);
1689+
if (enclosingElement is ExtensionTypeElementImpl2) {
1690+
_verifyExtensionElementImplements(
1691+
enclosingElement.asElement, namedType, type);
16911692
return;
16921693
}
16931694

pkg/analyzer/lib/src/diagnostic/diagnostic_factory.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import 'package:analyzer/source/source.dart';
1414
import 'package:analyzer/src/dart/ast/ast.dart';
1515
import 'package:analyzer/src/diagnostic/diagnostic.dart';
1616
import 'package:analyzer/src/error/codes.dart';
17+
import 'package:analyzer/src/utilities/extensions/element.dart';
1718

1819
/// A factory used to create diagnostics.
1920
class DiagnosticFactory {
@@ -343,8 +344,9 @@ class DiagnosticFactory {
343344
AnalysisError referencedBeforeDeclaration(
344345
Source source, {
345346
required Token nameToken,
346-
required Element element,
347+
required Element2 element2,
347348
}) {
349+
var element = element2.asElement!;
348350
String name = nameToken.lexeme;
349351
List<DiagnosticMessage>? contextMessages;
350352
int declarationOffset = element.nameOffset;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5182,7 +5182,7 @@ class ErrorVerifier extends RecursiveAstVisitor<void>
51825182
DiagnosticFactory().referencedBeforeDeclaration(
51835183
errorReporter.source,
51845184
nameToken: nameToken,
5185-
element: element,
5185+
element2: element.asElement2!,
51865186
),
51875187
);
51885188
}

0 commit comments

Comments
 (0)