Skip to content

Commit 6c68547

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Use more TypeImpl in TypeSystemImpl and clients.
Change-Id: Idd3c5b97ac37fd427591d1c303da5985b213040a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/412402 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Paul Berry <[email protected]>
1 parent d50d8b6 commit 6c68547

21 files changed

+108
-133
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6722,7 +6722,7 @@ final class ExtensionOverrideImpl extends ExpressionImpl
67226722
List<DartType>? typeArgumentTypes;
67236723

67246724
@override
6725-
DartType? extendedType;
6725+
TypeImpl? extendedType;
67266726

67276727
ExtensionOverrideImpl({
67286728
required this.importPrefix,

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -615,11 +615,11 @@ class ClassElementImpl2 extends InterfaceElementImpl2
615615
///
616616
/// If the class is sealed, and all its subtypes are either final or sealed,
617617
/// then these subtypes are all subtypes that are possible.
618-
List<InterfaceType>? get allSubtypes {
618+
List<InterfaceTypeImpl>? get allSubtypes {
619619
if (isFinal) {
620-
var result = <InterfaceType>[];
620+
var result = <InterfaceTypeImpl>[];
621621
for (var element in library2.children2) {
622-
if (element is InterfaceElement2 && element != this) {
622+
if (element is InterfaceElementImpl2 && element != this) {
623623
var elementThis = element.thisType;
624624
if (elementThis.asInstanceOf2(this) != null) {
625625
result.add(elementThis);
@@ -630,9 +630,9 @@ class ClassElementImpl2 extends InterfaceElementImpl2
630630
}
631631

632632
if (isSealed) {
633-
var result = <InterfaceType>[];
633+
var result = <InterfaceTypeImpl>[];
634634
for (var element in library2.children2) {
635-
if (element is! InterfaceElement2 || identical(element, this)) {
635+
if (element is! InterfaceElementImpl2 || identical(element, this)) {
636636
continue;
637637
}
638638

@@ -642,7 +642,7 @@ class ClassElementImpl2 extends InterfaceElementImpl2
642642
}
643643

644644
switch (element) {
645-
case ClassElement2 _:
645+
case ClassElementImpl2 _:
646646
if (element.isFinal || element.isSealed) {
647647
result.add(elementThis);
648648
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ class GenericInferrer {
553553
return inferredTypes;
554554
}
555555

556-
void _demoteTypes(List<DartType> types) {
556+
void _demoteTypes(List<TypeImpl> types) {
557557
for (var i = 0; i < types.length; i++) {
558558
types[i] = _typeSystem.demoteType(types[i]);
559559
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ class NormalizeHelper {
237237
/// NORM(X & T)
238238
/// * let S be NORM(T)
239239
TypeImpl _typeParameterType_promoted(
240-
TypeParameterElementImpl2 X, DartType S) {
240+
TypeParameterElementImpl2 X, TypeImpl S) {
241241
// * if S is Never then Never
242242
if (identical(S, NeverTypeImpl.instance)) {
243243
return NeverTypeImpl.instance;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
821821
String get name => element.name;
822822

823823
/// The instantiated representation type, if [element] is an extension type.
824-
DartType? get representationType {
824+
TypeImpl? get representationType {
825825
if (element case ExtensionTypeElement element) {
826826
var substitution = Substitution.fromInterfaceType(this);
827827
var representationType = element.representation.type;

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

Lines changed: 42 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class ExtensionTypeErasure extends ReplacementVisitor {
5050
TypeImpl? visitInterfaceType(covariant InterfaceTypeImpl type) {
5151
if (type.representationType case var representationType?) {
5252
var erased = representationType.accept(this) ?? representationType;
53-
erased as TypeImpl;
5453
// If the extension type is nullable, apply it to the erased.
5554
if (type.nullabilitySuffix == NullabilitySuffix.question) {
5655
return erased.withNullability(NullabilitySuffix.question);
@@ -139,8 +138,8 @@ class TypeSystemImpl implements TypeSystem {
139138
/// If [eraseTypes] is not null, this function uses that function to erase the
140139
/// extension types within [left] and [right]. Otherwise, it uses the
141140
/// extension type erasure.
142-
bool canBeSubtypeOf(DartType left, DartType right,
143-
{(DartType, DartType) Function(DartType, DartType)? eraseTypes}) {
141+
bool canBeSubtypeOf(TypeImpl left, TypeImpl right,
142+
{(TypeImpl, TypeImpl) Function(TypeImpl, TypeImpl)? eraseTypes}) {
144143
(left, right) = eraseTypes != null
145144
? eraseTypes(left, right)
146145
: (left.extensionTypeErasure, right.extensionTypeErasure);
@@ -199,7 +198,8 @@ class TypeSystemImpl implements TypeSystem {
199198
return true;
200199
}
201200

202-
bool canBeSubtypeOfInterfaces(InterfaceType left, InterfaceType right) {
201+
bool canBeSubtypeOfInterfaces(
202+
InterfaceTypeImpl left, InterfaceTypeImpl right) {
203203
assert(left.element3 == right.element3);
204204
var leftArguments = left.typeArguments;
205205
var rightArguments = right.typeArguments;
@@ -279,7 +279,7 @@ class TypeSystemImpl implements TypeSystem {
279279
}
280280
}
281281

282-
if (left is RecordType && right is RecordType) {
282+
if (left is RecordTypeImpl && right is RecordTypeImpl) {
283283
if (left.positionalFields.length != right.positionalFields.length) {
284284
return false;
285285
}
@@ -314,11 +314,9 @@ class TypeSystemImpl implements TypeSystem {
314314
/// Returns [type] in which all promoted type variables have been replaced
315315
/// with their unpromoted equivalents, and, if non-nullable by default,
316316
/// replaces all legacy types with their non-nullable equivalents.
317-
TypeImpl demoteType(DartType type) {
317+
TypeImpl demoteType(TypeImpl type) {
318318
var visitor = const DemotionVisitor();
319-
// TODO(paulberry): eliminate this cast by changing `ReplacementVisitor` so
320-
// that it implements `TypeVisitor<TypeImpl?>`.
321-
return (type.accept(visitor) ?? type) as TypeImpl;
319+
return type.accept(visitor) ?? type;
322320
}
323321

324322
/// Eliminates type variables from the context [type], replacing them with
@@ -345,10 +343,7 @@ class TypeSystemImpl implements TypeSystem {
345343
/// Defines the "remainder" of `T` when `S` has been removed from
346344
/// consideration by an instance check. This operation is used for type
347345
/// promotion during flow analysis.
348-
TypeImpl factor(DartType T, DartType S) {
349-
// TODO(paulberry): eliminate this cast by changing the type of the
350-
// parameter `T`.
351-
T as TypeImpl;
346+
TypeImpl factor(TypeImpl T, TypeImpl S) {
352347
// * If T <: S then Never
353348
if (isSubtypeOf(T, S)) {
354349
return NeverTypeImpl.instance;
@@ -385,10 +380,8 @@ class TypeSystemImpl implements TypeSystem {
385380
}
386381

387382
@override
388-
TypeImpl flatten(DartType T) {
389-
// TODO(paulberry): remove this cast by making the parameter `T` covariant
390-
// and changing its type to `TypeImpl`.
391-
if (identical(T as TypeImpl, UnknownInferredType.instance)) {
383+
TypeImpl flatten(covariant TypeImpl T) {
384+
if (identical(T, UnknownInferredType.instance)) {
392385
return T;
393386
}
394387

@@ -421,9 +414,9 @@ class TypeSystemImpl implements TypeSystem {
421414
// If T has future type Future<S> or FutureOr<S> then flatten(T) = S
422415
// If T has future type Future<S>? or FutureOr<S>? then flatten(T) = S?
423416
var futureType = this.futureType(T);
424-
if (futureType is InterfaceType) {
417+
if (futureType is InterfaceTypeImpl) {
425418
if (futureType.isDartAsyncFuture || futureType.isDartAsyncFutureOr) {
426-
var S = futureType.typeArguments[0] as TypeImpl;
419+
var S = futureType.typeArguments[0];
427420
if (futureType.nullabilitySuffix == NullabilitySuffix.question) {
428421
return S.withNullability(NullabilitySuffix.question);
429422
}
@@ -435,25 +428,23 @@ class TypeSystemImpl implements TypeSystem {
435428
return T;
436429
}
437430

438-
TypeImpl futureOrBase(DartType type) {
431+
TypeImpl futureOrBase(TypeImpl type) {
439432
// If `T` is `FutureOr<S>` for some `S`,
440433
// then `futureOrBase(T)` = `futureOrBase(S)`
441-
if (type is InterfaceType && type.isDartAsyncFutureOr) {
434+
if (type is InterfaceTypeImpl && type.isDartAsyncFutureOr) {
442435
return futureOrBase(
443436
type.typeArguments[0],
444437
);
445438
}
446439

447440
// Otherwise `futureOrBase(T)` = `T`.
448-
// TODO(paulberrry): eliminate this cast by changing the type of the
449-
// parameter `type`.
450-
return type as TypeImpl;
441+
return type;
451442
}
452443

453444
/// We say that S is the future type of a type T in the following cases,
454445
/// using the first applicable case:
455446
@visibleForTesting
456-
DartType? futureType(DartType T) {
447+
TypeImpl? futureType(TypeImpl T) {
457448
// T implements S, and there is a U such that S is Future<U>
458449
if (T.nullabilitySuffix != NullabilitySuffix.question) {
459450
var result = T.asInstanceOf(typeProvider.futureElement);
@@ -472,16 +463,16 @@ class TypeSystemImpl implements TypeSystem {
472463
/// https://github.com/dart-lang/language/
473464
/// See `nnbd/feature-specification.md`
474465
/// See `#the-future-value-type-of-an-asynchronous-non-generator-function`
475-
DartType futureValueType(DartType T) {
466+
TypeImpl futureValueType(TypeImpl T) {
476467
// futureValueType(`S?`) = futureValueType(`S`), for all `S`.
477468
if (T.nullabilitySuffix != NullabilitySuffix.none) {
478-
var S = (T as TypeImpl).withNullability(NullabilitySuffix.none);
469+
var S = T.withNullability(NullabilitySuffix.none);
479470
return futureValueType(S);
480471
}
481472

482473
// futureValueType(Future<`S`>) = `S`, for all `S`.
483474
// futureValueType(FutureOr<`S`>) = `S`, for all `S`.
484-
if (T is InterfaceType) {
475+
if (T is InterfaceTypeImpl) {
485476
if (T.isDartAsyncFuture || T.isDartAsyncFutureOr) {
486477
return T.typeArguments[0];
487478
}
@@ -962,9 +953,7 @@ class TypeSystemImpl implements TypeSystem {
962953

963954
/// We say that a type `T` is _incompatible with await_ if at least
964955
/// one of the following criteria holds:
965-
bool isIncompatibleWithAwait(DartType T) {
966-
T as TypeImpl;
967-
956+
bool isIncompatibleWithAwait(TypeImpl T) {
968957
// `T` is `S?`, and `S` is incompatible with await.
969958
if (T.nullabilitySuffix == NullabilitySuffix.question) {
970959
var T_none = T.withNullability(NullabilitySuffix.none);
@@ -1018,12 +1007,9 @@ class TypeSystemImpl implements TypeSystem {
10181007
/// Defines an (almost) total order on bottom and `Null` types. This does not
10191008
/// currently consistently order two different type variables with the same
10201009
/// bound.
1021-
bool isMoreBottom(DartType T, DartType S) {
1022-
var T_impl = T as TypeImpl;
1023-
var S_impl = S as TypeImpl;
1024-
1025-
var T_nullability = T_impl.nullabilitySuffix;
1026-
var S_nullability = S_impl.nullabilitySuffix;
1010+
bool isMoreBottom(TypeImpl T, TypeImpl S) {
1011+
var T_nullability = T.nullabilitySuffix;
1012+
var S_nullability = S.nullabilitySuffix;
10271013

10281014
// MOREBOTTOM(Never, T) = true
10291015
if (identical(T, NeverTypeImpl.instance)) {
@@ -1048,8 +1034,8 @@ class TypeSystemImpl implements TypeSystem {
10481034
// MOREBOTTOM(T?, S?) = MOREBOTTOM(T, S)
10491035
if (T_nullability == NullabilitySuffix.question &&
10501036
S_nullability == NullabilitySuffix.question) {
1051-
var T2 = T_impl.withNullability(NullabilitySuffix.none);
1052-
var S2 = S_impl.withNullability(NullabilitySuffix.none);
1037+
var T2 = T.withNullability(NullabilitySuffix.none);
1038+
var S2 = S.withNullability(NullabilitySuffix.none);
10531039
return isMoreBottom(T2, S2);
10541040
}
10551041

@@ -1103,12 +1089,9 @@ class TypeSystemImpl implements TypeSystem {
11031089
}
11041090

11051091
/// Defines a total order on top and Object types.
1106-
bool isMoreTop(DartType T, DartType S) {
1107-
var T_impl = T as TypeImpl;
1108-
var S_impl = S as TypeImpl;
1109-
1110-
var T_nullability = T_impl.nullabilitySuffix;
1111-
var S_nullability = S_impl.nullabilitySuffix;
1092+
bool isMoreTop(TypeImpl T, TypeImpl S) {
1093+
var T_nullability = T.nullabilitySuffix;
1094+
var S_nullability = S.nullabilitySuffix;
11121095

11131096
// MORETOP(void, S) = true
11141097
if (identical(T, VoidTypeImpl.instance)) {
@@ -1145,8 +1128,8 @@ class TypeSystemImpl implements TypeSystem {
11451128
// MORETOP(T?, S?) = MORETOP(T, S)
11461129
if (T_nullability == NullabilitySuffix.question &&
11471130
S_nullability == NullabilitySuffix.question) {
1148-
var T2 = T_impl.withNullability(NullabilitySuffix.none);
1149-
var S2 = S_impl.withNullability(NullabilitySuffix.none);
1131+
var T2 = T.withNullability(NullabilitySuffix.none);
1132+
var S2 = S.withNullability(NullabilitySuffix.none);
11501133
return isMoreTop(T2, S2);
11511134
}
11521135

@@ -1202,9 +1185,8 @@ class TypeSystemImpl implements TypeSystem {
12021185
}
12031186

12041187
/// Return `true` for things in the equivalence class of `Null`.
1205-
bool isNull(DartType type) {
1206-
var typeImpl = type as TypeImpl;
1207-
var nullabilitySuffix = typeImpl.nullabilitySuffix;
1188+
bool isNull(TypeImpl type) {
1189+
var nullabilitySuffix = type.nullabilitySuffix;
12081190

12091191
// NULL(Null) is true
12101192
// Also includes `Null?` from the rules below.
@@ -1215,7 +1197,7 @@ class TypeSystemImpl implements TypeSystem {
12151197
// NULL(T?) is true iff NULL(T) or BOTTOM(T)
12161198
// The case for `Null?` is already checked above.
12171199
if (nullabilitySuffix == NullabilitySuffix.question) {
1218-
var T = typeImpl.withNullability(NullabilitySuffix.none);
1200+
var T = type.withNullability(NullabilitySuffix.none);
12191201
return T.isBottom;
12201202
}
12211203

@@ -1244,9 +1226,8 @@ class TypeSystemImpl implements TypeSystem {
12441226
}
12451227

12461228
/// Return `true` for any type which is in the equivalence class of `Object`.
1247-
bool isObject(DartType type) {
1248-
var typeImpl = type as TypeImpl;
1249-
if (typeImpl.nullabilitySuffix != NullabilitySuffix.none) {
1229+
bool isObject(TypeImpl type) {
1230+
if (type.nullabilitySuffix != NullabilitySuffix.none) {
12501231
return false;
12511232
}
12521233

@@ -1305,7 +1286,7 @@ class TypeSystemImpl implements TypeSystem {
13051286
}
13061287

13071288
/// Return `true` for any type which is in the equivalence class of top types.
1308-
bool isTop(DartType type) {
1289+
bool isTop(TypeImpl type) {
13091290
// TOP(?) is true
13101291
if (identical(type, UnknownInferredType.instance)) {
13111292
return true;
@@ -1322,12 +1303,11 @@ class TypeSystemImpl implements TypeSystem {
13221303
return true;
13231304
}
13241305

1325-
var typeImpl = type as TypeImpl;
1326-
var nullabilitySuffix = typeImpl.nullabilitySuffix;
1306+
var nullabilitySuffix = type.nullabilitySuffix;
13271307

13281308
// TOP(T?) is true iff TOP(T) or OBJECT(T)
13291309
if (nullabilitySuffix == NullabilitySuffix.question) {
1330-
var T = typeImpl.withNullability(NullabilitySuffix.none);
1310+
var T = type.withNullability(NullabilitySuffix.none);
13311311
return isTop(T) || isObject(T);
13321312
}
13331313

@@ -1643,10 +1623,7 @@ class TypeSystemImpl implements TypeSystem {
16431623
}
16441624

16451625
@override
1646-
TypeImpl resolveToBound(DartType type) {
1647-
// TODO(paulberry): remove this cast by making the parameter `T` covariant
1648-
// and changing its type to `TypeImpl`.
1649-
type as TypeImpl;
1626+
TypeImpl resolveToBound(covariant TypeImpl type) {
16501627
if (type is TypeParameterTypeImpl) {
16511628
var promotedBound = type.promotedBound;
16521629
if (promotedBound != null) {
@@ -1734,10 +1711,7 @@ class TypeSystemImpl implements TypeSystem {
17341711

17351712
/// Tries to promote from the first type from the second type, and returns the
17361713
/// promoted type if it succeeds, otherwise null.
1737-
TypeImpl? tryPromoteToType(DartType to, DartType from) {
1738-
// TODO(paulberry): eliminate this cast by changing the type of the
1739-
// parameter `to`.
1740-
to as TypeImpl;
1714+
TypeImpl? tryPromoteToType(TypeImpl to, TypeImpl from) {
17411715
// Allow promoting to a subtype, for example:
17421716
//
17431717
// f(Base b) {
@@ -1753,7 +1727,7 @@ class TypeSystemImpl implements TypeSystem {
17531727
}
17541728
// For a type parameter `T extends U`, allow promoting the upper bound
17551729
// `U` to `S` where `S <: U`, yielding a type parameter `T extends S`.
1756-
if (from is TypeParameterType) {
1730+
if (from is TypeParameterTypeImpl) {
17571731
if (isSubtypeOf(to, from.bound)) {
17581732
var declaration = from.element3.baseElement;
17591733
return TypeParameterTypeImpl.v2(
@@ -1817,7 +1791,7 @@ class TypeSystemImpl implements TypeSystem {
18171791
/// type: `T0` itself is `T0` bounded; if `B` is `T0` bounded and `X` is a
18181792
/// type variable with bound `B` then `X` is `T0` bounded; finally, if `B`
18191793
/// is `T0` bounded and `X` is a type variable then `X&B` is `T0` bounded.
1820-
DartType? _futureTypeOfBounded(DartType T) {
1794+
TypeImpl? _futureTypeOfBounded(TypeImpl T) {
18211795
if (T is InterfaceType) {
18221796
if (T.nullabilitySuffix != NullabilitySuffix.question) {
18231797
if (T.isDartAsyncFutureOr) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ class BinaryExpressionResolver {
470470
void _resolveUserDefinableType(BinaryExpressionImpl node) {
471471
var leftOperand = node.leftOperand;
472472

473-
DartType leftType;
473+
TypeImpl leftType;
474474
if (leftOperand is AugmentedExpressionImpl) {
475475
leftType = leftOperand.typeOrThrow;
476476
} else if (leftOperand is ExtensionOverrideImpl) {

0 commit comments

Comments
 (0)