Skip to content

Commit 749de93

Browse files
stereotype441Commit Queue
authored andcommitted
[analyzer] Refine types returned by TypeSystemImpl methods.
In the following methods in `TypeSystemImpl`, the return type is changed from `DartType` to `TypeImpl`: - `greatestClosure` - `greatestLowerBound` - `leastClosure` - `leastUpperBound` - `makeNullable` - `promoteToNonNull` This required adding a few casts to `TypeSystemImpl`, `LeastGreatestClosureHelper`, and `InterfaceTypeImpl`, which I believe I will be able to remove in future CLs. It also allowed removing some casts from `GreatestLowerBoundHelper`, `LeastUpperBoundHelper`, and `InterfaceTypeImpl`. This is part of a larger arc of work to change the analyzer's use of the shared code so that the type parameters it supplies are not part of the analyzer public API. See #59763. Change-Id: I24f27090bb2d07ae33be9c2c8d7908ef71a96929 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/404640 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Paul Berry <[email protected]>
1 parent 3417d1a commit 749de93

File tree

6 files changed

+61
-58
lines changed

6 files changed

+61
-58
lines changed

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

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

77
import 'package:analyzer/dart/element/element.dart';
88
import 'package:analyzer/dart/element/nullability_suffix.dart';
9-
import 'package:analyzer/dart/element/type.dart';
109
import 'package:analyzer/src/dart/element/extensions.dart';
1110
import 'package:analyzer/src/dart/element/type.dart';
1211
import 'package:analyzer/src/dart/element/type_provider.dart';
@@ -27,7 +26,7 @@ class GreatestLowerBoundHelper {
2726
///
2827
/// https://github.com/dart-lang/language
2928
/// See `resources/type-system/upper-lower-bounds.md`
30-
DartType getGreatestLowerBound(DartType T1, DartType T2) {
29+
TypeImpl getGreatestLowerBound(TypeImpl T1, TypeImpl T2) {
3130
// DOWN(T, T) = T
3231
if (identical(T1, T2)) {
3332
return T1;
@@ -103,11 +102,8 @@ class GreatestLowerBoundHelper {
103102
}
104103
}
105104

106-
var T1_impl = T1 as TypeImpl;
107-
var T2_impl = T2 as TypeImpl;
108-
109-
var T1_nullability = T1_impl.nullabilitySuffix;
110-
var T2_nullability = T2_impl.nullabilitySuffix;
105+
var T1_nullability = T1.nullabilitySuffix;
106+
var T2_nullability = T2.nullabilitySuffix;
111107

112108
// DOWN(Null, T2)
113109
if (T1_nullability == NullabilitySuffix.none && T1.isDartCoreNull) {
@@ -153,7 +149,7 @@ class GreatestLowerBoundHelper {
153149
}
154150

155151
// * NonNull(T2) if NonNull(T2) is non-nullable
156-
var T2_nonNull = _typeSystem.promoteToNonNull(T2_impl);
152+
var T2_nonNull = _typeSystem.promoteToNonNull(T2);
157153
if (_typeSystem.isNonNullable(T2_nonNull)) {
158154
return T2_nonNull;
159155
}
@@ -170,7 +166,7 @@ class GreatestLowerBoundHelper {
170166
}
171167

172168
// * NonNull(T1) if NonNull(T1) is non-nullable
173-
var T1_nonNull = _typeSystem.promoteToNonNull(T1_impl);
169+
var T1_nonNull = _typeSystem.promoteToNonNull(T1);
174170
if (_typeSystem.isNonNullable(T1_nonNull)) {
175171
return T1_nonNull;
176172
}
@@ -184,12 +180,12 @@ class GreatestLowerBoundHelper {
184180
// DOWN(T1, T2?) = S where S is DOWN(T1, T2)
185181
if (T1_nullability != NullabilitySuffix.none ||
186182
T2_nullability != NullabilitySuffix.none) {
187-
var T1_none = T1_impl.withNullability(NullabilitySuffix.none);
188-
var T2_none = T2_impl.withNullability(NullabilitySuffix.none);
183+
var T1_none = T1.withNullability(NullabilitySuffix.none);
184+
var T2_none = T2.withNullability(NullabilitySuffix.none);
189185
var S = getGreatestLowerBound(T1_none, T2_none);
190186
if (T1_nullability == NullabilitySuffix.question &&
191187
T2_nullability == NullabilitySuffix.question) {
192-
return (S as TypeImpl).withNullability(NullabilitySuffix.question);
188+
return S.withNullability(NullabilitySuffix.question);
193189
}
194190
return S;
195191
}
@@ -258,7 +254,7 @@ class GreatestLowerBoundHelper {
258254
///
259255
/// https://github.com/dart-lang/language
260256
/// See `resources/type-system/upper-lower-bounds.md`
261-
DartType _functionType(FunctionType f, FunctionType g) {
257+
TypeImpl _functionType(FunctionTypeImpl f, FunctionTypeImpl g) {
262258
var fTypeFormals = f.typeFormals;
263259
var gTypeFormals = g.typeFormals;
264260

@@ -378,7 +374,7 @@ class GreatestLowerBoundHelper {
378374
);
379375
}
380376

381-
DartType _recordType(RecordTypeImpl T1, RecordTypeImpl T2) {
377+
TypeImpl _recordType(RecordTypeImpl T1, RecordTypeImpl T2) {
382378
var positional1 = T1.positionalFields;
383379
var positional2 = T2.positionalFields;
384380
if (positional1.length != positional2.length) {

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,23 @@ class LeastGreatestClosureHelper extends ReplacementVisitor {
4747
}
4848

4949
/// Returns a supertype of [type] for all values of [eliminationTargets].
50-
DartType eliminateToGreatest(DartType type) {
50+
TypeImpl eliminateToGreatest(DartType type) {
5151
_isCovariant = true;
5252
_isLeastClosure = false;
53-
return type.accept(this) ?? type;
53+
// TODO(paulberry): make this cast unnecessary by changing the type of
54+
// `type` and by changing `ReplacementVisitor` to implement
55+
// `TypeVisitor<TypeImpl?>`.
56+
return (type.accept(this) ?? type) as TypeImpl;
5457
}
5558

5659
/// Returns a subtype of [type] for all values of [eliminationTargets].
57-
DartType eliminateToLeast(DartType type) {
60+
TypeImpl eliminateToLeast(DartType type) {
5861
_isCovariant = true;
5962
_isLeastClosure = true;
60-
return type.accept(this) ?? type;
63+
// TODO(paulberry): make this cast unnecessary by changing the type of
64+
// `type` and by changing `ReplacementVisitor` to implement
65+
// `TypeVisitor<TypeImpl?>`.
66+
return (type.accept(this) ?? type) as TypeImpl;
6167
}
6268

6369
@override

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ class LeastUpperBoundHelper {
318318

319319
LeastUpperBoundHelper(this._typeSystem);
320320

321-
InterfaceType get _interfaceTypeFunctionNone {
321+
InterfaceTypeImpl get _interfaceTypeFunctionNone {
322322
return _typeSystem.typeProvider.functionType.element3.instantiate(
323323
typeArguments: const [],
324324
nullabilitySuffix: NullabilitySuffix.none,
@@ -329,7 +329,7 @@ class LeastUpperBoundHelper {
329329
///
330330
/// https://github.com/dart-lang/language
331331
/// See `resources/type-system/upper-lower-bounds.md`
332-
DartType getLeastUpperBound(DartType T1, DartType T2) {
332+
TypeImpl getLeastUpperBound(TypeImpl T1, TypeImpl T2) {
333333
// UP(T, T) = T
334334
if (identical(T1, T2)) {
335335
return T1;
@@ -439,11 +439,8 @@ class LeastUpperBoundHelper {
439439
}
440440
}
441441

442-
var T1_impl = T1 as TypeImpl;
443-
var T2_impl = T2 as TypeImpl;
444-
445-
var T1_nullability = T1_impl.nullabilitySuffix;
446-
var T2_nullability = T2_impl.nullabilitySuffix;
442+
var T1_nullability = T1.nullabilitySuffix;
443+
var T2_nullability = T2.nullabilitySuffix;
447444

448445
// UP(T1, T2) where NULL(T1)
449446
if (T1_isNull) {
@@ -508,10 +505,10 @@ class LeastUpperBoundHelper {
508505
// UP(T1, T2?) = S? where S is UP(T1, T2)
509506
if (T1_nullability != NullabilitySuffix.none ||
510507
T2_nullability != NullabilitySuffix.none) {
511-
var T1_none = T1_impl.withNullability(NullabilitySuffix.none);
512-
var T2_none = T2_impl.withNullability(NullabilitySuffix.none);
508+
var T1_none = T1.withNullability(NullabilitySuffix.none);
509+
var T2_none = T2.withNullability(NullabilitySuffix.none);
513510
var S = getLeastUpperBound(T1_none, T2_none);
514-
return (S as TypeImpl).withNullability(NullabilitySuffix.question);
511+
return S.withNullability(NullabilitySuffix.question);
515512
}
516513

517514
assert(T1_nullability == NullabilitySuffix.none);
@@ -622,7 +619,7 @@ class LeastUpperBoundHelper {
622619
///
623620
/// https://github.com/dart-lang/language
624621
/// See `resources/type-system/upper-lower-bounds.md`
625-
DartType _functionType(FunctionType f, FunctionType g) {
622+
TypeImpl _functionType(FunctionTypeImpl f, FunctionTypeImpl g) {
626623
var fTypeFormals = f.typeParameters;
627624
var gTypeFormals = g.typeParameters;
628625

@@ -744,20 +741,20 @@ class LeastUpperBoundHelper {
744741
);
745742
}
746743

747-
DartType? _futureOr(DartType T1, DartType T2) {
748-
var T1_futureOr = T1 is InterfaceType && T1.isDartAsyncFutureOr
744+
TypeImpl? _futureOr(TypeImpl T1, TypeImpl T2) {
745+
var T1_futureOr = T1 is InterfaceTypeImpl && T1.isDartAsyncFutureOr
749746
? T1.typeArguments[0]
750747
: null;
751748

752-
var T1_future = T1 is InterfaceType && T1.isDartAsyncFuture
749+
var T1_future = T1 is InterfaceTypeImpl && T1.isDartAsyncFuture
753750
? T1.typeArguments[0]
754751
: null;
755752

756-
var T2_futureOr = T2 is InterfaceType && T2.isDartAsyncFutureOr
753+
var T2_futureOr = T2 is InterfaceTypeImpl && T2.isDartAsyncFutureOr
757754
? T2.typeArguments[0]
758755
: null;
759756

760-
var T2_future = T2 is InterfaceType && T2.isDartAsyncFuture
757+
var T2_future = T2 is InterfaceTypeImpl && T2.isDartAsyncFuture
761758
? T2.typeArguments[0]
762759
: null;
763760

@@ -798,7 +795,7 @@ class LeastUpperBoundHelper {
798795
return _typeSystem.greatestLowerBound(a.type, b.type);
799796
}
800797

801-
DartType _recordType(RecordTypeImpl T1, RecordTypeImpl T2) {
798+
TypeImpl _recordType(RecordTypeImpl T1, RecordTypeImpl T2) {
802799
var positional1 = T1.positionalFields;
803800
var positional2 = T2.positionalFields;
804801
if (positional1.length != positional2.length) {

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ class FutureOrTypeImpl extends InterfaceTypeImpl {
538538
@override
539539
bool get isDartAsyncFutureOr => true;
540540

541-
DartType get typeArgument => typeArguments[0];
541+
TypeImpl get typeArgument => typeArguments[0];
542542

543543
@override
544544
InterfaceTypeImpl withNullability(NullabilitySuffix nullabilitySuffix) {
@@ -575,7 +575,7 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
575575
final InterfaceElementImpl2 element3;
576576

577577
@override
578-
final List<DartType> typeArguments;
578+
final List<TypeImpl> typeArguments;
579579

580580
@override
581581
final NullabilitySuffix nullabilitySuffix;
@@ -614,8 +614,10 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
614614
if (element.name3 == 'FutureOr' && element.library2.isDartAsync) {
615615
return FutureOrTypeImpl(
616616
element3: element,
617+
// TODO(paulberry): avoid this cast by changing the type of
618+
// `typeArguments`.
617619
typeArgument: typeArguments.isNotEmpty
618-
? typeArguments[0]
620+
? typeArguments[0] as TypeImpl
619621
: InvalidTypeImpl.instance,
620622
nullabilitySuffix: nullabilitySuffix,
621623
alias: alias,
@@ -626,9 +628,11 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
626628
alias: alias,
627629
);
628630
} else {
631+
// TODO(paulberry): avoid this cast by changing the type of
632+
// `typeArguments`.
629633
return InterfaceTypeImpl._(
630634
element3: element,
631-
typeArguments: typeArguments,
635+
typeArguments: typeArguments.cast(),
632636
nullabilitySuffix: nullabilitySuffix,
633637
alias: alias,
634638
);
@@ -644,7 +648,7 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
644648

645649
InterfaceTypeImpl._futureOr({
646650
required this.element3,
647-
required DartType typeArgument,
651+
required TypeImpl typeArgument,
648652
required this.nullabilitySuffix,
649653
super.alias,
650654
}) : typeArguments = [typeArgument] {
@@ -1161,18 +1165,12 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
11611165

11621166
@override
11631167
bool referencesAny(Set<TypeParameterElement> parameters) {
1164-
return typeArguments.any((argument) {
1165-
var argumentImpl = argument as TypeImpl;
1166-
return argumentImpl.referencesAny(parameters);
1167-
});
1168+
return typeArguments.any((argument) => argument.referencesAny(parameters));
11681169
}
11691170

11701171
@override
11711172
bool referencesAny2(Set<TypeParameterElementImpl2> parameters) {
1172-
return typeArguments.any((argument) {
1173-
var argumentImpl = argument as TypeImpl;
1174-
return argumentImpl.referencesAny2(parameters);
1175-
});
1173+
return typeArguments.any((argument) => argument.referencesAny2(parameters));
11761174
}
11771175

11781176
@override

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ class TypeSystemImpl implements TypeSystem {
598598
///
599599
/// https://github.com/dart-lang/language
600600
/// See `resources/type-system/inference.md`
601-
DartType greatestClosure(
601+
TypeImpl greatestClosure(
602602
DartType type,
603603
List<TypeParameterElementImpl2> typeParameters,
604604
) {
@@ -638,8 +638,11 @@ class TypeSystemImpl implements TypeSystem {
638638
}
639639

640640
@override
641-
DartType greatestLowerBound(DartType T1, DartType T2) {
642-
return _greatestLowerBoundHelper.getGreatestLowerBound(T1, T2);
641+
TypeImpl greatestLowerBound(DartType T1, DartType T2) {
642+
// TODO(paulberry): make these casts unnecessary by changing the type of
643+
// `T1` and `T2`.
644+
return _greatestLowerBoundHelper.getGreatestLowerBound(
645+
T1 as TypeImpl, T2 as TypeImpl);
643646
}
644647

645648
/// Given a generic function type `F<T0, T1, ... Tn>` and a context type C,
@@ -1380,7 +1383,7 @@ class TypeSystemImpl implements TypeSystem {
13801383
///
13811384
/// https://github.com/dart-lang/language
13821385
/// See `resources/type-system/inference.md`
1383-
DartType leastClosure(
1386+
TypeImpl leastClosure(
13841387
DartType type,
13851388
List<TypeParameterElementImpl2> typeParameters,
13861389
) {
@@ -1419,13 +1422,16 @@ class TypeSystemImpl implements TypeSystem {
14191422
}
14201423

14211424
@override
1422-
DartType leastUpperBound(DartType T1, DartType T2) {
1423-
return _leastUpperBoundHelper.getLeastUpperBound(T1, T2);
1425+
TypeImpl leastUpperBound(DartType T1, DartType T2) {
1426+
// TODO(paulberry): make these casts unnecessary by changing the type of
1427+
// `T1` and `T2`.
1428+
return _leastUpperBoundHelper.getLeastUpperBound(
1429+
T1 as TypeImpl, T2 as TypeImpl);
14241430
}
14251431

14261432
/// Returns a nullable version of [type]. The result would be equivalent to
14271433
/// the union `type | Null` (if we supported union types).
1428-
DartType makeNullable(DartType type) {
1434+
TypeImpl makeNullable(DartType type) {
14291435
// TODO(paulberry): handle type parameter types
14301436
return (type as TypeImpl).withNullability(NullabilitySuffix.question);
14311437
}
@@ -1485,7 +1491,7 @@ class TypeSystemImpl implements TypeSystem {
14851491
/// Returns a non-nullable version of [type]. This is equivalent to the
14861492
/// operation `NonNull` defined in the spec.
14871493
@override
1488-
DartType promoteToNonNull(DartType type) {
1494+
TypeImpl promoteToNonNull(DartType type) {
14891495
if (type.isDartCoreNull) return NeverTypeImpl.instance;
14901496

14911497
if (type is TypeParameterTypeImpl) {

pkg/linter/lib/src/rules/invalid_runtime_check_with_js_interop_types.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,10 @@ class _Visitor extends SimpleAstVisitor<void> {
296296
}) {
297297
LintCode? lintCode;
298298
(DartType, DartType) eraseTypes(DartType left, DartType right) {
299-
var erasedLeft = typeSystem.promoteToNonNull(
299+
DartType erasedLeft = typeSystem.promoteToNonNull(
300300
eraseNonJsInteropTypes.perform(left),
301301
);
302-
var erasedRight = typeSystem.promoteToNonNull(
302+
DartType erasedRight = typeSystem.promoteToNonNull(
303303
eraseNonJsInteropTypes.perform(right),
304304
);
305305
var leftIsInteropType = _isJsInteropType(

0 commit comments

Comments
 (0)