Skip to content

Commit 01f5956

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate NormalizeHelper.
Change-Id: I6a6bed4ef30e17297faa3828a1c55d52df982a8b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/403929 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent a5c3b34 commit 01f5956

File tree

4 files changed

+110
-18
lines changed

4 files changed

+110
-18
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,25 @@ extension ExecutableElementExtensionQuestion on ExecutableElement? {
187187
}
188188
}
189189

190+
extension FormalParameterElementExtension on FormalParameterElement {
191+
/// Returns [FormalParameterElementImpl] with the specified properties
192+
/// replaced.
193+
FormalParameterElementImpl copyWith({
194+
DartType? type,
195+
ParameterKind? kind,
196+
bool? isCovariant,
197+
}) {
198+
var firstFragment = this.firstFragment as ParameterElement;
199+
return FormalParameterElementImpl(
200+
firstFragment.copyWith(
201+
type: type,
202+
kind: kind,
203+
isCovariant: isCovariant,
204+
),
205+
);
206+
}
207+
}
208+
190209
extension InterfaceTypeExtension on InterfaceType {
191210
bool get isDartCoreObjectNone {
192211
return isDartCoreObject && nullabilitySuffix == NullabilitySuffix.none;

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

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +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-
7-
import 'package:analyzer/dart/element/element.dart';
5+
import 'package:analyzer/dart/element/element2.dart';
86
import 'package:analyzer/dart/element/nullability_suffix.dart';
97
import 'package:analyzer/dart/element/type.dart';
108
import 'package:analyzer/src/dart/element/extensions.dart';
@@ -22,7 +20,7 @@ class NormalizeHelper {
2220
final TypeSystemImpl typeSystem;
2321
final TypeProviderImpl typeProvider;
2422

25-
final Set<TypeParameterElement> _typeParameters = {};
23+
final Set<TypeParameterElement2> _typeParameters = {};
2624

2725
NormalizeHelper(this.typeSystem) : typeProvider = typeSystem.typeProvider;
2826

@@ -35,7 +33,7 @@ class NormalizeHelper {
3533
/// * and B1 = NORM(B)
3634
/// * and S1 = NORM(S)
3735
FunctionTypeImpl _functionType(FunctionType functionType) {
38-
var fresh = getFreshTypeParameters(functionType.typeFormals);
36+
var fresh = getFreshTypeParameters2(functionType.typeParameters);
3937
for (var typeParameter in fresh.freshTypeParameters) {
4038
var bound = typeParameter.firstFragment.bound;
4139
if (bound != null) {
@@ -45,9 +43,9 @@ class NormalizeHelper {
4543

4644
functionType = fresh.applyToFunctionType(functionType);
4745

48-
return FunctionTypeImpl(
49-
typeFormals: functionType.typeFormals,
50-
parameters: functionType.parameters.map((e) {
46+
return FunctionTypeImpl.v2(
47+
typeParameters: functionType.typeParameters,
48+
formalParameters: functionType.formalParameters.map((e) {
5149
return e.copyWith(
5250
type: _normalize(e.type),
5351
);
@@ -134,7 +132,7 @@ class NormalizeHelper {
134132

135133
// NORM(C<T0, ..., Tn>) = C<R0, ..., Rn> where Ri is NORM(Ti)
136134
if (T is InterfaceType) {
137-
return T.element.instantiate(
135+
return T.element3.instantiate(
138136
typeArguments: T.typeArguments.map(_normalize).toFixedList(),
139137
nullabilitySuffix: NullabilitySuffix.none,
140138
);
@@ -202,7 +200,7 @@ class NormalizeHelper {
202200
/// NORM(X & T)
203201
/// NORM(X extends T)
204202
DartType _typeParameterType(TypeParameterTypeImpl T) {
205-
var element = T.element;
203+
var element = T.element3;
206204

207205
// NORM(X & T)
208206
var promotedBound = T.promotedBound;
@@ -237,45 +235,45 @@ class NormalizeHelper {
237235

238236
/// NORM(X & T)
239237
/// * let S be NORM(T)
240-
DartType _typeParameterType_promoted(TypeParameterElement X, DartType S) {
238+
DartType _typeParameterType_promoted(TypeParameterElement2 X, DartType S) {
241239
// * if S is Never then Never
242240
if (identical(S, NeverTypeImpl.instance)) {
243241
return NeverTypeImpl.instance;
244242
}
245243

246244
// * if S is a top type then X
247245
if (typeSystem.isTop(S)) {
248-
return X.declaration.instantiate(
246+
return X.instantiate(
249247
nullabilitySuffix: NullabilitySuffix.none,
250248
);
251249
}
252250

253251
// * if S is X then X
254252
if (S is TypeParameterType &&
255253
S.nullabilitySuffix == NullabilitySuffix.none &&
256-
S.element == X.declaration) {
257-
return X.declaration.instantiate(
254+
S.element3 == X) {
255+
return X.instantiate(
258256
nullabilitySuffix: NullabilitySuffix.none,
259257
);
260258
}
261259

262260
// * if S is Object and NORM(B) is Object where B is the bound of X then X
263261
if (S.nullabilitySuffix == NullabilitySuffix.none && S.isDartCoreObject) {
264-
var B = X.declaration.bound;
262+
var B = X.bound;
265263
if (B != null) {
266264
var B_norm = _normalize(B);
267265
if (B_norm.nullabilitySuffix == NullabilitySuffix.none &&
268266
B_norm.isDartCoreObject) {
269-
return X.declaration.instantiate(
267+
return X.instantiate(
270268
nullabilitySuffix: NullabilitySuffix.none,
271269
);
272270
}
273271
}
274272
}
275273

276274
// * else X & S
277-
return TypeParameterTypeImpl(
278-
element: X.declaration,
275+
return TypeParameterTypeImpl.v2(
276+
element: X,
279277
nullabilitySuffix: NullabilitySuffix.none,
280278
promotedBound: S,
281279
);

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,6 +1707,22 @@ class TypeParameterTypeImpl extends TypeImpl implements TypeParameterType {
17071707
super.alias,
17081708
});
17091709

1710+
/// Initialize a newly created type parameter type to be declared by the given
1711+
/// [element] and to have the given name.
1712+
factory TypeParameterTypeImpl.v2({
1713+
required TypeParameterElement2 element,
1714+
required NullabilitySuffix nullabilitySuffix,
1715+
DartType? promotedBound,
1716+
InstantiatedTypeAliasElement? alias,
1717+
}) {
1718+
return TypeParameterTypeImpl(
1719+
element: element.asElement,
1720+
nullabilitySuffix: nullabilitySuffix,
1721+
promotedBound: promotedBound,
1722+
alias: alias,
1723+
);
1724+
}
1725+
17101726
@override
17111727
DartType get bound =>
17121728
promotedBound ?? element.bound ?? DynamicTypeImpl.instance;

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,55 @@ FreshTypeParameters getFreshTypeParameters(
6161
return FreshTypeParameters(freshParameters, substitution);
6262
}
6363

64+
/// Generates a fresh copy of the given type parameters, with their bounds
65+
/// substituted to reference the new parameters.
66+
///
67+
/// The returned object contains the fresh type parameter list as well as a
68+
/// mapping to be used for replacing other types to use the new type parameters.
69+
FreshTypeParameters getFreshTypeParameters2(
70+
List<TypeParameterElement2> typeParameters) {
71+
var freshParameters = List<TypeParameterElementImpl2>.generate(
72+
typeParameters.length,
73+
(i) {
74+
var name = typeParameters[i].name3;
75+
var fragment = TypeParameterElementImpl(name ?? '', -1);
76+
return TypeParameterElementImpl2(
77+
firstFragment: fragment,
78+
name3: name,
79+
bound: null,
80+
);
81+
},
82+
growable: false,
83+
);
84+
85+
var map = <TypeParameterElement2, DartType>{};
86+
for (int i = 0; i < typeParameters.length; ++i) {
87+
map[typeParameters[i]] = TypeParameterTypeImpl.v2(
88+
element: freshParameters[i],
89+
nullabilitySuffix: NullabilitySuffix.none,
90+
);
91+
}
92+
93+
var substitution = Substitution.fromMap2(map);
94+
95+
for (int i = 0; i < typeParameters.length; ++i) {
96+
// TODO(kallentu): : Clean up TypeParameterElementImpl casting once
97+
// variance is added to the interface.
98+
var typeParameter = typeParameters[i] as TypeParameterElementImpl2;
99+
if (!typeParameter.isLegacyCovariant) {
100+
freshParameters[i].firstFragment.variance = typeParameter.variance;
101+
}
102+
103+
var bound = typeParameter.bound;
104+
if (bound != null) {
105+
var newBound = substitution.substituteType(bound);
106+
freshParameters[i].firstFragment.bound = newBound;
107+
}
108+
}
109+
110+
return FreshTypeParameters(freshParameters, substitution);
111+
}
112+
64113
/// Given a generic function [type] of a class member (so that it does not
65114
/// carry its element and type arguments), substitute its type parameters with
66115
/// the [newTypeParameters] in the formal parameters and return type.
@@ -200,6 +249,16 @@ abstract class Substitution {
200249
return _MapSubstitution(map);
201250
}
202251

252+
/// Substitutes each parameter to the type it maps to in [map].
253+
static MapSubstitution fromMap2(Map<TypeParameterElement2, DartType> map) {
254+
if (map.isEmpty) {
255+
return _NullSubstitution.instance;
256+
}
257+
return _MapSubstitution(
258+
map.map((key, value) => MapEntry(key.asElement, value)),
259+
);
260+
}
261+
203262
/// Substitutes the Nth parameter in [parameters] with the Nth type in
204263
/// [types].
205264
static MapSubstitution fromPairs(

0 commit comments

Comments
 (0)