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