Skip to content

Commit 9ad6149

Browse files
stereotype441commit-bot@chromium.org
authored andcommitted
Make TypeSystemImpl getters for nullable num/int/double.
Change-Id: Ic6fe64f206c3273c9e5c4a688b6a8fe32a78d1fa Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/159963 Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent 607e7d1 commit 9ad6149

File tree

3 files changed

+38
-35
lines changed

3 files changed

+38
-35
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ class InterfaceTypeImpl extends TypeImpl implements InterfaceType {
13051305
}
13061306

13071307
@override
1308-
TypeImpl withNullability(NullabilitySuffix nullabilitySuffix) {
1308+
InterfaceTypeImpl withNullability(NullabilitySuffix nullabilitySuffix) {
13091309
if (this.nullabilitySuffix == nullabilitySuffix) return this;
13101310

13111311
return InterfaceTypeImpl(

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,19 @@ class TypeProviderImpl extends TypeProviderBase {
5757
InterfaceType _boolType;
5858
InterfaceType _deprecatedType;
5959
InterfaceType _doubleType;
60+
InterfaceType _doubleTypeQuestion;
6061
InterfaceType _functionType;
6162
InterfaceType _futureDynamicType;
6263
InterfaceType _futureNullType;
6364
InterfaceType _futureOrNullType;
6465
InterfaceType _intType;
66+
InterfaceType _intTypeQuestion;
6567
InterfaceType _iterableDynamicType;
6668
InterfaceType _iterableObjectType;
6769
InterfaceType _mapObjectObjectType;
6870
InterfaceType _nullType;
6971
InterfaceType _numType;
72+
InterfaceType _numTypeQuestion;
7073
InterfaceType _objectType;
7174
InterfaceType _stackTraceType;
7275
InterfaceType _streamDynamicType;
@@ -148,6 +151,10 @@ class TypeProviderImpl extends TypeProviderBase {
148151
return _doubleType;
149152
}
150153

154+
InterfaceType get doubleTypeQuestion =>
155+
_doubleTypeQuestion ??= (doubleType as InterfaceTypeImpl)
156+
.withNullability(NullabilitySuffix.question);
157+
151158
@override
152159
DartType get dynamicType => DynamicTypeImpl.instance;
153160

@@ -208,6 +215,10 @@ class TypeProviderImpl extends TypeProviderBase {
208215
return _intType;
209216
}
210217

218+
InterfaceType get intTypeQuestion =>
219+
_intTypeQuestion ??= (intType as InterfaceTypeImpl)
220+
.withNullability(NullabilitySuffix.question);
221+
211222
@override
212223
InterfaceType get iterableDynamicType {
213224
_iterableDynamicType ??= InterfaceTypeImpl(
@@ -298,6 +309,10 @@ class TypeProviderImpl extends TypeProviderBase {
298309
return _numType;
299310
}
300311

312+
InterfaceType get numTypeQuestion =>
313+
_numTypeQuestion ??= (numType as InterfaceTypeImpl)
314+
.withNullability(NullabilitySuffix.question);
315+
301316
ClassElement get objectElement {
302317
return _objectElement ??= _getClassElement(_coreLibrary, 'Object');
303318
}

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

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import 'package:analyzer/src/dart/element/subtype.dart';
2525
import 'package:analyzer/src/dart/element/top_merge.dart';
2626
import 'package:analyzer/src/dart/element/type.dart';
2727
import 'package:analyzer/src/dart/element/type_algebra.dart';
28+
import 'package:analyzer/src/dart/element/type_provider.dart';
2829
import 'package:analyzer/src/dart/element/type_schema.dart';
2930
import 'package:analyzer/src/dart/element/type_schema_elimination.dart';
3031
import 'package:meta/meta.dart';
@@ -424,7 +425,7 @@ class TypeSystemImpl extends TypeSystem2 {
424425
bool strictInference;
425426

426427
@override
427-
final TypeProvider typeProvider;
428+
final TypeProviderImpl typeProvider;
428429

429430
/// The cached instance of `Object?`.
430431
InterfaceTypeImpl _objectQuestion;
@@ -448,8 +449,9 @@ class TypeSystemImpl extends TypeSystem2 {
448449
@required this.implicitCasts,
449450
@required bool isNonNullableByDefault,
450451
@required this.strictInference,
451-
@required this.typeProvider,
452-
}) : super(isNonNullableByDefault: isNonNullableByDefault) {
452+
@required TypeProvider typeProvider,
453+
}) : typeProvider = typeProvider as TypeProviderImpl,
454+
super(isNonNullableByDefault: isNonNullableByDefault) {
453455
_greatestLowerBoundHelper = GreatestLowerBoundHelper(this);
454456
_leastUpperBoundHelper = LeastUpperBoundHelper(this);
455457
_subtypeHelper = SubtypeHelper(this);
@@ -1591,29 +1593,26 @@ class TypeSystemImpl extends TypeSystem2 {
15911593
var t = targetType;
15921594
assert(!t.isBottom);
15931595
var numType = typeProvider.numType;
1594-
var numTypeQuestion = makeNullable(numType as InterfaceTypeImpl);
1595-
if (isSubtypeOf(t, numTypeQuestion)) {
1596+
if (isSubtypeOf(t, typeProvider.numTypeQuestion)) {
15961597
// Then:
15971598
// - If int <: C, not num <: C, and T <: int, then the context type of
15981599
// e2 is int.
15991600
// (Note: as above, we check the type of T against `int?`, because it's
16001601
// equivalent and leads to better error recovery.)
16011602
var intType = typeProvider.intType;
1602-
var intTypeQuestion = makeNullable(intType as InterfaceTypeImpl);
16031603
if (isSubtypeOf(intType, c) &&
16041604
!isSubtypeOf(numType, c) &&
1605-
isSubtypeOf(t, intTypeQuestion)) {
1605+
isSubtypeOf(t, typeProvider.intTypeQuestion)) {
16061606
return intType;
16071607
}
16081608
// - If double <: C, not num <: C, and not T <: double, then the context
16091609
// type of e2 is double.
16101610
// (Note: as above, we check the type of T against `double?`, because
16111611
// it's equivalent and leads to better error recovery.)
16121612
var doubleType = typeProvider.doubleType;
1613-
var doubleTypeQuestion = makeNullable(doubleType as InterfaceTypeImpl);
16141613
if (isSubtypeOf(doubleType, c) &&
16151614
!isSubtypeOf(numType, c) &&
1616-
!isSubtypeOf(t, doubleTypeQuestion)) {
1615+
!isSubtypeOf(t, typeProvider.doubleTypeQuestion)) {
16171616
return doubleType;
16181617
}
16191618
// Otherwise, the context type of e2 is num.
@@ -1638,27 +1637,24 @@ class TypeSystemImpl extends TypeSystem2 {
16381637
var t = targetType;
16391638
assert(!t.isBottom);
16401639
var numType = typeProvider.numType;
1641-
var numTypeQuestion = makeNullable(numType as InterfaceTypeImpl);
1642-
if (isSubtypeOf(t, numTypeQuestion)) {
1640+
if (isSubtypeOf(t, typeProvider.numTypeQuestion)) {
16431641
// Then:
16441642
// - If int <: C, not num <: C, and T <: int, then the context type of
16451643
// e2 and e3 is int.
16461644
// (Note: as above, we check the type of T against `int?`, because it's
16471645
// equivalent and leads to better error recovery.)
16481646
var intType = typeProvider.intType;
1649-
var intTypeQuestion = makeNullable(intType as InterfaceTypeImpl);
16501647
if (isSubtypeOf(intType, c) &&
16511648
!isSubtypeOf(numType, c) &&
1652-
isSubtypeOf(t, intTypeQuestion)) {
1649+
isSubtypeOf(t, typeProvider.intTypeQuestion)) {
16531650
return intType;
16541651
}
16551652
// - If double <: C, not num <: C, and T <: double, then the context
16561653
// type of e2 and e3 is double.
16571654
var doubleType = typeProvider.doubleType;
1658-
var doubleTypeQuestion = makeNullable(doubleType as InterfaceTypeImpl);
16591655
if (isSubtypeOf(doubleType, c) &&
16601656
!isSubtypeOf(numType, c) &&
1661-
isSubtypeOf(t, doubleTypeQuestion)) {
1657+
isSubtypeOf(t, typeProvider.doubleTypeQuestion)) {
16621658
return doubleType;
16631659
}
16641660
// - Otherwise the context type of e2 an e3 is num.
@@ -1695,9 +1691,7 @@ class TypeSystemImpl extends TypeSystem2 {
16951691
// `int? + int` to resolve to `int` rather than `num`).
16961692
var t = targetType;
16971693
assert(!t.isBottom);
1698-
var numType = typeProvider.numType;
1699-
var numTypeQuestion = makeNullable(numType as InterfaceTypeImpl);
1700-
if (isSubtypeOf(t, numTypeQuestion)) {
1694+
if (isSubtypeOf(t, typeProvider.numTypeQuestion)) {
17011695
// ...and where the static type of e2 is S and S is assignable to num.
17021696
// (Note: we don't have to check that S is assignable to num because
17031697
// this is required by the signature of the method.)
@@ -1709,8 +1703,7 @@ class TypeSystemImpl extends TypeSystem2 {
17091703
// (Note: as above, we check against `double?` because it's equivalent
17101704
// and leads to better error recovery.)
17111705
var doubleType = typeProvider.doubleType;
1712-
var doubleTypeQuestion =
1713-
makeNullable(doubleType as InterfaceTypeImpl);
1706+
var doubleTypeQuestion = typeProvider.doubleTypeQuestion;
17141707
if (isSubtypeOf(t, doubleTypeQuestion)) {
17151708
return doubleType;
17161709
}
@@ -1723,15 +1716,14 @@ class TypeSystemImpl extends TypeSystem2 {
17231716
// - If T <: int, S <: int and not S <: Never, then the static type of
17241717
// e is int.
17251718
// (As above, we check against `int?` for error recovery.)
1726-
var intType = typeProvider.intType;
1727-
var intTypeQuestion = makeNullable(intType as InterfaceTypeImpl);
1719+
var intTypeQuestion = typeProvider.intTypeQuestion;
17281720
if (!s.isBottom &&
17291721
isSubtypeOf(t, intTypeQuestion) &&
17301722
isSubtypeOf(s, intTypeQuestion)) {
1731-
return intType;
1723+
return typeProvider.intType;
17321724
}
17331725
// - Otherwise the static type of e is num.
1734-
return numType;
1726+
return typeProvider.numType;
17351727
}
17361728
}
17371729
}
@@ -1755,8 +1747,7 @@ class TypeSystemImpl extends TypeSystem2 {
17551747
// `int?.clamp(int, int)` to resolve to `int` rather than `num`).
17561748
// - We don't check that T2 and T3 are subtypes of num because the
17571749
// signature of `num.clamp` requires it.
1758-
var numType = typeProvider.numType;
1759-
var numTypeQuestion = makeNullable(numType as InterfaceTypeImpl);
1750+
var numTypeQuestion = typeProvider.numTypeQuestion;
17601751
if (isSubtypeOf(t1, numTypeQuestion) && !t2.isBottom && !t3.isBottom) {
17611752
assert(!t1.isBottom);
17621753
assert(isSubtypeOf(t2, numTypeQuestion));
@@ -1766,26 +1757,23 @@ class TypeSystemImpl extends TypeSystem2 {
17661757
// int.
17671758
// (Note: as above, we check against `int?` because it's equivalent
17681759
// and leads to better error recovery.)
1769-
var intType = typeProvider.intType;
1770-
var intTypeQuestion = makeNullable(intType as InterfaceTypeImpl);
1760+
var intTypeQuestion = typeProvider.intTypeQuestion;
17711761
if (isSubtypeOf(t1, intTypeQuestion) &&
17721762
isSubtypeOf(t2, intTypeQuestion) &&
17731763
isSubtypeOf(t3, intTypeQuestion)) {
1774-
return intType;
1764+
return typeProvider.intType;
17751765
}
17761766
// If T1, T2 and T3 are all subtypes of double, the static type of e
17771767
// is double.
17781768
// (As above, we check against `double?` for error recovery.)
1779-
var doubleType = typeProvider.doubleType;
1780-
var doubleTypeQuestion =
1781-
makeNullable(doubleType as InterfaceTypeImpl);
1769+
var doubleTypeQuestion = typeProvider.doubleTypeQuestion;
17821770
if (isSubtypeOf(t1, doubleTypeQuestion) &&
17831771
isSubtypeOf(t2, doubleTypeQuestion) &&
17841772
isSubtypeOf(t3, doubleTypeQuestion)) {
1785-
return doubleType;
1773+
return typeProvider.doubleType;
17861774
}
17871775
// Otherwise the static type of e is num.
1788-
return numType;
1776+
return typeProvider.numType;
17891777
}
17901778
}
17911779
}

0 commit comments

Comments
 (0)