Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 159fcee

Browse files
joshualittcommit-bot@chromium.org
authored andcommitted
[dart2js] Fix type inference with Future.then
Change-Id: I77fa246bce935b162b2047c3dca0a462d4dfaea7 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/138240 Commit-Queue: Joshua Litt <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]> Reviewed-by: Mayank Patke <[email protected]>
1 parent 54903e5 commit 159fcee

File tree

3 files changed

+41
-29
lines changed

3 files changed

+41
-29
lines changed

pkg/compiler/lib/src/js_backend/runtime_types_resolution.dart

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -390,22 +390,23 @@ class TypeVariableTests {
390390
}
391391

392392
void processCheckedType(DartType type) {
393-
if (type is InterfaceType) {
393+
var typeWithoutNullability = type.withoutNullability;
394+
if (typeWithoutNullability is InterfaceType) {
394395
// Register that if [cls] needs type arguments then so do the entities
395396
// that declare type variables occurring in [type].
396-
ClassEntity cls = type.element;
397-
registerDependencies(_getClassNode(cls), type);
397+
ClassEntity cls = typeWithoutNullability.element;
398+
registerDependencies(_getClassNode(cls), typeWithoutNullability);
398399
}
399-
if (type is FutureOrType) {
400-
// [type] is `FutureOr<X>`.
400+
if (typeWithoutNullability is FutureOrType) {
401+
// [typeWithoutNullability] is `FutureOr<X>`.
401402

402403
// For the implied `is Future<X>` test, register that if `Future` needs
403404
// type arguments then so do the entities that declare type variables
404405
// occurring in `type.typeArgument`.
405-
registerDependencies(
406-
_getClassNode(commonElements.futureClass), type.typeArgument);
406+
registerDependencies(_getClassNode(commonElements.futureClass),
407+
typeWithoutNullability.typeArgument);
407408
// Process `type.typeArgument` for the implied `is X` test.
408-
processCheckedType(type.typeArgument);
409+
processCheckedType(typeWithoutNullability.typeArgument);
409410
}
410411
}
411412

@@ -486,11 +487,12 @@ class TypeVariableTests {
486487
}
487488

488489
void processType(DartType type, {bool direct: true}) {
489-
if (type is FutureOrType) {
490+
var typeWithoutNullability = type.withoutNullability;
491+
if (typeWithoutNullability is FutureOrType) {
490492
_getClassNode(commonElements.futureClass).markIndirectTest();
491-
processType(type.typeArgument, direct: false);
493+
processType(typeWithoutNullability.typeArgument, direct: false);
492494
} else {
493-
type.forEachTypeVariable((TypeVariableType type) {
495+
typeWithoutNullability.forEachTypeVariable((TypeVariableType type) {
494496
processTypeVariableType(type, direct: direct);
495497
});
496498
}
@@ -568,10 +570,12 @@ class TypeVariableTests {
568570
/// If [type] is of the form `FutureOr<X>`, also register the implicit
569571
/// is-tests of `Future<X>` and `X`.
570572
void addImplicitCheck(DartType type) {
571-
if (implicitIsChecks.add(type)) {
572-
if (type is FutureOrType) {
573-
addImplicitCheck(commonElements.futureType(type.typeArgument));
574-
addImplicitCheck(type.typeArgument);
573+
var typeWithoutNullability = type.withoutNullability;
574+
if (implicitIsChecks.add(typeWithoutNullability)) {
575+
if (typeWithoutNullability is FutureOrType) {
576+
addImplicitCheck(
577+
commonElements.futureType(typeWithoutNullability.typeArgument));
578+
addImplicitCheck(typeWithoutNullability.typeArgument);
575579
}
576580
}
577581
}
@@ -581,9 +585,11 @@ class TypeVariableTests {
581585
}
582586

583587
world.isChecks.forEach((DartType type) {
584-
if (type is FutureOrType) {
585-
addImplicitCheck(commonElements.futureType(type.typeArgument));
586-
addImplicitCheck(type.typeArgument);
588+
var typeWithoutNullability = type.withoutNullability;
589+
if (typeWithoutNullability is FutureOrType) {
590+
addImplicitCheck(
591+
commonElements.futureType(typeWithoutNullability.typeArgument));
592+
addImplicitCheck(typeWithoutNullability.typeArgument);
587593
}
588594
});
589595

pkg/compiler/lib/src/js_model/element_map_impl.dart

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,28 +2250,34 @@ class JsElementEnvironment extends ElementEnvironment
22502250
@override
22512251
DartType getAsyncOrSyncStarElementType(
22522252
AsyncMarker asyncMarker, DartType returnType) {
2253+
var returnTypeWithoutNullability = returnType.withoutNullability;
22532254
switch (asyncMarker) {
22542255
case AsyncMarker.SYNC:
22552256
return returnType;
22562257
case AsyncMarker.SYNC_STAR:
2257-
if (returnType is InterfaceType) {
2258-
if (returnType.element == elementMap.commonElements.iterableClass) {
2259-
return returnType.typeArguments.first;
2258+
if (returnTypeWithoutNullability is InterfaceType) {
2259+
if (returnTypeWithoutNullability.element ==
2260+
elementMap.commonElements.iterableClass) {
2261+
return returnTypeWithoutNullability.typeArguments.first;
22602262
}
22612263
}
22622264
return dynamicType;
22632265
case AsyncMarker.ASYNC:
2264-
if (returnType is FutureOrType) return returnType.typeArgument;
2265-
if (returnType is InterfaceType) {
2266-
if (returnType.element == elementMap.commonElements.futureClass) {
2267-
return returnType.typeArguments.first;
2266+
if (returnTypeWithoutNullability is FutureOrType) {
2267+
return returnTypeWithoutNullability.typeArgument;
2268+
}
2269+
if (returnTypeWithoutNullability is InterfaceType) {
2270+
if (returnTypeWithoutNullability.element ==
2271+
elementMap.commonElements.futureClass) {
2272+
return returnTypeWithoutNullability.typeArguments.first;
22682273
}
22692274
}
22702275
return dynamicType;
22712276
case AsyncMarker.ASYNC_STAR:
2272-
if (returnType is InterfaceType) {
2273-
if (returnType.element == elementMap.commonElements.streamClass) {
2274-
return returnType.typeArguments.first;
2277+
if (returnTypeWithoutNullability is InterfaceType) {
2278+
if (returnTypeWithoutNullability.element ==
2279+
elementMap.commonElements.streamClass) {
2280+
return returnTypeWithoutNullability.typeArguments.first;
22752281
}
22762282
}
22772283
return dynamicType;

pkg/compiler/lib/src/kernel/element_map_impl.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1475,7 +1475,7 @@ class KernelToElementMapImpl implements KernelToElementMap, IrToElementMap {
14751475
if (supertype != null) {
14761476
return true;
14771477
}
1478-
return data.callType is FunctionType;
1478+
return data.callType?.withoutNullability is FunctionType;
14791479
}
14801480

14811481
@override

0 commit comments

Comments
 (0)