|
| 1 | +// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// dart2jsOptions=--omit-implicit-checks |
| 6 | +// dart2wasmOptions=--omit-implicit-checks |
| 7 | + |
| 8 | +import 'package:expect/expect.dart'; |
| 9 | +import 'package:expect/config.dart'; |
| 10 | + |
| 11 | +final kTrue = int.parse('1') == 1; |
| 12 | + |
| 13 | +void main() { |
| 14 | + // This test is specific to testing dart2wasm & dart2js. |
| 15 | + if (!isDart2jsConfiguration && !isDart2WasmConfiguration) return; |
| 16 | + |
| 17 | + testExplicitAsCheck(); |
| 18 | + |
| 19 | + testCovariantMethodCheck(); |
| 20 | + testDynamicCall(); |
| 21 | + testCovariantKeyword(); |
| 22 | +} |
| 23 | + |
| 24 | +void testExplicitAsCheck() { |
| 25 | + final dynamic x = kTrue ? A() : B(); |
| 26 | + Expect.throws(() => x as B); |
| 27 | +} |
| 28 | + |
| 29 | +void testCovariantMethodCheck() { |
| 30 | + final List<dynamic> list = kTrue ? <String>['a'] : <int>[1]; |
| 31 | + list.add('b'); |
| 32 | + list.add(3); |
| 33 | + Expect.equals('a', list[0]); |
| 34 | + Expect.equals('b', list[1]); |
| 35 | + Expect.equals(3, list[2]); |
| 36 | +} |
| 37 | + |
| 38 | +void testDynamicCall() { |
| 39 | + final dynamic a = kTrue ? (String a) => 'closure($a)' : A(); |
| 40 | + Expect.equals('closure(B)', a(B())); |
| 41 | +} |
| 42 | + |
| 43 | +void testCovariantKeyword() { |
| 44 | + final object = kTrue ? B() : A(); |
| 45 | + |
| 46 | + // Normally the `covariant String` would cause a type error to be thrown, but |
| 47 | + // due to --omit-implicit-checks it works just fine. |
| 48 | + Expect.equals('B.foo(42)', object.foo(42)); |
| 49 | +} |
| 50 | + |
| 51 | +class A { |
| 52 | + String foo(Object arg) => 'A.foo($arg)'; |
| 53 | +} |
| 54 | + |
| 55 | +class B extends A { |
| 56 | + String foo(covariant String arg) => 'B.foo($arg)'; |
| 57 | + |
| 58 | + String toString() => 'B'; |
| 59 | +} |
0 commit comments