OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // Verify semantics of the ?. operator when it is used to invoke a method. | 5 // Verify semantics of the ?. operator when it is used to invoke a method. |
6 | 6 |
7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
8 import "conditional_access_helper.dart" as h; | 8 import "conditional_access_helper.dart" as h; |
9 | 9 |
10 bad() { | 10 bad() { |
(...skipping 12 matching lines...) Expand all Loading... |
23 } | 23 } |
24 | 24 |
25 C nullC() => null; | 25 C nullC() => null; |
26 | 26 |
27 main() { | 27 main() { |
28 // Make sure the "none" test fails if method invocation using "?." is not | 28 // Make sure the "none" test fails if method invocation using "?." is not |
29 // implemented. This makes status files easier to maintain. | 29 // implemented. This makes status files easier to maintain. |
30 nullC()?.f(null); | 30 nullC()?.f(null); |
31 | 31 |
32 // o?.m(...) is equivalent to ((x) => x == null ? null : x.m(...))(o). | 32 // o?.m(...) is equivalent to ((x) => x == null ? null : x.m(...))(o). |
33 Expect.equals(null, nullC()?.f(bad())); /// 01: ok | 33 Expect.equals(null, nullC()?.f(bad())); //# 01: ok |
34 Expect.equals(1, new C()?.f(() => 1)); /// 02: ok | 34 Expect.equals(1, new C()?.f(() => 1)); //# 02: ok |
35 | 35 |
36 // C?.m(...) is equivalent to C.m(...). | 36 // C?.m(...) is equivalent to C.m(...). |
37 Expect.equals(1, C?.staticF(() => 1)); /// 14: ok | 37 Expect.equals(1, C?.staticF(() => 1)); //# 14: ok |
38 Expect.equals(1, h.C?.staticF(() => 1)); /// 15: ok | 38 Expect.equals(1, h.C?.staticF(() => 1)); //# 15: ok |
39 | 39 |
40 // The static type of o?.m(...) is the same as the static type of | 40 // The static type of o?.m(...) is the same as the static type of |
41 // o.m(...). | 41 // o.m(...). |
42 { int i = nullC()?.g(bad()); Expect.equals(null, i); } /// 03: ok | 42 { int i = nullC()?.g(bad()); Expect.equals(null, i); } //# 03: ok |
43 { int i = new C()?.g(() => 1); Expect.equals(1, i); } /// 04: ok | 43 { int i = new C()?.g(() => 1); Expect.equals(1, i); } //# 04: ok |
44 { String s = nullC()?.g(bad()); Expect.equals(null, s); } /// 05: static type
warning | 44 { String s = nullC()?.g(bad()); Expect.equals(null, s); } //# 05: static type
warning |
45 { String s = new C()?.g(() => null); Expect.equals(null, s); } /// 06: static
type warning | 45 { String s = new C()?.g(() => null); Expect.equals(null, s); } //# 06: static
type warning |
46 { int i = C?.staticG(() => 1); Expect.equals(1, i); } /// 16: ok | 46 { int i = C?.staticG(() => 1); Expect.equals(1, i); } //# 16: ok |
47 { int i = h.C?.staticG(() => 1); Expect.equals(1, i); } /// 17: ok | 47 { int i = h.C?.staticG(() => 1); Expect.equals(1, i); } //# 17: ok |
48 { String s = C?.staticG(() => null); Expect.equals(null, s); } /// 18: static
type warning | 48 { String s = C?.staticG(() => null); Expect.equals(null, s); } //# 18: static
type warning |
49 { String s = h.C?.staticG(() => null); Expect.equals(null, s); } /// 19: stati
c type warning | 49 { String s = h.C?.staticG(() => null); Expect.equals(null, s); } //# 19: stati
c type warning |
50 | 50 |
51 // Let T be the static type of o and let y be a fresh variable of type T. | 51 // Let T be the static type of o and let y be a fresh variable of type T. |
52 // Exactly the same static warnings that would be caused by y.m(...) are also | 52 // Exactly the same static warnings that would be caused by y.m(...) are also |
53 // generated in the case of o?.m(...). | 53 // generated in the case of o?.m(...). |
54 { B b = new C(); Expect.equals(1, b?.f(() => 1)); } /// 07: static type warnin
g | 54 { B b = new C(); Expect.equals(1, b?.f(() => 1)); } //# 07: static type warnin
g |
55 { int i = 1; Expect.equals(null, nullC()?.f(i)); } /// 08: static type warning | 55 { int i = 1; Expect.equals(null, nullC()?.f(i)); } //# 08: static type warning |
56 | 56 |
57 // '?.' can't be used to access toplevel functions in libraries imported via | 57 // '?.' can't be used to access toplevel functions in libraries imported via |
58 // prefix. | 58 // prefix. |
59 h?.topLevelFunction(); /// 11: compile-time error | 59 h?.topLevelFunction(); //# 11: compile-time error |
60 | 60 |
61 // Nor can it be used to access the toString method on the class Type. | 61 // Nor can it be used to access the toString method on the class Type. |
62 Expect.throws(() => C?.toString(), noMethod); /// 12: static type warning | 62 Expect.throws(() => C?.toString(), noMethod); //# 12: static type warning |
63 Expect.throws(() => h.C?.toString(), noMethod); /// 13: static type warning | 63 Expect.throws(() => h.C?.toString(), noMethod); //# 13: static type warning |
64 } | 64 } |
OLD | NEW |