OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // Verify semantics of the ?. operator when it does not appear on the LHS of an |
| 6 // assignment. |
| 7 |
| 8 import "package:expect/expect.dart"; |
| 9 import "conditional_access_helper.dart" as h; |
| 10 |
| 11 noMethod(e) => e is NoSuchMethodError; |
| 12 |
| 13 class B {} |
| 14 |
| 15 class C extends B { |
| 16 int v; |
| 17 C(this.v); |
| 18 static int staticInt; |
| 19 } |
| 20 |
| 21 C nullC() => null; |
| 22 |
| 23 main() { |
| 24 // Make sure the "none" test fails if property access using "?." is not |
| 25 // implemented. This makes status files easier to maintain. |
| 26 nullC()?.v; |
| 27 |
| 28 // e1?.id is equivalent to ((x) => x == null ? null : x.id)(e1). |
| 29 Expect.equals(null, nullC()?.v); /// 01: ok |
| 30 Expect.equals(1, new C(1)?.v); /// 02: ok |
| 31 |
| 32 // C?.id is equivalent to C.id. |
| 33 { C.staticInt = 1; Expect.equals(1, C?.staticInt); } /// 12: ok |
| 34 { h.C.staticInt = 1; Expect.equals(1, h.C?.staticInt); } /// 13: ok |
| 35 |
| 36 // The static type of e1?.d is the static type of e1.id. |
| 37 { int i = new C(1)?.v; Expect.equals(1, i); } /// 03: ok |
| 38 { String s = new C(null)?.v; Expect.equals(null, s); } /// 04: static type war
ning |
| 39 { C.staticInt = 1; int i = C?.staticInt; Expect.equals(1, i); } /// 14: ok |
| 40 { h.C.staticInt = 1; int i = h.C?.staticInt; Expect.equals(1, i); } /// 15: ok |
| 41 { C.staticInt = null; String s = C?.staticInt; Expect.equals(null, s); } /// 1
6: static type warning |
| 42 { h.C.staticInt = null; String s = h.C?.staticInt; Expect.equals(null, s); } /
// 17: static type warning |
| 43 |
| 44 // Let T be the static type of e1 and let y be a fresh variable of type T. |
| 45 // Exactly the same static warnings that would be caused by y.id are also |
| 46 // generated in the case of e1?.id. |
| 47 Expect.equals(null, nullC()?.bad); /// 05: static type warning |
| 48 { B b = new C(1); Expect.equals(1, b?.v); } /// 06: static type warning |
| 49 |
| 50 // '?.' cannot be used to access toplevel properties in libraries imported via |
| 51 // prefix. |
| 52 var x = h?.topLevelVar; /// 09: compile-time error |
| 53 |
| 54 // Nor can it be used to access the hashCode getter on the class Type. |
| 55 Expect.throws(() => C?.hashCode, noMethod); /// 10: static type warning |
| 56 Expect.throws(() => h.C?.hashCode, noMethod); /// 11: static type warning |
| 57 } |
OLD | NEW |