ca3e67a3b6
This CL adds a new --interop-null-assertions flag (cf. --native-null-assertions) with the goal of validating that JS interop APIs with non-nullable static return types do not return null values. This flag is currently disabled by default but is intended to assist in sound null safety migrations. In general, we don't guarantee type soundness of package:js interop since users can write incorrect static types which are not backed by any runtime checks. However, it is likely that during the null safety migration, some interop APIs which should have been made nullable weren't. Therefore, we want to offer some additional (but limited) checking for this case. For static invocations of functions with non-nullable return types, we can simply perform a null check on the result of the call. For instance methods (which could be invoked virtually/dynamically), we want to perform a null check on the return value in the callee (the interceptor method) itself when possible. It's possible for multiple interop bindings to share the same interceptor method. We produce a null check in the interceptor method body if all the methods have non-nullable return types. Otherwise, we insert checks at callsites when appropriate. Change-Id: Ifd155d7f8326152b6d57d61199e0b7973c4a1211 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/369784 Reviewed-by: Sigmund Cherem <sigmund@google.com> Commit-Queue: Mayank Patke <fishythefish@google.com>
22 lines
805 B
Dart
22 lines
805 B
Dart
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
// dart2jsOptions=--interop-null-assertions
|
|
// ddcOptions=--interop-null-assertions
|
|
|
|
/// When using non-static JavaScript interop via package:js, values flowing
|
|
/// through APIs defined to be non-nullable should be checked for null when
|
|
/// `--interop-null-assertions` is enabled.
|
|
///
|
|
/// - In dart2js this is a compile time flag.
|
|
/// - In DDC this is a runtime flag so the option gets passed to the
|
|
/// bootstrapper script and set in the runtime before invoking the main
|
|
/// method.
|
|
|
|
import 'js_interop_non_null_asserts_utils.dart';
|
|
|
|
void main() {
|
|
runTests(checksEnabled: true);
|
|
}
|