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 that the static type of a ??= b is the least upper bound of the |
| 6 // static types of a and b. |
| 7 |
| 8 import "package:expect/expect.dart"; |
| 9 |
| 10 // Determine whether the VM is running in checked mode. |
| 11 // TODO(jmesserly): changed this to the DDC-friendly pattern. |
| 12 bool get checkedMode { |
| 13 bool checked = false; |
| 14 assert((checked = true)); |
| 15 return checked; |
| 16 } |
| 17 |
| 18 noMethod(e) => e is NoSuchMethodError; |
| 19 |
| 20 bad() { |
| 21 Expect.fail('Should not be executed'); |
| 22 } |
| 23 |
| 24 class A { |
| 25 String a; |
| 26 } |
| 27 |
| 28 class B extends A { |
| 29 String b; |
| 30 } |
| 31 |
| 32 class C extends A { |
| 33 String c; |
| 34 } |
| 35 |
| 36 A get a => null; |
| 37 |
| 38 void set a(A value) {} |
| 39 |
| 40 B get b => null; |
| 41 |
| 42 void set b(B value) {} |
| 43 |
| 44 class ClassWithStaticGetters { |
| 45 static A get a => null; |
| 46 |
| 47 static void set a(A value) {} |
| 48 |
| 49 static B get b => null; |
| 50 |
| 51 static void set b(B value) {} |
| 52 } |
| 53 |
| 54 class ClassWithInstanceGetters { |
| 55 A get a => null; |
| 56 |
| 57 void set a(A value) {} |
| 58 |
| 59 B get b => null; |
| 60 |
| 61 void set b(B value) {} |
| 62 } |
| 63 |
| 64 class DerivedClass extends ClassWithInstanceGetters { |
| 65 A get a => bad(); |
| 66 |
| 67 void set a(A value) { bad(); } |
| 68 |
| 69 B get b => bad(); |
| 70 |
| 71 void set b(B value) { bad(); } |
| 72 |
| 73 void derivedTest() { |
| 74 // The static type of super.v ??= e is the LUB of the static types of |
| 75 // super.v and e. |
| 76 (super.a ??= new A()).a; /// 01: ok |
| 77 Expect.throws(() => (super.a ??= new A()).b, noMethod); /// 02: static type
warning |
| 78 (super.a ??= new B()).a; /// 03: ok |
| 79 (super.a ??= new B()).b; /// 04: static type warning |
| 80 if (!checkedMode) { |
| 81 (super.b ??= new A()).a; /// 05: ok |
| 82 Expect.throws(() => (super.b ??= new A()).b, noMethod); /// 06: static typ
e warning |
| 83 |
| 84 // Exactly the same static warnings that would be caused by super.v = e |
| 85 // are also generated in the case of super.v ??= e. |
| 86 super.b ??= new C(); /// 07: static type warning |
| 87 } |
| 88 } |
| 89 } |
| 90 |
| 91 main() { |
| 92 // Make sure the "none" test fails if "??=" is not implemented. This makes |
| 93 // status files easier to maintain. |
| 94 var _; _ ??= null; |
| 95 |
| 96 new DerivedClass().derivedTest(); |
| 97 |
| 98 // The static type of v ??= e is the LUB of the static types of v and e. |
| 99 (a ??= new A()).a; /// 08: ok |
| 100 Expect.throws(() => (a ??= new A()).b, noMethod); /// 09: static type warning |
| 101 (a ??= new B()).a; /// 10: ok |
| 102 (a ??= new B()).b; /// 11: static type warning |
| 103 if (!checkedMode) { |
| 104 (b ??= new A()).a; /// 12: ok |
| 105 Expect.throws(() => (b ??= new A()).b, noMethod); /// 13: static type warnin
g |
| 106 |
| 107 // Exactly the same static warnings that would be caused by v = e are also |
| 108 // generated in the case of v ??= e. |
| 109 b ??= new C(); /// 14: static type warning |
| 110 } |
| 111 |
| 112 // The static type of C.v ??= e is the LUB of the static types of C.v and e. |
| 113 (ClassWithStaticGetters.a ??= new A()).a; /// 15: ok |
| 114 Expect.throws(() => (ClassWithStaticGetters.a ??= new A()).b, noMethod); /// 1
6: static type warning |
| 115 (ClassWithStaticGetters.a ??= new B()).a; /// 17: ok |
| 116 (ClassWithStaticGetters.a ??= new B()).b; /// 18: static type warning |
| 117 if (!checkedMode) { |
| 118 (ClassWithStaticGetters.b ??= new A()).a; /// 19: ok |
| 119 Expect.throws(() => (ClassWithStaticGetters.b ??= new A()).b, noMethod); ///
20: static type warning |
| 120 |
| 121 // Exactly the same static warnings that would be caused by C.v = e are |
| 122 // also generated in the case of C.v ??= e. |
| 123 ClassWithStaticGetters.b ??= new C(); /// 21: static type warning |
| 124 } |
| 125 |
| 126 // The static type of e1.v ??= e2 is the LUB of the static types of e1.v and |
| 127 // e2. |
| 128 (new ClassWithInstanceGetters().a ??= new A()).a; /// 22: ok |
| 129 Expect.throws(() => (new ClassWithInstanceGetters().a ??= new A()).b, noMethod
); /// 23: static type warning |
| 130 (new ClassWithInstanceGetters().a ??= new B()).a; /// 24: ok |
| 131 (new ClassWithInstanceGetters().a ??= new B()).b; /// 25: static type warning |
| 132 if (!checkedMode) { |
| 133 (new ClassWithInstanceGetters().b ??= new A()).a; /// 26: ok |
| 134 Expect.throws(() => (new ClassWithInstanceGetters().b ??= new A()).b, noMeth
od); /// 27: static type warning |
| 135 |
| 136 // Exactly the same static warnings that would be caused by e1.v = e2 are |
| 137 // also generated in the case of e1.v ??= e2. |
| 138 new ClassWithInstanceGetters().b ??= new C(); /// 28: static type warning |
| 139 } |
| 140 |
| 141 // The static type of e1[e2] ??= e3 is the LUB of the static types of e1[e2] |
| 142 // and e3. |
| 143 ((<A>[null])[0] ??= new A()).a; /// 29: ok |
| 144 Expect.throws(() => ((<A>[null])[0] ??= new A()).b, noMethod); /// 30: static
type warning |
| 145 ((<A>[null])[0] ??= new B()).a; /// 31: ok |
| 146 ((<A>[null])[0] ??= new B()).b; /// 32: static type warning |
| 147 if (!checkedMode) { |
| 148 ((<B>[null])[0] ??= new A()).a; /// 33: ok |
| 149 Expect.throws(() => ((<B>[null])[0] ??= new A()).b, noMethod); /// 34: stati
c type warning |
| 150 |
| 151 // Exactly the same static warnings that would be caused by e1[e2] = e3 are |
| 152 // also generated in the case of e1[e2] ??= e3. |
| 153 (<B>[null])[0] ??= new C(); /// 35: static type warning |
| 154 } |
| 155 |
| 156 // The static type of e1?.v op= e2 is the static type of e1.v op e2, |
| 157 // therefore the static type of e1?.v ??= e2 is the static type of |
| 158 // e1.v ?? e2, which is the LUB of the static types of e1?.v and e2. |
| 159 (new ClassWithInstanceGetters()?.a ??= new A()).a; /// 36: ok |
| 160 Expect.throws(() => (new ClassWithInstanceGetters()?.a ??= new A()).b, noMetho
d); /// 37: static type warning |
| 161 (new ClassWithInstanceGetters()?.a ??= new B()).a; /// 38: ok |
| 162 (new ClassWithInstanceGetters()?.a ??= new B()).b; /// 39: static type warning |
| 163 if (!checkedMode) { |
| 164 (new ClassWithInstanceGetters()?.b ??= new A()).a; /// 40: ok |
| 165 Expect.throws(() => (new ClassWithInstanceGetters()?.b ??= new A()).b, noMet
hod); /// 41: static type warning |
| 166 |
| 167 // Exactly the same static warnings that would be caused by e1.v ??= e2 are |
| 168 // also generated in the case of e1?.v ??= e2. |
| 169 new ClassWithInstanceGetters()?.b ??= new C(); /// 42: static type warning |
| 170 } |
| 171 } |
OLD | NEW |