From ecd37e5d835ce705f7d29cdab3eabc8b385e17da Mon Sep 17 00:00:00 2001 From: Nicholas Shahan Date: Thu, 9 Mar 2023 23:43:49 +0000 Subject: [PATCH] [ddc] Default native null asserts on in sound mode This does not necessarily enable the native null assertions by default everywhere but it is a step on the path to get there. - The calls to the method that reads this value are only generated in sound null safety so even if this is true the checks don't happen in weak null safety. Added a warning when the option is enabled in weak null safety. - Many DDC integrations will set the flag manually during the bootstrap. That setting still overrides the default value being changed here. Change-Id: I4459fb8e8928424b461eb5519f3a7dc87c578172 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/286606 Reviewed-by: Mark Zhou Commit-Queue: Nicholas Shahan --- pkg/dev_compiler/tool/ddb | 11 ++++++----- .../js_dev_runtime/private/ddc_runtime/types.dart | 12 +++++++++--- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/pkg/dev_compiler/tool/ddb b/pkg/dev_compiler/tool/ddb index 4c95bf42acf..f52cc0368d5 100755 --- a/pkg/dev_compiler/tool/ddb +++ b/pkg/dev_compiler/tool/ddb @@ -14,8 +14,7 @@ import 'dart:io'; import 'package:args/args.dart' show ArgParser; -import 'package:dev_compiler/src/compiler/js_names.dart' - as js_names; +import 'package:dev_compiler/src/compiler/js_names.dart' as js_names; import 'package:path/path.dart' as p; enum NullSafety { strict, weak, disabled } @@ -50,7 +49,8 @@ void main(List args) async { ..addFlag('sound-null-safety', help: 'Compile for sound null safety at runtime. Passed through to the ' 'DDC binary. Defaults to false.', - negatable: true, defaultsTo: true) + negatable: true, + defaultsTo: true) ..addFlag('emit-debug-symbols', help: 'Pass through flag for DDC, emits debug symbols file along with ' 'the compiled module.', @@ -62,7 +62,6 @@ void main(List args) async { ..addFlag('native-null-assertions', help: 'Run with assertions on non-nullable values returned from native ' 'APIs.', - defaultsTo: true, negatable: true) ..addFlag('weak-null-safety-errors', help: 'Treat weak null safety warnings as errors.', defaultsTo: false) @@ -126,7 +125,9 @@ void main(List args) async { var soundNullSafety = options['sound-null-safety'] as bool; var emitDebugSymbols = options['emit-debug-symbols'] as bool; var nonNullAsserts = options['null-assertions'] as bool; - var nativeNonNullAsserts = options['native-null-assertions'] as bool; + var nativeNonNullAsserts = options.wasParsed('native-null-assertions') + ? options['native-null-assertions'] as bool + : soundNullSafety; var weakNullSafetyErrors = options['weak-null-safety-errors'] as bool; var canaryFeatures = options['canary'] as bool; var entry = p.canonicalize(options.rest.first); diff --git a/sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/types.dart b/sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/types.dart index bf903553ef9..d01fe03a8ed 100644 --- a/sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/types.dart +++ b/sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/types.dart @@ -78,14 +78,20 @@ void nonNullAsserts(bool enable) { } @notNull -bool _nativeNonNullAsserts = false; +bool _nativeNonNullAsserts = compileTimeFlag('soundNullSafety'); -/// Enables null assertions on native APIs to make sure value returned from the -/// browser is sound. +/// Enables null assertions on native APIs to make sure values returned from the +/// browser are sound. /// /// These apply to dart:html and similar web libraries. Note that these only are /// added in sound null-safety only. void nativeNonNullAsserts(bool enable) { + if (enable && !compileTimeFlag('soundNullSafety')) { + _warn('Enabling `native-null-assertions` is only supported when sound null ' + 'safety is enabled.'); + } + // This value is only read from `checkNativeNonNull` and calls to that method + // are only generated in sound null safe code. _nativeNonNullAsserts = enable; }