| 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) { | |
| 68 bad(); | |
| 69 } | |
| 70 | |
| 71 B get b => bad(); | |
| 72 | |
| 73 void set b(B value) { | |
| 74 bad(); | |
| 75 } | |
| 76 | |
| 77 void derivedTest() { | |
| 78 // The static type of super.v ??= e is the LUB of the static types of | |
| 79 // super.v and e. | |
| 80 (super.a ??= new A()).a; //# 01: ok | |
| 81 Expect.throws(() => (super.a ??= new A()).b, noMethod); //# 02: static type
warning | |
| 82 (super.a ??= new B()).a; //# 03: ok | |
| 83 (super.a ??= new B()).b; //# 04: static type warning | |
| 84 if (!checkedMode) { | |
| 85 (super.b ??= new A()).a; //# 05: ok | |
| 86 Expect.throws(() => (super.b ??= new A()).b, noMethod); //# 06: static typ
e warning | |
| 87 | |
| 88 // Exactly the same static warnings that would be caused by super.v = e | |
| 89 // are also generated in the case of super.v ??= e. | |
| 90 super.b ??= new C(); //# 07: static type warning | |
| 91 } | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 main() { | |
| 96 // Make sure the "none" test fails if "??=" is not implemented. This makes | |
| 97 // status files easier to maintain. | |
| 98 var _; | |
| 99 _ ??= null; | |
| 100 | |
| 101 new DerivedClass().derivedTest(); | |
| 102 | |
| 103 // The static type of v ??= e is the LUB of the static types of v and e. | |
| 104 (a ??= new A()).a; //# 08: ok | |
| 105 Expect.throws(() => (a ??= new A()).b, noMethod); //# 09: static type warning | |
| 106 (a ??= new B()).a; //# 10: ok | |
| 107 (a ??= new B()).b; //# 11: static type warning | |
| 108 if (!checkedMode) { | |
| 109 (b ??= new A()).a; //# 12: ok | |
| 110 Expect.throws(() => (b ??= new A()).b, noMethod); //# 13: static type warnin
g | |
| 111 | |
| 112 // Exactly the same static warnings that would be caused by v = e are also | |
| 113 // generated in the case of v ??= e. | |
| 114 b ??= new C(); //# 14: static type warning | |
| 115 } | |
| 116 | |
| 117 // The static type of C.v ??= e is the LUB of the static types of C.v and e. | |
| 118 (ClassWithStaticGetters.a ??= new A()).a; //# 15: ok | |
| 119 Expect.throws(() => (ClassWithStaticGetters.a ??= new A()).b, noMethod); //# 1
6: static type warning | |
| 120 (ClassWithStaticGetters.a ??= new B()).a; //# 17: ok | |
| 121 (ClassWithStaticGetters.a ??= new B()).b; //# 18: static type warning | |
| 122 if (!checkedMode) { | |
| 123 (ClassWithStaticGetters.b ??= new A()).a; //# 19: ok | |
| 124 Expect.throws(() => (ClassWithStaticGetters.b ??= new A()).b, noMethod); //#
20: static type warning | |
| 125 | |
| 126 // Exactly the same static warnings that would be caused by C.v = e are | |
| 127 // also generated in the case of C.v ??= e. | |
| 128 ClassWithStaticGetters.b ??= new C(); //# 21: static type warning | |
| 129 } | |
| 130 | |
| 131 // The static type of e1.v ??= e2 is the LUB of the static types of e1.v and | |
| 132 // e2. | |
| 133 (new ClassWithInstanceGetters().a ??= new A()).a; //# 22: ok | |
| 134 Expect.throws(() => (new ClassWithInstanceGetters().a ??= new A()).b, noMethod
); //# 23: static type warning | |
| 135 (new ClassWithInstanceGetters().a ??= new B()).a; //# 24: ok | |
| 136 (new ClassWithInstanceGetters().a ??= new B()).b; //# 25: static type warning | |
| 137 if (!checkedMode) { | |
| 138 (new ClassWithInstanceGetters().b ??= new A()).a; //# 26: ok | |
| 139 Expect.throws(() => (new ClassWithInstanceGetters().b ??= new A()).b, noMeth
od); //# 27: static type warning | |
| 140 | |
| 141 // Exactly the same static warnings that would be caused by e1.v = e2 are | |
| 142 // also generated in the case of e1.v ??= e2. | |
| 143 new ClassWithInstanceGetters().b ??= new C(); //# 28: static type warning | |
| 144 } | |
| 145 | |
| 146 // The static type of e1[e2] ??= e3 is the LUB of the static types of e1[e2] | |
| 147 // and e3. | |
| 148 ((<A>[null])[0] ??= new A()).a; //# 29: ok | |
| 149 Expect.throws(() => ((<A>[null])[0] ??= new A()).b, noMethod); //# 30: static
type warning | |
| 150 ((<A>[null])[0] ??= new B()).a; //# 31: ok | |
| 151 ((<A>[null])[0] ??= new B()).b; //# 32: static type warning | |
| 152 if (!checkedMode) { | |
| 153 ((<B>[null])[0] ??= new A()).a; //# 33: ok | |
| 154 Expect.throws(() => ((<B>[null])[0] ??= new A()).b, noMethod); //# 34: stati
c type warning | |
| 155 | |
| 156 // Exactly the same static warnings that would be caused by e1[e2] = e3 are | |
| 157 // also generated in the case of e1[e2] ??= e3. | |
| 158 (<B>[null])[0] ??= new C(); //# 35: static type warning | |
| 159 } | |
| 160 | |
| 161 // The static type of e1?.v op= e2 is the static type of e1.v op e2, | |
| 162 // therefore the static type of e1?.v ??= e2 is the static type of | |
| 163 // e1.v ?? e2, which is the LUB of the static types of e1?.v and e2. | |
| 164 (new ClassWithInstanceGetters()?.a ??= new A()).a; //# 36: ok | |
| 165 Expect.throws(() => (new ClassWithInstanceGetters()?.a ??= new A()).b, noMetho
d); //# 37: static type warning | |
| 166 (new ClassWithInstanceGetters()?.a ??= new B()).a; //# 38: ok | |
| 167 (new ClassWithInstanceGetters()?.a ??= new B()).b; //# 39: static type warning | |
| 168 if (!checkedMode) { | |
| 169 (new ClassWithInstanceGetters()?.b ??= new A()).a; //# 40: ok | |
| 170 Expect.throws(() => (new ClassWithInstanceGetters()?.b ??= new A()).b, noMet
hod); //# 41: static type warning | |
| 171 | |
| 172 // Exactly the same static warnings that would be caused by e1.v ??= e2 are | |
| 173 // also generated in the case of e1?.v ??= e2. | |
| 174 new ClassWithInstanceGetters()?.b ??= new C(); //# 42: static type warning | |
| 175 } | |
| 176 } | |
| OLD | NEW |