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