Skip to content

Commit 6e3c85a

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate NamedTypeBuilder.
Change-Id: Ib6a775a52f409d2f657f2877556b44e2b9c5b788 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/406220 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Phil Quitslund <[email protected]>
1 parent efee57c commit 6e3c85a

File tree

5 files changed

+69
-58
lines changed

5 files changed

+69
-58
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4555,6 +4555,13 @@ class FormalParameterElementImpl extends PromotableElementImpl2
45554555
}
45564556
}
45574557

4558+
/// Creates a synthetic parameter with [name], [type] and [parameterKind].
4559+
factory FormalParameterElementImpl.synthetic(
4560+
String? name, DartType type, ParameterKind parameterKind) {
4561+
var fragment = ParameterElementImpl.synthetic(name, type, parameterKind);
4562+
return FormalParameterElementImpl(fragment);
4563+
}
4564+
45584565
@override
45594566
FormalParameterElement get baseElement => this;
45604567

@@ -11234,6 +11241,13 @@ class TypeAliasElementImpl2 extends TypeDefiningElementImpl2
1123411241
@override
1123511242
TypeImpl get aliasedType => firstFragment.aliasedType;
1123611243

11244+
set aliasedType(TypeImpl value) {
11245+
firstFragment.aliasedType = value;
11246+
}
11247+
11248+
/// The aliased type, might be `null` if not yet linked.
11249+
DartType? get aliasedTypeRaw => firstFragment.aliasedTypeRaw;
11250+
1123711251
@override
1123811252
TypeAliasElementImpl2 get baseElement => this;
1123911253

@@ -11510,6 +11524,11 @@ class TypeParameterElementImpl2 extends TypeDefiningElementImpl2
1151011524
@override
1151111525
TypeImpl? get boundShared => bound;
1151211526

11527+
/// The default value of the type parameter. It is used to provide the
11528+
/// corresponding missing type argument in type annotations and as the
11529+
/// fall-back type value in type inference.
11530+
DartType? get defaultType => firstFragment.defaultType;
11531+
1151311532
@override
1151411533
List<TypeParameterElementImpl> get fragments {
1151511534
return [

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ abstract class _TypeSubstitutor
607607
return NamedTypeBuilder(
608608
type.linker,
609609
type.typeSystem,
610-
type.element,
610+
type.element3,
611611
arguments,
612612
type.nullabilitySuffix,
613613
);

pkg/analyzer/lib/src/summary2/named_type_builder.dart

Lines changed: 40 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
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';
8-
import 'package:analyzer/dart/element/element.dart';
96
import 'package:analyzer/dart/element/element2.dart';
107
import 'package:analyzer/dart/element/nullability_suffix.dart';
118
import 'package:analyzer/dart/element/type.dart';
@@ -19,7 +16,6 @@ import 'package:analyzer/src/dart/element/type_visitor.dart';
1916
import 'package:analyzer/src/summary2/link.dart';
2017
import 'package:analyzer/src/summary2/type_builder.dart';
2118
import 'package:analyzer/src/utilities/extensions/collection.dart';
22-
import 'package:analyzer/src/utilities/extensions/element.dart';
2319

2420
/// The type builder for a [NamedType].
2521
class NamedTypeBuilder extends TypeBuilder {
@@ -32,7 +28,7 @@ class NamedTypeBuilder extends TypeBuilder {
3228
final TypeSystemImpl typeSystem;
3329

3430
@override
35-
final Element element;
31+
final Element2 element3;
3632

3733
final List<DartType> arguments;
3834

@@ -50,15 +46,15 @@ class NamedTypeBuilder extends TypeBuilder {
5046
/// and set for the [node].
5147
TypeImpl? _type;
5248

53-
NamedTypeBuilder(this.linker, this.typeSystem, this.element, this.arguments,
49+
NamedTypeBuilder(this.linker, this.typeSystem, this.element3, this.arguments,
5450
this.nullabilitySuffix,
5551
{this.node});
5652

5753
factory NamedTypeBuilder.of(
5854
Linker linker,
5955
TypeSystemImpl typeSystem,
6056
NamedTypeImpl node,
61-
Element element,
57+
Element2 element,
6258
NullabilitySuffix nullabilitySuffix,
6359
) {
6460
List<DartType> arguments;
@@ -85,21 +81,16 @@ class NamedTypeBuilder extends TypeBuilder {
8581
return NamedTypeBuilder(
8682
linker,
8783
typeSystem,
88-
element.asElement!,
84+
element,
8985
arguments,
9086
nullabilitySuffix,
9187
node: node,
9288
);
9389
}
9490

95-
@override
96-
Element2 get element3 {
97-
return element.asElement2!;
98-
}
99-
10091
// TODO(scheglov): Only when enabled both in the element, and target?
10192
bool get _isNonFunctionTypeAliasesEnabled {
102-
return element.library!.featureSet.isEnabled(
93+
return element3.library2!.featureSet.isEnabled(
10394
Feature.nonfunction_type_aliases,
10495
);
10596
}
@@ -120,28 +111,28 @@ class NamedTypeBuilder extends TypeBuilder {
120111
return _type!;
121112
}
122113

123-
var element = this.element;
124-
if (element is InterfaceElementImpl) {
125-
var parameters = element.typeParameters;
114+
var element3 = this.element3;
115+
if (element3 is InterfaceElementImpl2) {
116+
var parameters = element3.typeParameters2;
126117
var arguments = _buildArguments(parameters);
127-
_type = element.instantiate(
118+
_type = element3.instantiate(
128119
typeArguments: arguments,
129120
nullabilitySuffix: nullabilitySuffix,
130121
);
131-
} else if (element is TypeAliasElementImpl) {
132-
var aliasedType = _getAliasedType(element);
133-
var parameters = element.typeParameters;
122+
} else if (element3 is TypeAliasElementImpl2) {
123+
var aliasedType = _getAliasedType(element3);
124+
var parameters = element3.typeParameters2;
134125
var arguments = _buildArguments(parameters);
135-
element.aliasedType = aliasedType;
136-
_type = element.instantiate(
126+
element3.aliasedType = aliasedType as TypeImpl;
127+
_type = element3.instantiate(
137128
typeArguments: arguments,
138129
nullabilitySuffix: nullabilitySuffix,
139130
);
140-
} else if (element is NeverElementImpl) {
131+
} else if (element3 is NeverElementImpl2) {
141132
_type = NeverTypeImpl.instance.withNullability(nullabilitySuffix);
142-
} else if (element is TypeParameterElement) {
143-
_type = TypeParameterTypeImpl(
144-
element: element,
133+
} else if (element3 is TypeParameterElement2) {
134+
_type = TypeParameterTypeImpl.v2(
135+
element: element3,
145136
nullabilitySuffix: nullabilitySuffix,
146137
);
147138
} else {
@@ -155,7 +146,7 @@ class NamedTypeBuilder extends TypeBuilder {
155146
@override
156147
String toString() {
157148
var buffer = StringBuffer();
158-
buffer.write(element.displayName);
149+
buffer.write(element3.displayName);
159150
if (arguments.isNotEmpty) {
160151
buffer.write('<');
161152
buffer.write(arguments.join(', '));
@@ -171,11 +162,11 @@ class NamedTypeBuilder extends TypeBuilder {
171162
}
172163

173164
return NamedTypeBuilder(
174-
linker, typeSystem, element, arguments, nullabilitySuffix,
165+
linker, typeSystem, element3, arguments, nullabilitySuffix,
175166
node: node);
176167
}
177168

178-
DartType _buildAliasedType(TypeAnnotation? node) {
169+
TypeImpl _buildAliasedType(TypeAnnotation? node) {
179170
if (_isNonFunctionTypeAliasesEnabled) {
180171
if (node != null) {
181172
return _buildType(node.typeOrThrow);
@@ -186,9 +177,9 @@ class NamedTypeBuilder extends TypeBuilder {
186177
if (node is GenericFunctionType) {
187178
return _buildType(node.typeOrThrow);
188179
} else {
189-
return FunctionTypeImpl(
190-
typeFormals: const <TypeParameterElement>[],
191-
parameters: const <ParameterElement>[],
180+
return FunctionTypeImpl.v2(
181+
typeParameters: const <TypeParameterElement2>[],
182+
formalParameters: const <FormalParameterElement>[],
192183
returnType: _dynamicType,
193184
nullabilitySuffix: NullabilitySuffix.none,
194185
);
@@ -197,23 +188,23 @@ class NamedTypeBuilder extends TypeBuilder {
197188
}
198189

199190
/// Build arguments that correspond to the type [parameters].
200-
List<DartType> _buildArguments(List<TypeParameterElement> parameters) {
191+
List<DartType> _buildArguments(List<TypeParameterElement2> parameters) {
201192
if (parameters.isEmpty) {
202193
return const <DartType>[];
203194
} else if (arguments.isNotEmpty) {
204195
if (arguments.length == parameters.length) {
205196
return List.generate(arguments.length, (index) {
206197
var type = arguments[index];
207-
return _buildType(type);
198+
return _buildType(type as TypeImpl);
208199
}, growable: false);
209200
} else {
210201
return _listOfDynamic(parameters.length);
211202
}
212203
} else {
213204
return List.generate(parameters.length, (index) {
214-
var parameter = parameters[index] as TypeParameterElementImpl;
205+
var parameter = parameters[index] as TypeParameterElementImpl2;
215206
var defaultType = parameter.defaultType!;
216-
return _buildType(defaultType);
207+
return _buildType(defaultType as TypeImpl);
217208
}, growable: false);
218209
}
219210
}
@@ -235,7 +226,7 @@ class NamedTypeBuilder extends TypeBuilder {
235226
}
236227
}
237228

238-
FunctionType _buildFunctionType({
229+
FunctionTypeImpl _buildFunctionType({
239230
required TypeParameterList? typeParameterList,
240231
required TypeAnnotation? returnTypeNode,
241232
required FormalParameterList parameterList,
@@ -245,9 +236,9 @@ class NamedTypeBuilder extends TypeBuilder {
245236
var typeParameters = _typeParameters(typeParameterList);
246237
var formalParameters = _formalParameters(parameterList);
247238

248-
return FunctionTypeImpl(
249-
typeFormals: typeParameters,
250-
parameters: formalParameters,
239+
return FunctionTypeImpl.v2(
240+
typeParameters: typeParameters,
241+
formalParameters: formalParameters,
251242
returnType: returnType,
252243
nullabilitySuffix: _getNullabilitySuffix(hasQuestion),
253244
);
@@ -261,18 +252,18 @@ class NamedTypeBuilder extends TypeBuilder {
261252
}
262253
}
263254

264-
List<ParameterElementImpl> _formalParameters(FormalParameterList node) {
255+
List<FormalParameterElementImpl> _formalParameters(FormalParameterList node) {
265256
return node.parameters.asImpl.map((parameter) {
266-
return ParameterElementImpl.synthetic(
257+
return FormalParameterElementImpl.synthetic(
267258
parameter.name?.lexeme ?? '',
268259
_buildFormalParameterType(parameter),
269260
parameter.kind,
270261
);
271262
}).toFixedList();
272263
}
273264

274-
DartType _getAliasedType(TypeAliasElementImpl element) {
275-
var typedefNode = linker.getLinkingNode(element);
265+
DartType _getAliasedType(TypeAliasElementImpl2 element) {
266+
var typedefNode = linker.getLinkingNode2(element);
276267

277268
// If the element is not being linked, the types have already been built.
278269
if (typedefNode == null) {
@@ -315,7 +306,7 @@ class NamedTypeBuilder extends TypeBuilder {
315306
}
316307

317308
/// If the [type] is a [TypeBuilder], build it; otherwise return as is.
318-
static DartType _buildType(DartType type) {
309+
static TypeImpl _buildType(TypeImpl type) {
319310
if (type is TypeBuilder) {
320311
return type.build();
321312
} else {
@@ -327,13 +318,13 @@ class NamedTypeBuilder extends TypeBuilder {
327318
return List<DartType>.filled(length, _dynamicType);
328319
}
329320

330-
static List<TypeParameterElement> _typeParameters(TypeParameterList? node) {
321+
static List<TypeParameterElement2> _typeParameters(TypeParameterList? node) {
331322
if (node != null) {
332323
return node.typeParameters
333-
.map<TypeParameterElement>((p) => p.declaredElement!)
324+
.map<TypeParameterElement2>((p) => p.declaredFragment!.element)
334325
.toFixedList();
335326
} else {
336-
return const <TypeParameterElement>[];
327+
return const <TypeParameterElement2>[];
337328
}
338329
}
339330
}

pkg/analyzer/lib/src/summary2/reference_resolver.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import 'package:analyzer/dart/ast/visitor.dart';
88
import 'package:analyzer/dart/element/element.dart';
9+
import 'package:analyzer/dart/element/element2.dart';
910
import 'package:analyzer/dart/element/nullability_suffix.dart';
1011
import 'package:analyzer/dart/element/scope.dart';
1112
import 'package:analyzer/src/dart/ast/ast.dart';
@@ -19,7 +20,6 @@ import 'package:analyzer/src/summary2/linking_node_scope.dart';
1920
import 'package:analyzer/src/summary2/named_type_builder.dart';
2021
import 'package:analyzer/src/summary2/record_type_builder.dart';
2122
import 'package:analyzer/src/summary2/types_builder.dart';
22-
import 'package:analyzer/src/utilities/extensions/element.dart';
2323

2424
/// Recursive visitor of LinkedNodes that resolves explicit type annotations
2525
/// in outlines. This includes resolving element references in identifiers
@@ -395,7 +395,7 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
395395

396396
@override
397397
void visitNamedType(covariant NamedTypeImpl node) {
398-
Element? element;
398+
Element2? element;
399399
var importPrefix = node.importPrefix;
400400
if (importPrefix != null) {
401401
var prefixToken = importPrefix.name;
@@ -405,7 +405,7 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
405405

406406
if (prefixElement is PrefixElement) {
407407
var name = node.name2.lexeme;
408-
element = prefixElement.scope.lookup(name).getter;
408+
element = prefixElement.scope.lookup(name).getter2;
409409
}
410410
} else {
411411
var name = node.name2.lexeme;
@@ -415,17 +415,17 @@ class ReferenceResolver extends ThrowingAstVisitor<void> {
415415
return;
416416
}
417417

418-
element = scope.lookup(name).getter;
418+
element = scope.lookup(name).getter2;
419419
}
420-
node.element2 = element.asElement2;
420+
node.element2 = element;
421421

422422
node.typeArguments?.accept(this);
423423

424424
var nullabilitySuffix = _getNullabilitySuffix(node.question != null);
425425
if (element == null) {
426426
node.type = InvalidTypeImpl.instance;
427-
} else if (element is TypeParameterElement) {
428-
node.type = TypeParameterTypeImpl(
427+
} else if (element is TypeParameterElement2) {
428+
node.type = TypeParameterTypeImpl.v2(
429429
element: element,
430430
nullabilitySuffix: nullabilitySuffix,
431431
);

pkg/analyzer/lib/src/summary2/variance_builder.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import 'package:analyzer/src/summary2/function_type_builder.dart';
1414
import 'package:analyzer/src/summary2/link.dart';
1515
import 'package:analyzer/src/summary2/named_type_builder.dart';
1616
import 'package:analyzer/src/summary2/record_type_builder.dart';
17+
import 'package:analyzer/src/utilities/extensions/element.dart';
1718

1819
class VarianceBuilder {
1920
final Linker _linker;
@@ -64,7 +65,7 @@ class VarianceBuilder {
6465
return Variance.unrelated;
6566
}
6667
} else if (type is NamedTypeBuilder) {
67-
var element = type.element;
68+
var element = type.element3.asElement;
6869
var arguments = type.arguments;
6970
if (element is InterfaceElement) {
7071
var result = Variance.unrelated;

0 commit comments

Comments
 (0)