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