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 appears on the LHS of an |
| 6 // assignment. |
| 7 |
| 8 import "package:expect/expect.dart"; |
| 9 import "conditional_access_helper.dart" as h; |
| 10 |
| 11 bad() { |
| 12 Expect.fail('Should not be executed'); |
| 13 } |
| 14 |
| 15 noMethod(e) => e is NoSuchMethodError; |
| 16 |
| 17 class B {} |
| 18 |
| 19 class C extends B { |
| 20 int v; |
| 21 C(this.v); |
| 22 static int staticInt; |
| 23 } |
| 24 |
| 25 class D { |
| 26 E v; |
| 27 D(this.v); |
| 28 static E staticE; |
| 29 } |
| 30 |
| 31 class E { |
| 32 G operator+(int i) => new I(); |
| 33 } |
| 34 |
| 35 class F {} |
| 36 |
| 37 class G extends E implements F {} |
| 38 |
| 39 class H {} |
| 40 |
| 41 class I extends G implements H {} |
| 42 |
| 43 C nullC() => null; |
| 44 |
| 45 main() { |
| 46 // Make sure the "none" test fails if assignment to "?." is not implemented. |
| 47 // This makes status files easier to maintain. |
| 48 nullC()?.v = 1; |
| 49 |
| 50 // e1?.v = e2 is equivalent to ((x) => x == null ? null : x.v = e2)(e1). |
| 51 Expect.equals(null, nullC()?.v = bad()); /// 01: ok |
| 52 { C c = new C(1); Expect.equals(2, c?.v = 2); Expect.equals(2, c.v); } /// 02:
ok |
| 53 |
| 54 // C?.v = e2 is equivalent to C.v = e2. |
| 55 { C.staticInt = 1; Expect.equals(2, C?.staticInt = 2); Expect.equals(2, C.stat
icInt); } /// 23: ok |
| 56 { h.C.staticInt = 1; Expect.equals(2, h.C?.staticInt = 2); Expect.equals(2, h.
C.staticInt); } /// 24: ok |
| 57 |
| 58 // The static type of e1?.v = e2 is the static type of e2. |
| 59 { D d = new D(new E()); G g = new G(); F f = (d?.v = g); Expect.identical(f, g
); } /// 03: ok |
| 60 { D d = new D(new E()); E e = new G(); F f = (d?.v = e); Expect.identical(f, e
); } /// 04: static type warning |
| 61 { D.staticE = new E(); G g = new G(); F f = (D?.staticE = g); Expect.identical
(f, g); } /// 25: ok |
| 62 { h.D.staticE = new h.E(); h.G g = new h.G(); h.F f = (h.D?.staticE = g); Expe
ct.identical(f, g); } /// 26: ok |
| 63 { D.staticE = new E(); E e = new G(); F f = (D?.staticE = e); Expect.identical
(f, e); } /// 27: static type warning |
| 64 { h.D.staticE = new h.E(); h.E e = new h.G(); h.F f = (h.D?.staticE = e); Expe
ct.identical(f, e); } /// 28: static type warning |
| 65 |
| 66 // Exactly the same static warnings that would be caused by e1.v = e2 are |
| 67 // also generated in the case of e1?.v = e2. |
| 68 Expect.equals(null, nullC()?.bad = bad()); /// 05: static type warning |
| 69 { B b = new C(1); Expect.equals(2, b?.v = 2); Expect.equals(2, (b as C).v); }
/// 06: static type warning |
| 70 |
| 71 // e1?.v op= e2 is equivalent to ((x) => x?.v = x.v op e2)(e1). |
| 72 Expect.equals(null, nullC()?.v += bad()); /// 07: ok |
| 73 { C c = new C(1); Expect.equals(3, c?.v += 2); Expect.equals(3, c.v); } /// 08
: ok |
| 74 |
| 75 // C?.v op= e2 is equivalent to C.v op= e2. |
| 76 { C.staticInt = 1; Expect.equals(3, C?.staticInt += 2); Expect.equals(3, C?.st
aticInt); } /// 29: ok |
| 77 |
| 78 // The static type of e1?.v op= e2 is the static type of e1.v op e2. |
| 79 { D d = new D(new E()); F f = (d?.v += 1); Expect.identical(d.v, f); } /// 09:
ok |
| 80 { D.staticE = new E(); F f = (D?.staticE += 1); Expect.identical(D.staticE, f)
; } /// 30: ok |
| 81 { h.D.staticE = new h.E(); h.F f = (h.D?.staticE += 1); Expect.identical(h.D.s
taticE, f); } /// 31: ok |
| 82 |
| 83 // Let T be the static type of e1 and let y be a fresh variable of type T. |
| 84 // Exactly the same static warnings that would be caused by y.v op e2 are |
| 85 // also generated in the case of e1?.v op= e2. |
| 86 Expect.equals(null, nullC()?.bad = bad()); /// 10: static type warning |
| 87 { B b = new C(1); Expect.equals(3, b?.v += 2); Expect.equals(3, (b as C).v); }
/// 11: static type warning |
| 88 { D d = new D(new E()); F f = (d?.v += nullC()); Expect.identical(d.v, f); } /
// 12: static type warning |
| 89 { D d = new D(new E()); H h = (d?.v += 1); Expect.identical(d.v, h); } /// 13:
static type warning |
| 90 { D.staticE = new E(); F f = (D?.staticE += nullC()); Expect.identical(D.stati
cE, f); } /// 32: static type warning |
| 91 { h.D.staticE = new h.E(); h.F f = (h.D?.staticE += h.nullC()); Expect.identic
al(h.D.staticE, f); } /// 33: static type warning |
| 92 { D.staticE = new E(); H h = (D?.staticE += 1); Expect.identical(D.staticE, h)
; } /// 34: static type warning |
| 93 { h.D.staticE = new h.E(); h.H hh = (h.D?.staticE += 1); Expect.identical(h.D.
staticE, hh); } /// 35: static type warning |
| 94 |
| 95 // '?.' cannot be used to assign to toplevel properties in libraries imported |
| 96 // via prefix. |
| 97 h?.topLevelVar = null; /// 20: compile-time error |
| 98 h?.topLevelVar += null; /// 21: compile-time error |
| 99 h?.topLevelVar ??= null; /// 22: compile-time error |
| 100 } |
OLD | NEW |