Skip to content

Commit 3700d33

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate type_algebra_test.dart
Change-Id: If9c1ef42eb9f0b9f83cad42429c99b4a2a95dd5a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/408142 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 97772dd commit 3700d33

File tree

8 files changed

+198
-189
lines changed

8 files changed

+198
-189
lines changed

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

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,25 @@ DartType substitute(
151151
return Substitution.fromMap(substitution).substituteType(type);
152152
}
153153

154+
/// Returns a type where all occurrences of the given type parameters have been
155+
/// replaced with the corresponding types.
156+
///
157+
/// This will copy only the sub-terms of [type] that contain substituted
158+
/// variables; all other [DartType] objects will be reused.
159+
///
160+
/// In particular, if no type parameters were substituted, this is guaranteed
161+
/// to return the [type] instance (not a copy), so the caller may use
162+
/// [identical] to efficiently check if a distinct type was created.
163+
DartType substitute2(
164+
DartType type,
165+
Map<TypeParameterElement2, DartType> substitution,
166+
) {
167+
if (substitution.isEmpty) {
168+
return type;
169+
}
170+
return Substitution.fromMap2(substitution).substituteType(type);
171+
}
172+
154173
/// 1. Substituting T=X! into T! yields X!
155174
/// 3. Substituting T=X? into T! yields X?
156175
/// 7. Substituting T=X! into T? yields X?
@@ -284,26 +303,6 @@ abstract class Substitution {
284303
var fragments = parameters.map((e) => e.asElement).toList();
285304
return fromPairs(fragments, types);
286305
}
287-
288-
/// Substitutes all occurrences of the given type parameters with the
289-
/// corresponding upper or lower bound, depending on the variance of the
290-
/// context where it occurs.
291-
///
292-
/// For example the type `(T) => T` with the bounds `bottom <: T <: num`
293-
/// becomes `(bottom) => num` (in this example, `num` is the upper bound,
294-
/// and `bottom` is the lower bound).
295-
///
296-
/// This is a way to obtain an upper bound for a type while eliminating all
297-
/// references to certain type variables.
298-
static Substitution fromUpperAndLowerBounds(
299-
Map<TypeParameterElement, DartType> upper,
300-
Map<TypeParameterElement, DartType> lower,
301-
) {
302-
if (upper.isEmpty && lower.isEmpty) {
303-
return _NullSubstitution.instance;
304-
}
305-
return _UpperLowerBoundsSubstitution(upper, lower);
306-
}
307306
}
308307

309308
class _CombinedSubstitution extends Substitution {
@@ -696,18 +695,3 @@ abstract class _TypeSubstitutor
696695
return types.map((e) => e.accept(this)).toFixedList();
697696
}
698697
}
699-
700-
class _UpperLowerBoundsSubstitution extends Substitution {
701-
final Map<TypeParameterElement, DartType> upper;
702-
final Map<TypeParameterElement, DartType> lower;
703-
704-
_UpperLowerBoundsSubstitution(this.upper, this.lower);
705-
706-
@override
707-
DartType? getSubstitute(TypeParameterElement parameter, bool upperBound) {
708-
return upperBound ? upper[parameter] : lower[parameter];
709-
}
710-
711-
@override
712-
String toString() => '_UpperLowerBoundsSubstitution($upper, $lower)';
713-
}

pkg/analyzer/lib/src/utilities/extensions/collection.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ extension MapExtension<K, V> on Map<K, V> {
108108
return keys.firstOrNull;
109109
}
110110

111+
Map<K2, V> mapKey<K2>(K2 Function(K) convert) {
112+
return map((key, value) {
113+
var key2 = convert(key);
114+
return MapEntry(key2, value);
115+
});
116+
}
117+
111118
Map<K, V2> mapValue<V2>(V2 Function(V) convert) {
112119
return map((key, value) {
113120
var value2 = convert(value);

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,12 @@ extension TypeParameterElement2Extension on TypeParameterElement2 {
637637

638638
extension TypeParameterElementExtension on TypeParameterElement {
639639
TypeParameterElement2 get asElement2 {
640-
return (this as TypeParameterElementImpl).element;
640+
return (this as TypeParameterElementImpl).asElement2;
641+
}
642+
}
643+
644+
extension TypeParameterElementImplExtension on TypeParameterElementImpl {
645+
TypeParameterElementImpl2 get asElement2 {
646+
return element;
641647
}
642648
}

pkg/analyzer/test/generated/elements_types_mixin.dart

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,22 @@ mixin ElementsTypesMixin {
686686
return fragment;
687687
}
688688

689+
TypeAliasElementImpl2 typeAlias2({
690+
required String name,
691+
required List<TypeParameterElementImpl2> typeParameters,
692+
required DartType aliasedType,
693+
}) {
694+
var fragment = TypeAliasElementImpl(name, 0);
695+
fragment.name2 = name;
696+
fragment.enclosingElement3 = testLibrary.definingCompilationUnit;
697+
fragment.typeParameters = typeParameters
698+
.map((e) => e.asElement as TypeParameterElementImpl)
699+
.toList();
700+
fragment.aliasedType = aliasedType;
701+
702+
return TypeAliasElementImpl2(Reference.root(), fragment);
703+
}
704+
689705
TypeImpl typeAliasTypeNone(
690706
TypeAliasElementImpl element, {
691707
List<DartType> typeArguments = const [],
@@ -696,6 +712,16 @@ mixin ElementsTypesMixin {
696712
);
697713
}
698714

715+
TypeImpl typeAliasTypeNone2(
716+
TypeAliasElementImpl2 element, {
717+
List<DartType> typeArguments = const [],
718+
}) {
719+
return element.instantiate(
720+
typeArguments: typeArguments,
721+
nullabilitySuffix: NullabilitySuffix.none,
722+
);
723+
}
724+
699725
TypeParameterElementImpl typeParameter(String name,
700726
{DartType? bound, Variance? variance}) {
701727
var element = TypeParameterElementImpl.synthetic(name);
@@ -704,7 +730,7 @@ mixin ElementsTypesMixin {
704730
return element;
705731
}
706732

707-
TypeParameterElement2 typeParameter2(String name,
733+
TypeParameterElementImpl2 typeParameter2(String name,
708734
{DartType? bound, Variance? variance}) {
709735
return typeParameter(name, bound: bound, variance: variance).asElement2;
710736
}

pkg/analyzer/test/src/dart/element/least_greatest_closure_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class GreatestClosureTest extends AbstractTypeSystemTest {
2626
void setUp() {
2727
super.setUp();
2828

29-
T = typeParameter2('T') as TypeParameterElementImpl2;
29+
T = typeParameter2('T');
3030
T_none = typeParameterTypeNone2(T);
3131
T_question = typeParameterTypeQuestion2(T);
3232
}

0 commit comments

Comments
 (0)