[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 <markzipan@google.com>
Commit-Queue: Nicholas Shahan <nshahan@google.com>
This commit is contained in:
Nicholas Shahan 2023-03-09 23:43:49 +00:00 committed by Commit Queue
parent 624fc6a9d6
commit ecd37e5d83
2 changed files with 15 additions and 8 deletions

View file

@ -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<String> 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<String> 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<String> 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);

View file

@ -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;
}