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

Commit 2ae10b1

Browse files
joshualittcommit-bot@chromium.org
authored andcommitted
[dart2js] Fix nullability of native methods.
Change-Id: Ie7228802f7d7c3c8c7be174b60714d232e5bfa81 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/138604 Commit-Queue: Joshua Litt <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]> Reviewed-by: Stephen Adams <[email protected]>
1 parent b01cb58 commit 2ae10b1

File tree

3 files changed

+23
-34
lines changed

3 files changed

+23
-34
lines changed

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,26 +2467,6 @@ class JsElementEnvironment extends ElementEnvironment
24672467
}
24682468
}
24692469

2470-
/// [BehaviorBuilder] for kernel based elements.
2471-
class JsBehaviorBuilder extends BehaviorBuilder {
2472-
@override
2473-
final ElementEnvironment elementEnvironment;
2474-
@override
2475-
final CommonElements commonElements;
2476-
@override
2477-
final DiagnosticReporter reporter;
2478-
@override
2479-
final NativeBasicData nativeBasicData;
2480-
final CompilerOptions _options;
2481-
2482-
JsBehaviorBuilder(this.elementEnvironment, this.commonElements,
2483-
this.nativeBasicData, this.reporter, this._options);
2484-
2485-
@override
2486-
bool get trustJSInteropTypeAnnotations =>
2487-
_options.trustJSInteropTypeAnnotations;
2488-
}
2489-
24902470
/// [EntityLookup] implementation used to deserialize [JsKernelToElementMap].
24912471
///
24922472
/// Since data objects and environments are registered together with their

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,14 +1869,15 @@ class KernelBehaviorBuilder extends BehaviorBuilder {
18691869
final DiagnosticReporter reporter;
18701870
@override
18711871
final NativeBasicData nativeBasicData;
1872-
final CompilerOptions _options;
1872+
@override
1873+
final CompilerOptions options;
18731874

18741875
KernelBehaviorBuilder(this.elementEnvironment, this.commonElements,
1875-
this.nativeBasicData, this.reporter, this._options);
1876+
this.nativeBasicData, this.reporter, this.options);
18761877

18771878
@override
18781879
bool get trustJSInteropTypeAnnotations =>
1879-
_options.trustJSInteropTypeAnnotations;
1880+
options.trustJSInteropTypeAnnotations;
18801881
}
18811882

18821883
class KernelNativeMemberResolver implements NativeMemberResolver {

pkg/compiler/lib/src/native/behavior.dart

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import '../elements/entities.dart';
99
import '../elements/types.dart';
1010
import '../js/js.dart' as js;
1111
import '../js_backend/native_data.dart' show NativeBasicData;
12+
import '../options.dart';
1213
import '../serialization/serialization.dart';
1314
import '../universe/side_effects.dart' show SideEffects;
1415
import '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

Comments
 (0)