diff --git a/runtime/tests/vm/dart/type_casts_with_null_safety_autodetection_strong_script.dart b/runtime/tests/vm/dart/type_casts_with_null_safety_autodetection_strong_script.dart new file mode 100644 index 00000000000..08214612fdd --- /dev/null +++ b/runtime/tests/vm/dart/type_casts_with_null_safety_autodetection_strong_script.dart @@ -0,0 +1,29 @@ +// Copyright (c) 2020, 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. + +// Test script for type_casts_with_null_safety_autodetection_test.dart which +// is supposed to run in strong mode because it is opted-in. + +import 'package:expect/expect.dart'; + +dynamic nullObj; + +@pragma('vm:never-inline') +typeCast(x) => x as T; + +doTests() { + typeCast(nullObj); + typeCast(nullObj); + Expect.throwsTypeError(() => typeCast(nullObj)); + Expect.throwsTypeError(() => typeCast(nullObj)); + typeCast>([]); + Expect.throwsTypeError(() => typeCast>([])); +} + +main() { + for (int i = 0; i < 20; ++i) { + doTests(); + } + print('OK(strong)'); +} diff --git a/runtime/tests/vm/dart/type_casts_with_null_safety_autodetection_test.dart b/runtime/tests/vm/dart/type_casts_with_null_safety_autodetection_test.dart new file mode 100644 index 00000000000..701f409ef12 --- /dev/null +++ b/runtime/tests/vm/dart/type_casts_with_null_safety_autodetection_test.dart @@ -0,0 +1,43 @@ +// Copyright (c) 2020, 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. + +// OtherResources=type_casts_with_null_safety_autodetection_strong_script.dart +// OtherResources=type_casts_with_null_safety_autodetection_weak_script.dart + +// This test verifies that casts with type testing stubs work as expected +// if null safety mode (weak/strong) is auto-detected. + +import 'dart:io' show File, Platform; + +import 'package:path/path.dart' as path; +import 'snapshot_test_helper.dart'; + +runTest(String script, String output, String temp) async { + // Need to copy test scripts out of Dart SDK to avoid hardcoded + // opted-in/opted-out status for Dart SDK tests. + final scriptInTemp = path.join(temp, script); + File.fromUri(Platform.script.resolve(script)).copySync(scriptInTemp); + + // Do not add Platform.executableArguments into arguments to avoid passing + // --null-safety / --no-null-safety arguments. + final result = await runBinary("RUN $script", Platform.executable, [ + '--enable-experiment=non-nullable', + '--deterministic', + '--optimization-counter-threshold=10', + '--packages=${Platform.packageConfig}', + scriptInTemp, + ]); + expectOutput(output, result); +} + +main() async { + await withTempDir((String temp) async { + await runTest( + 'type_casts_with_null_safety_autodetection_strong_script.dart', + 'OK(strong)', + temp); + await runTest('type_casts_with_null_safety_autodetection_weak_script.dart', + 'OK(weak)', temp); + }); +} diff --git a/runtime/tests/vm/dart/type_casts_with_null_safety_autodetection_weak_script.dart b/runtime/tests/vm/dart/type_casts_with_null_safety_autodetection_weak_script.dart new file mode 100644 index 00000000000..a12eacd91ad --- /dev/null +++ b/runtime/tests/vm/dart/type_casts_with_null_safety_autodetection_weak_script.dart @@ -0,0 +1,31 @@ +// Copyright (c) 2020, 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. + +// Test script for type_casts_with_null_safety_autodetection_test.dart which +// is supposed to run in weak mode because it is opted-out. + +// @dart=2.6 + +import 'package:expect/expect.dart'; + +dynamic nullObj; + +@pragma('vm:never-inline') +typeCast(x) => x as T; + +void doTests() { + typeCast(nullObj); + typeCast(nullObj); + typeCast(nullObj); + typeCast(nullObj); + typeCast>([]); + typeCast>([]); +} + +main() { + for (int i = 0; i < 20; ++i) { + doTests(); + } + print('OK(weak)'); +} diff --git a/runtime/tests/vm/vm.status b/runtime/tests/vm/vm.status index c8912f5999e..72ce9c8371b 100644 --- a/runtime/tests/vm/vm.status +++ b/runtime/tests/vm/vm.status @@ -248,6 +248,7 @@ dart/minimal_kernel_bytecode_test: SkipByDesign # Test needs to run from source dart/minimal_kernel_test: SkipByDesign # Test needs to run from source dart/null_safety_autodetection_in_kernel_compiler_test: SkipByDesign # Test needs to run from source dart/snapshot_depfile_test: SkipByDesign # Test needs to run from source +dart/type_casts_with_null_safety_autodetection_test: SkipByDesign # Test needs to run from source dart_2/appjit*: SkipByDesign # Test needs to run from source dart_2/bytecode_and_ast_mix_test: SkipByDesign # Test needs to run from source dart_2/kernel_determinism_test: SkipByDesign # Test needs to run from source diff --git a/runtime/vm/dart_entry.cc b/runtime/vm/dart_entry.cc index 6c306ac5af9..8a1350e6490 100644 --- a/runtime/vm/dart_entry.cc +++ b/runtime/vm/dart_entry.cc @@ -188,6 +188,7 @@ ObjectPtr DartEntry::InvokeCode(const Code& code, Thread* thread) { ASSERT(!code.IsNull()); ASSERT(thread->no_callback_scope_depth() == 0); + ASSERT(!Isolate::Current()->null_safety_not_set()); invokestub entrypoint = reinterpret_cast(StubCode::InvokeDartCode().EntryPoint());