Skip to content

Commit b4194ea

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate InvocationInferrer.
Change-Id: I55fc394c06669e7d8c0d1ffb1449dd73aa6b4bf2 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/411363 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent db96984 commit b4194ea

File tree

2 files changed

+49
-62
lines changed

2 files changed

+49
-62
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,10 @@ final class AnnotationImpl extends AstNodeImpl implements Annotation {
332332
return element?.asElement2;
333333
}
334334

335+
set element2(Element2? value) {
336+
element = value?.asElement;
337+
}
338+
335339
@override
336340
Token get endToken {
337341
if (arguments case var arguments?) {

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

Lines changed: 45 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
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:_fe_analyzer_shared/src/base/errors.dart';
86
import 'package:_fe_analyzer_shared/src/deferred_function_literal_heuristic.dart';
97
import 'package:_fe_analyzer_shared/src/flow_analysis/flow_analysis.dart';
108
import 'package:_fe_analyzer_shared/src/types/shared_type.dart';
119
import 'package:analyzer/dart/ast/syntactic_entity.dart';
12-
import 'package:analyzer/dart/element/element.dart';
1310
import 'package:analyzer/dart/element/element2.dart';
1411
import 'package:analyzer/dart/element/type.dart';
1512
import 'package:analyzer/src/dart/ast/ast.dart';
@@ -24,7 +21,6 @@ import 'package:analyzer/src/dart/element/type_system.dart';
2421
import 'package:analyzer/src/error/codes.dart';
2522
import 'package:analyzer/src/generated/inference_log.dart';
2623
import 'package:analyzer/src/generated/resolver.dart';
27-
import 'package:analyzer/src/utilities/extensions/element.dart';
2824

2925
Set<Object> _computeExplicitlyTypedParameterSet(
3026
FunctionExpression functionExpression) {
@@ -46,15 +42,12 @@ Set<Object> _computeExplicitlyTypedParameterSet(
4642
/// Given an iterable of parameters, computes a map whose keys are either the
4743
/// parameter name (for named parameters) or the zero-based integer index (for
4844
/// unnamed parameters), and whose values are the parameters themselves.
49-
Map<Object, ParameterElementMixin> _computeParameterMap(
50-
Iterable<ParameterElement> parameters) {
45+
Map<Object, FormalParameterElementMixin> _computeParameterMap(
46+
Iterable<FormalParameterElementMixin> parameters) {
5147
int unnamedParameterIndex = 0;
5248
return {
5349
for (var parameter in parameters)
54-
parameter.isNamed ? parameter.name : unnamedParameterIndex++:
55-
// TODO(paulberry): eliminate this cast by changing the type of
56-
// `FunctionTypeImpl.parameters` to `List<ParameterElementMixin>`.
57-
parameter as ParameterElementMixin
50+
parameter.isNamed ? parameter.name3! : unnamedParameterIndex++: parameter
5851
};
5952
}
6053

@@ -94,16 +87,16 @@ class AnnotationInferrer extends FullInvocationInferrer<AnnotationImpl> {
9487
CompileTimeErrorCode.WRONG_NUMBER_OF_TYPE_ARGUMENTS;
9588

9689
@override
97-
List<ParameterElement>? _storeResult(
90+
List<FormalParameterElement>? _storeResult(
9891
List<DartType>? typeArgumentTypes, FunctionType? invokeType) {
9992
if (invokeType != null) {
100-
var constructorElement = ConstructorMember.from(
101-
node.element as ConstructorElement,
93+
var constructorElement = ConstructorMember.from2(
94+
node.element2 as ConstructorElement2,
10295
invokeType.returnType as InterfaceType,
10396
);
104-
constructorName?.staticElement = constructorElement;
105-
node.element = constructorElement;
106-
return constructorElement.parameters;
97+
constructorName?.element = constructorElement;
98+
node.element2 = constructorElement;
99+
return constructorElement.formalParameters;
107100
}
108101
return null;
109102
}
@@ -183,7 +176,7 @@ abstract class FullInvocationInferrer<Node extends AstNodeImpl>
183176
} else if (typeArgumentList != null) {
184177
if (rawType != null &&
185178
typeArgumentList.arguments.length != rawType.typeFormals.length) {
186-
var typeParameters = rawType.typeFormals;
179+
var typeParameters = rawType.typeParameters;
187180
_reportWrongNumberOfTypeArguments(
188181
typeArgumentList, rawType, typeParameters);
189182
typeArgumentTypes = List.filled(
@@ -253,7 +246,8 @@ abstract class FullInvocationInferrer<Node extends AstNodeImpl>
253246

254247
List<_IdenticalArgumentInfo?>? identicalArgumentInfo =
255248
_isIdentical ? [] : null;
256-
var parameterMap = _computeParameterMap(rawType?.parameters ?? const []);
249+
var parameterMap =
250+
_computeParameterMap(rawType?.formalParameters ?? const []);
257251
var deferredFunctionLiterals = _visitArguments(
258252
parameterMap: parameterMap,
259253
identicalArgumentInfo: identicalArgumentInfo,
@@ -264,7 +258,7 @@ abstract class FullInvocationInferrer<Node extends AstNodeImpl>
264258
for (var stage in _FunctionLiteralDependencies(
265259
resolver.typeSystem,
266260
deferredFunctionLiterals,
267-
rawType?.typeFormals.toSet() ?? const {},
261+
rawType?.typeParameters.toSet() ?? const {},
268262
_computeUndeferredParamInfo(
269263
rawType, parameterMap, deferredFunctionLiterals))
270264
.planReconciliationStages()) {
@@ -293,7 +287,7 @@ abstract class FullInvocationInferrer<Node extends AstNodeImpl>
293287
argumentList.correspondingStaticParameters =
294288
ResolverVisitor.resolveArgumentsToParameters(
295289
argumentList: argumentList,
296-
formalParameters: parameters.map((e) => e.asElement2).toList(),
290+
formalParameters: parameters,
297291
errorReporter: resolver.errorReporter,
298292
);
299293
}
@@ -307,7 +301,7 @@ abstract class FullInvocationInferrer<Node extends AstNodeImpl>
307301
/// parameters that were *not* deferred.
308302
List<_ParamInfo> _computeUndeferredParamInfo(
309303
FunctionType? rawType,
310-
Map<Object, ParameterElementMixin> parameterMap,
304+
Map<Object, FormalParameterElementMixin> parameterMap,
311305
List<_DeferredParamInfo> deferredFunctionLiterals) {
312306
if (rawType == null) return const [];
313307
var parameterKeysAlreadyCovered = {
@@ -324,7 +318,7 @@ abstract class FullInvocationInferrer<Node extends AstNodeImpl>
324318
DartType _refineReturnType(DartType returnType) => returnType;
325319

326320
void _reportWrongNumberOfTypeArguments(TypeArgumentList typeArgumentList,
327-
FunctionType rawType, List<TypeParameterElement> typeParameters) {
321+
FunctionType rawType, List<TypeParameterElement2> typeParameters) {
328322
resolver.errorReporter.atNode(
329323
typeArgumentList,
330324
_wrongNumberOfTypeArgumentsErrorCode,
@@ -336,9 +330,9 @@ abstract class FullInvocationInferrer<Node extends AstNodeImpl>
336330
);
337331
}
338332

339-
List<ParameterElement>? _storeResult(
333+
List<FormalParameterElement>? _storeResult(
340334
List<DartType>? typeArgumentTypes, FunctionTypeImpl? invokeType) {
341-
return invokeType?.parameters;
335+
return invokeType?.formalParameters;
342336
}
343337
}
344338

@@ -388,22 +382,22 @@ class InstanceCreationInferrer
388382

389383
@override
390384
void _reportWrongNumberOfTypeArguments(TypeArgumentList typeArgumentList,
391-
FunctionType rawType, List<TypeParameterElement> typeParameters) {
385+
FunctionType rawType, List<TypeParameterElement2> typeParameters) {
392386
// Error reporting for instance creations is done elsewhere.
393387
}
394388

395389
@override
396-
List<ParameterElement>? _storeResult(
390+
List<FormalParameterElement>? _storeResult(
397391
List<DartType>? typeArgumentTypes, FunctionTypeImpl? invokeType) {
398392
if (invokeType != null) {
399393
var constructedType = invokeType.returnType;
400394
node.constructorName.type.type = constructedType;
401-
var constructorElement = ConstructorMember.from(
402-
node.constructorName.staticElement!,
395+
var constructorElement = ConstructorMember.from2(
396+
node.constructorName.element!,
403397
constructedType as InterfaceType,
404398
);
405-
node.constructorName.staticElement = constructorElement;
406-
return constructorElement.parameters;
399+
node.constructorName.element = constructorElement;
400+
return constructorElement.formalParameters;
407401
}
408402
return null;
409403
}
@@ -429,7 +423,7 @@ abstract class InvocationExpressionInferrer<
429423
TypeArgumentListImpl? get _typeArguments => node.typeArguments;
430424

431425
@override
432-
List<ParameterElement>? _storeResult(
426+
List<FormalParameterElement>? _storeResult(
433427
List<DartType>? typeArgumentTypes, FunctionTypeImpl? invokeType) {
434428
node.typeArgumentTypes = typeArgumentTypes;
435429
node.staticInvokeType = invokeType ?? DynamicTypeImpl.instance;
@@ -467,7 +461,8 @@ class InvocationInferrer<Node extends AstNodeImpl> {
467461
/// arguments not applied yet).
468462
void resolveInvocation({required FunctionTypeImpl? rawType}) {
469463
var deferredFunctionLiterals = _visitArguments(
470-
parameterMap: _computeParameterMap(rawType?.parameters ?? const []));
464+
parameterMap:
465+
_computeParameterMap(rawType?.formalParameters ?? const []));
471466
if (deferredFunctionLiterals != null) {
472467
_resolveDeferredFunctionLiterals(
473468
deferredFunctionLiterals: deferredFunctionLiterals);
@@ -528,7 +523,7 @@ class InvocationInferrer<Node extends AstNodeImpl> {
528523
}
529524
if (parameter != null) {
530525
inferrer?.constrainArgument(
531-
argument.typeOrThrow, parameter.type, parameter.name,
526+
argument.typeOrThrow, parameter.type, parameter.name3!,
532527
nodeForTesting: node);
533528
}
534529
}
@@ -538,7 +533,7 @@ class InvocationInferrer<Node extends AstNodeImpl> {
538533
/// be deferred due to the `inference-update-1` feature, a list of them is
539534
/// returned.
540535
List<_DeferredParamInfo>? _visitArguments(
541-
{required Map<Object, ParameterElementMixin> parameterMap,
536+
{required Map<Object, FormalParameterElementMixin> parameterMap,
542537
List<_IdenticalArgumentInfo?>? identicalArgumentInfo,
543538
Substitution? substitution,
544539
GenericInferrer? inferrer}) {
@@ -551,7 +546,7 @@ class InvocationInferrer<Node extends AstNodeImpl> {
551546
for (int i = 0; i < arguments.length; i++) {
552547
var argument = arguments[i];
553548
Expression value;
554-
ParameterElementMixin? parameter;
549+
FormalParameterElementMixin? parameter;
555550
Object parameterKey;
556551
if (argument is NamedExpressionImpl) {
557552
value = argument.expression;
@@ -593,7 +588,7 @@ class InvocationInferrer<Node extends AstNodeImpl> {
593588
}
594589
if (parameter != null) {
595590
inferrer?.constrainArgument(
596-
argument.typeOrThrow, parameter.type, parameter.name,
591+
argument.typeOrThrow, parameter.type, parameter.name3!,
597592
nodeForTesting: node);
598593
}
599594
}
@@ -626,8 +621,8 @@ class MethodInvocationInferrer
626621

627622
@override
628623
bool get _isIdentical {
629-
var invokedMethod = node.methodName.staticElement;
630-
return invokedMethod is FunctionElement &&
624+
var invokedMethod = node.methodName.element;
625+
return invokedMethod is TopLevelFunctionElement &&
631626
invokedMethod.isDartCoreIdentical &&
632627
node.argumentList.arguments.length == 2;
633628
}
@@ -677,10 +672,10 @@ class _DeferredParamInfo extends _ParamInfo {
677672
}
678673

679674
class _FunctionLiteralDependencies extends FunctionLiteralDependencies<
680-
TypeParameterElement, _ParamInfo, _DeferredParamInfo> {
675+
TypeParameterElement2, _ParamInfo, _DeferredParamInfo> {
681676
final TypeSystemImpl _typeSystem;
682677

683-
final Set<TypeParameterElement> _typeVariables;
678+
final Set<TypeParameterElement2> _typeVariables;
684679

685680
_FunctionLiteralDependencies(
686681
this._typeSystem,
@@ -689,26 +684,19 @@ class _FunctionLiteralDependencies extends FunctionLiteralDependencies<
689684
List<_ParamInfo> undeferredParamInfo)
690685
: super(deferredParamInfo, _typeVariables, undeferredParamInfo);
691686

692-
Set<TypeParameterElement2> get _typeVariables2 {
693-
return _typeVariables.map((e) => e.asElement2).toSet();
694-
}
695-
696687
@override
697-
Iterable<TypeParameterElement> typeVarsFreeInParamParams(
688+
Iterable<TypeParameterElement2> typeVarsFreeInParamParams(
698689
_DeferredParamInfo paramInfo) {
699690
var type = paramInfo.parameter?.type;
700691
if (type is FunctionTypeImpl) {
701-
var parameterMap = _computeParameterMap(type.parameters);
692+
var parameterMap = _computeParameterMap(type.formalParameters);
702693
var explicitlyTypedParameters =
703694
_computeExplicitlyTypedParameterSet(paramInfo.value);
704-
Set<TypeParameterElement> result = {};
695+
Set<TypeParameterElement2> result = {};
705696
for (var entry in parameterMap.entries) {
706697
if (explicitlyTypedParameters.contains(entry.key)) continue;
707-
result.addAll(_typeSystem
708-
.getFreeParameters2(entry.value.type,
709-
candidates: _typeVariables2)
710-
?.map((e) => e.asElement)
711-
.toList() ??
698+
result.addAll(_typeSystem.getFreeParameters2(entry.value.type,
699+
candidates: _typeVariables) ??
712700
const []);
713701
}
714702
return result;
@@ -718,20 +706,15 @@ class _FunctionLiteralDependencies extends FunctionLiteralDependencies<
718706
}
719707

720708
@override
721-
Iterable<TypeParameterElement> typeVarsFreeInParamReturns(
709+
Iterable<TypeParameterElement2> typeVarsFreeInParamReturns(
722710
_ParamInfo paramInfo) {
723711
var type = paramInfo.parameter?.type;
724712
if (type is FunctionTypeImpl) {
725-
return _typeSystem
726-
.getFreeParameters2(type.returnType, candidates: _typeVariables2)
727-
?.map((e) => e.asElement)
728-
.toList() ??
713+
return _typeSystem.getFreeParameters2(type.returnType,
714+
candidates: _typeVariables) ??
729715
const [];
730716
} else if (type != null) {
731-
return _typeSystem
732-
.getFreeParameters2(type, candidates: _typeVariables2)
733-
?.map((e) => e.asElement)
734-
.toList() ??
717+
return _typeSystem.getFreeParameters2(type, candidates: _typeVariables) ??
735718
const [];
736719
} else {
737720
return const [];
@@ -759,7 +742,7 @@ class _IdenticalArgumentInfo {
759742
class _ParamInfo {
760743
/// The function parameter corresponding to the argument, or `null` if we are
761744
/// resolving a dynamic invocation.
762-
final ParameterElementMixin? parameter;
745+
final FormalParameterElementMixin? parameter;
763746

764747
_ParamInfo(this.parameter);
765748
}

0 commit comments

Comments
 (0)