2020-11-16 22:58:50 +00:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
// Requirements=nnbd-weak
|
|
|
|
|
2023-02-14 20:13:57 +00:00
|
|
|
// ddcOptions=--weak-null-safety-errors
|
2020-11-16 22:58:50 +00:00
|
|
|
|
|
|
|
import 'package:expect/expect.dart';
|
|
|
|
|
2023-09-08 18:39:53 +00:00
|
|
|
/// Code that runs without error when running with unsound null safety but
|
|
|
|
/// should throw in sound mode or when running DDC with
|
|
|
|
/// `--weak-null-safety-errors`.
|
|
|
|
|
|
|
|
void fn(StringBuffer arg) {}
|
2023-10-19 21:32:15 +00:00
|
|
|
void testArg<T>(T t) => throw 'do not call';
|
|
|
|
T testReturn<T>() => throw 'do not call';
|
2023-09-08 18:39:53 +00:00
|
|
|
|
|
|
|
const c = C<Duration>();
|
|
|
|
|
|
|
|
class C<T> {
|
|
|
|
covariantCheck(List<T> t) {}
|
|
|
|
const C();
|
|
|
|
}
|
|
|
|
|
2020-11-16 22:58:50 +00:00
|
|
|
void main() {
|
|
|
|
Expect.throwsTypeError(() => null as int);
|
|
|
|
dynamic dynamicNull = null;
|
|
|
|
Expect.throwsTypeError(() => fn(dynamicNull));
|
|
|
|
|
2023-10-19 21:32:15 +00:00
|
|
|
Expect.throwsTypeError(() => [Duration(days: 1), null] as List<Duration>);
|
2023-09-08 18:39:53 +00:00
|
|
|
|
|
|
|
// Constants get legacy types introduced in their type arguments.
|
|
|
|
C<Duration?> c2 = c;
|
|
|
|
Expect.throwsTypeError(() => c2.covariantCheck([Duration(days: 1), null]));
|
|
|
|
|
|
|
|
// Tearoff instantiations are "potentially constant" and are treated as a
|
|
|
|
// constant by the CFE.
|
|
|
|
// When compiling for unsound null safety the resulting type signature
|
|
|
|
// attached to the tearoff is `void Function(Duration*)` which is a valid
|
|
|
|
// subtype of `void Function(Duration?)`. In sound null safety the signature
|
|
|
|
// is `void Function(Duration)` which should fail in the cast.
|
|
|
|
Expect.throwsTypeError(() => (testArg<Duration>) as void Function(Duration?));
|
2023-10-19 21:32:15 +00:00
|
|
|
Expect.throwsTypeError(() => (testReturn<Duration?>) as Duration Function());
|
2023-09-08 18:39:53 +00:00
|
|
|
}
|