@@ -9,6 +9,7 @@ import '../elements/entities.dart';
9
9
import '../elements/types.dart' ;
10
10
import '../js/js.dart' as js;
11
11
import '../js_backend/native_data.dart' show NativeBasicData;
12
+ import '../options.dart' ;
12
13
import '../serialization/serialization.dart' ;
13
14
import '../universe/side_effects.dart' show SideEffects;
14
15
import 'js.dart' ;
@@ -737,6 +738,8 @@ abstract class BehaviorBuilder {
737
738
NativeBasicData get nativeBasicData;
738
739
bool get trustJSInteropTypeAnnotations;
739
740
ElementEnvironment get elementEnvironment;
741
+ CompilerOptions get options;
742
+ DartTypes get dartTypes => commonElements.dartTypes;
740
743
741
744
NativeBehavior _behavior;
742
745
@@ -796,6 +799,7 @@ abstract class BehaviorBuilder {
796
799
/// We assume that JS-interop APIs cannot instantiate Dart types or
797
800
/// non-JSInterop native types.
798
801
void _capture (DartType type, bool isJsInterop) {
802
+ type = type.withoutNullability;
799
803
if (type is FunctionType ) {
800
804
FunctionType functionType = type;
801
805
_capture (functionType.returnType, isJsInterop);
@@ -813,9 +817,7 @@ abstract class BehaviorBuilder {
813
817
_behavior.typesInstantiated.add (type);
814
818
}
815
819
816
- if (! trustJSInteropTypeAnnotations ||
817
- type is DynamicType ||
818
- type == commonElements.objectType) {
820
+ if (! trustJSInteropTypeAnnotations || dartTypes.isTopType (type)) {
819
821
// By saying that only JS-interop types can be created, we prevent
820
822
// pulling in every other native type (e.g. all of dart:html) when a
821
823
// JS interop API returns dynamic or when we don't trust the type
@@ -842,6 +844,18 @@ abstract class BehaviorBuilder {
842
844
_behavior.sideEffects.setAllSideEffects ();
843
845
}
844
846
847
+ void _addReturnType (DartType type) {
848
+ _behavior.typesReturned.add (type.withoutNullability);
849
+
850
+ // Breakdown nullable type into TypeWithoutNullability|Null.
851
+ // Pre-nnbd Declared types are nullable, so we also add null in that case.
852
+ if (type is NullableType ||
853
+ type is LegacyType ||
854
+ (! options.useNullSafety && type is ! VoidType )) {
855
+ _behavior.typesReturned.add (commonElements.nullType);
856
+ }
857
+ }
858
+
845
859
NativeBehavior buildFieldLoadBehavior (
846
860
DartType type,
847
861
Iterable <String > createsAnnotations,
@@ -850,11 +864,9 @@ abstract class BehaviorBuilder {
850
864
{bool isJsInterop}) {
851
865
_behavior = new NativeBehavior ();
852
866
// TODO(sigmund,sra): consider doing something better for numeric types.
853
- _behavior.typesReturned. add (! isJsInterop || trustJSInteropTypeAnnotations
867
+ _addReturnType (! isJsInterop || trustJSInteropTypeAnnotations
854
868
? type
855
869
: commonElements.dynamicType);
856
- // Declared types are nullable.
857
- _behavior.typesReturned.add (commonElements.nullType);
858
870
_capture (type, isJsInterop);
859
871
_overrideWithAnnotations (
860
872
createsAnnotations, returnsAnnotations, lookupType);
@@ -888,13 +900,9 @@ abstract class BehaviorBuilder {
888
900
// an interop call returns a DOM type and declares a dynamic return type,
889
901
// but otherwise we would include a lot of code by default).
890
902
// TODO(sigmund,sra): consider doing something better for numeric types.
891
- _behavior.typesReturned. add (! isJsInterop || trustJSInteropTypeAnnotations
903
+ _addReturnType (! isJsInterop || trustJSInteropTypeAnnotations
892
904
? returnType
893
905
: commonElements.dynamicType);
894
- if (type.returnType is ! VoidType ) {
895
- // Declared types are nullable.
896
- _behavior.typesReturned.add (commonElements.nullType);
897
- }
898
906
_capture (type, isJsInterop);
899
907
900
908
for (DartType type in type.optionalParameterTypes) {
0 commit comments