@@ -9,6 +9,7 @@ import '../elements/entities.dart';
99import '../elements/types.dart' ;
1010import '../js/js.dart' as js;
1111import '../js_backend/native_data.dart' show NativeBasicData;
12+ import '../options.dart' ;
1213import '../serialization/serialization.dart' ;
1314import '../universe/side_effects.dart' show SideEffects;
1415import 'js.dart' ;
@@ -737,6 +738,8 @@ abstract class BehaviorBuilder {
737738 NativeBasicData get nativeBasicData;
738739 bool get trustJSInteropTypeAnnotations;
739740 ElementEnvironment get elementEnvironment;
741+ CompilerOptions get options;
742+ DartTypes get dartTypes => commonElements.dartTypes;
740743
741744 NativeBehavior _behavior;
742745
@@ -796,6 +799,7 @@ abstract class BehaviorBuilder {
796799 /// We assume that JS-interop APIs cannot instantiate Dart types or
797800 /// non-JSInterop native types.
798801 void _capture (DartType type, bool isJsInterop) {
802+ type = type.withoutNullability;
799803 if (type is FunctionType ) {
800804 FunctionType functionType = type;
801805 _capture (functionType.returnType, isJsInterop);
@@ -813,9 +817,7 @@ abstract class BehaviorBuilder {
813817 _behavior.typesInstantiated.add (type);
814818 }
815819
816- if (! trustJSInteropTypeAnnotations ||
817- type is DynamicType ||
818- type == commonElements.objectType) {
820+ if (! trustJSInteropTypeAnnotations || dartTypes.isTopType (type)) {
819821 // By saying that only JS-interop types can be created, we prevent
820822 // pulling in every other native type (e.g. all of dart:html) when a
821823 // JS interop API returns dynamic or when we don't trust the type
@@ -842,6 +844,18 @@ abstract class BehaviorBuilder {
842844 _behavior.sideEffects.setAllSideEffects ();
843845 }
844846
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+
845859 NativeBehavior buildFieldLoadBehavior (
846860 DartType type,
847861 Iterable <String > createsAnnotations,
@@ -850,11 +864,9 @@ abstract class BehaviorBuilder {
850864 {bool isJsInterop}) {
851865 _behavior = new NativeBehavior ();
852866 // TODO(sigmund,sra): consider doing something better for numeric types.
853- _behavior.typesReturned. add (! isJsInterop || trustJSInteropTypeAnnotations
867+ _addReturnType (! isJsInterop || trustJSInteropTypeAnnotations
854868 ? type
855869 : commonElements.dynamicType);
856- // Declared types are nullable.
857- _behavior.typesReturned.add (commonElements.nullType);
858870 _capture (type, isJsInterop);
859871 _overrideWithAnnotations (
860872 createsAnnotations, returnsAnnotations, lookupType);
@@ -888,13 +900,9 @@ abstract class BehaviorBuilder {
888900 // an interop call returns a DOM type and declares a dynamic return type,
889901 // but otherwise we would include a lot of code by default).
890902 // TODO(sigmund,sra): consider doing something better for numeric types.
891- _behavior.typesReturned. add (! isJsInterop || trustJSInteropTypeAnnotations
903+ _addReturnType (! isJsInterop || trustJSInteropTypeAnnotations
892904 ? returnType
893905 : commonElements.dynamicType);
894- if (type.returnType is ! VoidType ) {
895- // Declared types are nullable.
896- _behavior.typesReturned.add (commonElements.nullType);
897- }
898906 _capture (type, isJsInterop);
899907
900908 for (DartType type in type.optionalParameterTypes) {
0 commit comments