Unverified Commit cb7770b3 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] add support for enable null safety asserts (#61114)

Enable null safety asserts for web debug mode. This induces runtime asserts at the boundaries between null safe and non-null safe libraries. Adds integration test that validates assertion error is thrown.

#61042
parent 0a64b5b2
...@@ -746,6 +746,7 @@ Future<void> _runWebIntegrationTests() async { ...@@ -746,6 +746,7 @@ Future<void> _runWebIntegrationTests() async {
await _runWebDebugTest('lib/stack_trace.dart'); await _runWebDebugTest('lib/stack_trace.dart');
await _runWebDebugTest('lib/web_directory_loading.dart'); await _runWebDebugTest('lib/web_directory_loading.dart');
await _runWebDebugTest('test/test.dart'); await _runWebDebugTest('test/test.dart');
await _runWebDebugTest('lib/null_assert_main.dart', enableNullSafety: true);
await _runWebDebugTest('lib/null_safe_main.dart', enableNullSafety: true); await _runWebDebugTest('lib/null_safe_main.dart', enableNullSafety: true);
await _runWebDebugTest('lib/web_define_loading.dart', await _runWebDebugTest('lib/web_define_loading.dart',
additionalArguments: <String>[ additionalArguments: <String>[
......
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart=2.8
import 'null_enabled_api.dart';
void main() {
dynamic error;
try {
// Validate that a generated null assertion is thrown.
methodThatAcceptsNonNull(null);
} catch (err) {
error = err;
}
if (error is AssertionError) {
print('--- TEST SUCCEEDED ---');
} else {
print('--- TEST FAILED ---');
}
}
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// @dart=2.9
void methodThatAcceptsNonNull(int x) {
print(x + 2);
}
...@@ -791,6 +791,8 @@ class WebDevFS implements DevFS { ...@@ -791,6 +791,8 @@ class WebDevFS implements DevFS {
'main_module.bootstrap.js', 'main_module.bootstrap.js',
generateMainModule( generateMainModule(
entrypoint: entrypoint, entrypoint: entrypoint,
nullSafety: buildInfo.extraFrontEndOptions
?.contains('--enable-experiment=non-nullable') ?? false,
), ),
); );
// TODO(jonahwilliams): refactor the asset code in this and the regular devfs to // TODO(jonahwilliams): refactor the asset code in this and the regular devfs to
......
...@@ -48,12 +48,18 @@ document.head.appendChild(requireEl); ...@@ -48,12 +48,18 @@ document.head.appendChild(requireEl);
/// the file `foo/bar/baz.dart` will generate a property named approximately /// the file `foo/bar/baz.dart` will generate a property named approximately
/// `foo__bar__baz`. Rather than attempt to guess, we assume the first property of /// `foo__bar__baz`. Rather than attempt to guess, we assume the first property of
/// this object is the module. /// this object is the module.
String generateMainModule({@required String entrypoint}) { String generateMainModule({
@required String entrypoint,
@required bool nullSafety,
}) {
return '''/* ENTRYPOINT_EXTENTION_MARKER */ return '''/* ENTRYPOINT_EXTENTION_MARKER */
// Create the main module loaded below. // Create the main module loaded below.
define("main_module.bootstrap", ["$entrypoint", "dart_sdk"], function(app, dart_sdk) { define("main_module.bootstrap", ["$entrypoint", "dart_sdk"], function(app, dart_sdk) {
dart_sdk.dart.setStartAsyncSynchronously(true); dart_sdk.dart.setStartAsyncSynchronously(true);
dart_sdk._debugger.registerDevtoolsFormatter(); dart_sdk._debugger.registerDevtoolsFormatter();
if ($nullSafety) {
dart_sdk.dart.nonNullAsserts(true);
}
// See the generateMainModule doc comment. // See the generateMainModule doc comment.
var child = {}; var child = {};
......
...@@ -23,9 +23,21 @@ void main() { ...@@ -23,9 +23,21 @@ void main() {
test('generateMainModule embeds urls correctly', () { test('generateMainModule embeds urls correctly', () {
final String result = generateMainModule( final String result = generateMainModule(
entrypoint: 'foo/bar/main.js', entrypoint: 'foo/bar/main.js',
nullSafety: false,
); );
// bootstrap main module has correct defined module. // bootstrap main module has correct defined module.
expect(result, contains('define("main_module.bootstrap", ["foo/bar/main.js", "dart_sdk"], ' expect(result, contains('define("main_module.bootstrap", ["foo/bar/main.js", "dart_sdk"], '
'function(app, dart_sdk) {')); 'function(app, dart_sdk) {'));
}); });
test('generateMainModule includes null safety switches', () {
final String result = generateMainModule(
entrypoint: 'foo/bar/main.js',
nullSafety: true,
);
expect(result, contains(
''' if (true) {
dart_sdk.dart.nonNullAsserts(true);'''));
});
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment