Skip to content

Commit e42a919

Browse files
committed
Elements. Migrate FunctionExpressionResolver.
Change-Id: I447bd537d1c70351f659efd12f5e404e7251a7c2 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/406401 Reviewed-by: Keerti Parthasarathy <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 6000ee0 commit e42a919

File tree

4 files changed

+46
-36
lines changed

4 files changed

+46
-36
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18119,8 +18119,7 @@ final class TypeParameterImpl extends DeclarationImpl implements TypeParameter {
1811918119

1812018120
@experimental
1812118121
@override
18122-
TypeParameterFragment? get declaredFragment =>
18123-
declaredElement as TypeParameterFragment?;
18122+
TypeParameterElementImpl? get declaredFragment => declaredElement;
1812418123

1812518124
@override
1812618125
Token get endToken {

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11563,7 +11563,7 @@ class TypeParameterElementImpl2 extends TypeDefiningElementImpl2
1156311563
}
1156411564

1156511565
@override
11566-
TypeParameterType instantiate({
11566+
TypeParameterTypeImpl instantiate({
1156711567
required NullabilitySuffix nullabilitySuffix,
1156811568
}) {
1156911569
return TypeParameterTypeImpl.v2(

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

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
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-
7-
import 'package:analyzer/dart/element/element.dart';
85
import 'package:analyzer/dart/element/nullability_suffix.dart';
96
import 'package:analyzer/dart/element/type.dart';
107
import 'package:analyzer/src/dart/ast/ast.dart';
@@ -34,15 +31,15 @@ class FunctionExpressionResolver {
3431
.executableDeclaration_enter(node, node.parameters, isClosure: true);
3532
}
3633

37-
bool wasFunctionTypeSupplied = contextType is FunctionType;
34+
bool wasFunctionTypeSupplied = contextType is FunctionTypeImpl;
3835
node.wasFunctionTypeSupplied = wasFunctionTypeSupplied;
3936
DartType? imposedType;
4037
if (wasFunctionTypeSupplied) {
4138
var instantiatedType = _matchTypeParameters(
4239
node.typeParameters,
4340
contextType,
4441
);
45-
if (instantiatedType is FunctionType) {
42+
if (instantiatedType is FunctionTypeImpl) {
4643
_inferFormalParameters(node.parameters, instantiatedType);
4744
var returnType = instantiatedType.returnType;
4845
if (!(returnType is DynamicType || returnType is UnknownInferredType)) {
@@ -82,7 +79,7 @@ class FunctionExpressionResolver {
8279
return;
8380
}
8481

85-
void inferType(ParameterElementImpl p, TypeImpl inferredType) {
82+
void inferType(FormalParameterElementImpl p, TypeImpl inferredType) {
8683
// Check that there is no declared type, and that we have not already
8784
// inferred a type in some fashion.
8885
if (p.hasImplicitType && p.type is DynamicType) {
@@ -98,39 +95,45 @@ class FunctionExpressionResolver {
9895
inferredType = _typeSystem.objectQuestion;
9996
}
10097
if (inferredType is! DynamicType) {
101-
p.type = inferredType;
98+
p.firstFragment.type = inferredType;
10299
}
103100
}
104101
}
105102

106-
var parameters = node.parameterElements.nonNulls;
103+
var nodeParameterFragments = node.parameterFragments.nonNulls;
107104
{
108-
Iterator<ParameterElement> positional =
109-
parameters.where((p) => p.isPositional).iterator;
110-
Iterator<ParameterElement> fnPositional =
111-
contextType.parameters.where((p) => p.isPositional).iterator;
112-
while (positional.moveNext() && fnPositional.moveNext()) {
105+
var nodePositional = nodeParameterFragments
106+
.map((fragment) => fragment.element)
107+
.where((element) => element.isPositional)
108+
.iterator;
109+
var contextPositional = contextType.formalParameters
110+
.where((element) => element.isPositional)
111+
.iterator;
112+
while (nodePositional.moveNext() && contextPositional.moveNext()) {
113113
inferType(
114-
positional.current as ParameterElementImpl,
115-
// TODO(paulberry): eliminate this cast by changing the type of
116-
// `contextType` to `FunctionTypeImpl`.
117-
fnPositional.current.type as TypeImpl);
114+
nodePositional.current as FormalParameterElementImpl,
115+
// TODO(paulberry): eliminate this cast by changing the type of
116+
// `contextType` to `FunctionTypeImpl`.
117+
contextPositional.current.type as TypeImpl,
118+
);
118119
}
119120
}
120121

121122
{
122-
Map<String, DartType> namedParameterTypes =
123-
contextType.namedParameterTypes;
124-
Iterable<ParameterElement> named = parameters.where((p) => p.isNamed);
125-
for (var p in named) {
126-
if (!namedParameterTypes.containsKey(p.name)) {
123+
var contextNamedTypes = contextType.namedParameterTypes;
124+
var nodeNamed = nodeParameterFragments
125+
.map((fragment) => fragment.element)
126+
.where((element) => element.isNamed);
127+
for (var element in nodeNamed) {
128+
if (!contextNamedTypes.containsKey(element.name3)) {
127129
continue;
128130
}
129131
inferType(
130-
p as ParameterElementImpl,
131-
// TODO(paulberry): eliminate this cast by changing the type of
132-
// `contextType` to `FunctionTypeImpl`.
133-
namedParameterTypes[p.name]! as TypeImpl);
132+
element as FormalParameterElementImpl,
133+
// TODO(paulberry): eliminate this cast by changing the type of
134+
// `contextType` to `FunctionTypeImpl`.
135+
contextNamedTypes[element.name3]! as TypeImpl,
136+
);
134137
}
135138
}
136139
}
@@ -140,32 +143,35 @@ class FunctionExpressionResolver {
140143
///
141144
/// Return `null` is the number of element in [typeParameterList] is not
142145
/// the same as the number of type parameters in the [type].
143-
FunctionType? _matchTypeParameters(
144-
TypeParameterList? typeParameterList, FunctionType type) {
146+
FunctionTypeImpl? _matchTypeParameters(
147+
TypeParameterListImpl? typeParameterList, FunctionTypeImpl type) {
145148
if (typeParameterList == null) {
146-
if (type.typeFormals.isEmpty) {
149+
if (type.typeParameters.isEmpty) {
147150
return type;
148151
}
149152
return null;
150153
}
151154

152155
var typeParameters = typeParameterList.typeParameters;
153-
if (typeParameters.length != type.typeFormals.length) {
156+
if (typeParameters.length != type.typeParameters.length) {
154157
return null;
155158
}
156159

157160
return type.instantiate(typeParameters.map((typeParameter) {
158-
return typeParameter.declaredElement!.instantiate(
161+
return typeParameter.declaredFragment!.element.instantiate(
159162
nullabilitySuffix: NullabilitySuffix.none,
160163
);
161164
}).toList());
162165
}
163166

164167
void _resolve2(FunctionExpressionImpl node, DartType? imposedType) {
165-
var functionElement = node.declaredElement as ExecutableElementImpl;
168+
var functionElement =
169+
node.declaredFragment!.element as ExecutableElementImpl2;
166170

167171
if (_shouldUpdateReturnType(node)) {
168-
functionElement.returnType = imposedType ?? DynamicTypeImpl.instance;
172+
var firstFragment =
173+
functionElement.firstFragment as ExecutableElementImpl;
174+
firstFragment.returnType = imposedType ?? DynamicTypeImpl.instance;
169175
}
170176

171177
node.recordStaticType(functionElement.type, resolver: _resolver);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,11 @@ class ResolverVisitor extends ThrowingAstVisitor<void>
11971197
return expression;
11981198
}
11991199

1200+
// Don't rewrite function declarations.
1201+
if (expression.parent is FunctionDeclaration) {
1202+
return expression;
1203+
}
1204+
12001205
var staticType = expression.staticType;
12011206
if (staticType is! FunctionTypeImpl || staticType.typeFormals.isEmpty) {
12021207
return expression;

0 commit comments

Comments
 (0)