OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, 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 // Dart test for type checks involving the void type. |
| 6 |
| 7 void use(dynamic x) { } |
| 8 |
| 9 void testVoidParam(void x) { |
| 10 x; /// param_stmt: ok |
| 11 true ? x : x; /// param_conditional: ok |
| 12 for (x; false; x) {} /// param_for: ok |
| 13 use(x); /// param_argument: static type warning |
| 14 use(x as Object); /// param_as: ok |
| 15 void y = x; /// param_void_init: static type warning |
| 16 dynamic z = x; /// param_dynamic_init: static type warning |
| 17 x is Object; /// param_is: static type warning |
| 18 throw x; /// param_throw: static type warning |
| 19 [x]; /// param_literal_list_init: static type warning |
| 20 var m1 = {4: x}; /// param_literal_map_value_init: static type warning |
| 21 var m2 = {x : 4}; /// param_literal_map_key_init: static type warning |
| 22 Map<dynamic, dynamic> m3 = {4: x}; /// param_literal_map_value_init2: static
type warning |
| 23 Map<dynamic, dynamic> m4 = {x : 4}; /// param_literal_map_key_init2: static t
ype warning |
| 24 x ?? 499; /// param_null_equals2: static type warning |
| 25 null ?? x; /// param_null_equals2: static type warning |
| 26 return x; /// param_return: static type warning |
| 27 while (x) {}; /// param_while: static type warning |
| 28 do {} while (x); /// param_do_while: static type warning |
| 29 for (var v in x) {} /// param_for_in: static type warning |
| 30 for (x in [1, 2]) {} /// param_for_in2: ok |
| 31 x += 1; /// param_plus_eq: static type warning |
| 32 x.toString(); /// param_toString: static type warning |
| 33 x?.toString(); /// param_null_dot: static type warning |
| 34 x..toString(); /// param_cascade: static type warning |
| 35 } |
| 36 |
| 37 void testVoidCall(void f()) { |
| 38 f(); /// call_stmt: ok |
| 39 true ? f() : f(); /// call_conditional: ok |
| 40 for (f(); false; f()) {} /// call_for: ok |
| 41 use(f()); /// call_argument: static type warning |
| 42 use(f() as Object); /// call_as: ok |
| 43 void y = f(); /// call_void_init: static type warning |
| 44 dynamic z = f(); /// call_dynamic_init: static type warning |
| 45 f() is Object; /// call_is: static type warning |
| 46 throw f(); /// call_throw: static type warning |
| 47 [f()]; /// call_literal_list_init: static type warning |
| 48 var m1 = {4: f() }; /// call_literal_map_value_init: static type warning |
| 49 var m2 = { f(): 4}; /// call_literal_map_key_init: static type warning |
| 50 Map<dynamic, dynamic> m3 = {4: f() }; /// call_literal_map_value_init2: stati
c type warning |
| 51 Map<dynamic, dynamic> m4 = { f(): 4}; /// call_literal_map_key_init2: static
type warning |
| 52 f() ?? 499; /// call_null_equals2: static type warning |
| 53 null ?? f(); /// call_null_equals2: static type warning |
| 54 return f(); /// call_return: static type warning |
| 55 while (f()) {}; /// call_while: static type warning |
| 56 do {} while (f()); /// call_do_while: static type warning |
| 57 for (var v in f()) {} /// call_for_in: static type warning |
| 58 f().toString(); /// call_toString: static type warning |
| 59 f()?.toString(); /// call_null_dot: static type warning |
| 60 f()..toString(); /// call_cascade: static type warning |
| 61 } |
| 62 |
| 63 void testVoidLocal() { |
| 64 void x; |
| 65 x = 42; /// local_assign: ok |
| 66 x; /// local_stmt: ok |
| 67 true ? x : x; /// local_conditional: ok |
| 68 for (x; false; x) {} /// local_for: ok |
| 69 use(x); /// local_argument: static type warning |
| 70 use(x as Object); /// local_as: ok |
| 71 void y = x; /// local_void_init: static type warning |
| 72 dynamic z = x; /// local_dynamic_init: static type warning |
| 73 x is Object; /// local_is: static type warning |
| 74 throw x; /// local_throw: static type warning |
| 75 [x]; /// local_literal_list_init: static type warning |
| 76 var m1 = {4: x}; /// local_literal_map_value_init: static type warning |
| 77 var m2 = {x : 4}; /// local_literal_map_key_init: static type warning |
| 78 Map<dynamic, dynamic> m3 = {4: x}; /// local_literal_map_value_init2: static
type warning |
| 79 Map<dynamic, dynamic> m4 = {x : 4}; /// local_literal_map_key_init2: static t
ype warning |
| 80 x ?? 499; /// local_null_equals2: static type warning |
| 81 null ?? x; /// local_null_equals2: static type warning |
| 82 return x; /// local_return: static type warning |
| 83 while (x) {}; /// local_while: static type warning |
| 84 do {} while (x); /// local_do_while: static type warning |
| 85 for (var v in x) {} /// local_for_in: static type warning |
| 86 for (x in [1, 2]) {} /// local_for_in2: ok |
| 87 x += 1; /// local_plus_eq: static type warning |
| 88 x.toString(); /// local_toString: static type warning |
| 89 x?.toString(); /// local_null_dot: static type warning |
| 90 x..toString(); /// local_cascade: static type warning |
| 91 } |
| 92 |
| 93 void testVoidFinalLocal() { |
| 94 final void x = null; |
| 95 x = 42; /// final_local_assign: static type warning |
| 96 x; /// final_local_stmt: ok |
| 97 true ? x : x; /// final_local_conditional: ok |
| 98 for (x; false; x) {} /// final_local_for: ok |
| 99 use(x); /// final_local_argument: static type warning |
| 100 use(x as Object); /// final_local_as: ok |
| 101 void y = x; /// final_local_void_init: static type warning |
| 102 dynamic z = x; /// final_local_dynamic_init: static type warning |
| 103 x is Object; /// final_local_is: static type warning |
| 104 throw x; /// final_local_throw: static type warning |
| 105 [x]; /// final_local_literal_list_init: static type warning |
| 106 var m1 = {4: x}; /// final_local_literal_map_value_init: static type warning |
| 107 var m2 = {x : 4}; /// final_local_literal_map_key_init: static type warning |
| 108 Map<dynamic, dynamic> m3 = {4: x}; /// final_local_literal_map_value_init2: s
tatic type warning |
| 109 Map<dynamic, dynamic> m4 = {x : 4}; /// final_local_literal_map_key_init2: st
atic type warning |
| 110 x ?? 499; /// final_local_null_equals2: static type warning |
| 111 null ?? x; /// final_local_null_equals2: static type warning |
| 112 return x; /// final_local_return: static type warning |
| 113 while (x) {}; /// final_local_while: static type warning |
| 114 do {} while (x); /// final_local_do_while: static type warning |
| 115 for (var v in x) {} /// final_local_for_in: static type warning |
| 116 for (x in [1, 2]) {} /// final_local_for_in2: ok |
| 117 x += 1; /// final_local_plus_eq: static type warning |
| 118 x.toString(); /// final_local_toString: static type warning |
| 119 x?.toString(); /// final_local_null_dot: static type warning |
| 120 x..toString(); /// final_local_cascade: static type warning |
| 121 } |
| 122 |
| 123 void global; |
| 124 void testVoidGlobal() { |
| 125 global; /// global_stmt: ok |
| 126 true ? global : global; /// global_conditional: ok |
| 127 for (global; false; global) {} /// global_for: ok |
| 128 use(global); /// global_argument: static type warning |
| 129 use(global as Object); /// global_as: ok |
| 130 void y = global; /// global_void_init: static type warning |
| 131 dynamic z = global; /// global_dynamic_init: static type warning |
| 132 global is Object; /// global_is: static type warning |
| 133 throw global; /// global_throw: static type warning |
| 134 [global]; /// global_literal_list_init: static type warning |
| 135 var m1 = {4: global }; /// global_literal_map_value_init: static type warnin
g |
| 136 var m2 = { global: 4}; /// global_literal_map_key_init: static type warning |
| 137 Map<dynamic, dynamic> m3 = {4: global }; /// global_literal_map_value_init2:
static type warning |
| 138 Map<dynamic, dynamic> m4 = { global: 4}; /// global_literal_map_key_init2: st
atic type warning |
| 139 null ?? global; /// global_null_equals2: static type warning |
| 140 global ?? 499; /// global_null_equals2: static type warning |
| 141 return global; /// global_return: static type warning |
| 142 while (global) {}; /// global_while: static type warning |
| 143 do {} while (global); /// global_do_while: static type warning |
| 144 for (var v in global) {} /// global_for_in: static type warning |
| 145 for (global in [1, 2]) {} /// global_for_in2: ok |
| 146 global += 1; /// global_plus_eq: static type warning |
| 147 global.toString(); /// global_toString: static type warning |
| 148 global?.toString(); /// global_null_dot: static type warning |
| 149 global..toString(); /// global_cascade: static type warning |
| 150 } |
| 151 |
| 152 void testVoidConditional() { |
| 153 void x; |
| 154 (true ? x : x); /// conditional_parens: static type warning |
| 155 true ? x : x; /// conditional_stmt: ok |
| 156 true ? true ? x : x : true ? x : x; /// conditional_conditional: ok |
| 157 for (true ? x : x; false; true ? x : x) {} /// conditional_for: ok |
| 158 use(true ? x : x); /// conditional_argument: static type warning |
| 159 void y = true ? x : x; /// conditional_void_init: static type warning |
| 160 dynamic z = true ? x : x; /// conditional_dynamic_init: static type warning |
| 161 throw true ? x : x; /// conditional_throw: static type warning |
| 162 [true ? x : x]; /// conditional_literal_list_init: static type warning |
| 163 var m1 = {4: true ? x : x}; /// conditional_literal_map_value_init: static t
ype warning |
| 164 Map<dynamic, dynamic> m3 = {4: true ? x : x}; /// conditional_literal_map_val
ue_init2: static type warning |
| 165 null ?? true ? x : x; /// conditional_null_equals2: static type warning |
| 166 return true ? x : x; /// conditional_return: static type warning |
| 167 while (true ? x : x) {}; /// conditional_while: static type warning |
| 168 do {} while (true ? x : x); /// conditional_do_while: static type warning |
| 169 for (var v in true ? x : x) {} /// conditional_for_in: static type warning |
| 170 |
| 171 (true ? 499 : x); /// conditional2_parens: static type warning |
| 172 true ? 499 : x; /// conditional2_stmt: ok |
| 173 true ? true ? 499 : x : true ? 499 : x; /// conditional2_conditional: ok |
| 174 for (true ? 499 : x; false; true ? 499 : x) {} /// conditional2_for: ok |
| 175 use(true ? 499 : x); /// conditional2_argument: static type warning |
| 176 void y2 = true ? 499 : x; /// conditional2_void_init: static type warning |
| 177 dynamic z2 = true ? 499 : x; /// conditional2_dynamic_init: static type warni
ng |
| 178 throw true ? 499 : x; /// conditional2_throw: static type warning |
| 179 [true ? 499 : x]; /// conditional2_literal_list_init: static type warning |
| 180 var m12 = {4: true ? 499 : x}; /// conditional2_literal_map_value_init: stat
ic type warning |
| 181 Map<dynamic, dynamic> m32 = {4: true ? 499 : x}; /// conditional2_literal_map
_value_init2: static type warning |
| 182 null ?? true ? 499 : x; /// conditional2_null_equals2: static type warning |
| 183 return true ? 499 : x; /// conditional2_return: static type warning |
| 184 while (true ? 499 : x) {}; /// conditional2while: static type warning |
| 185 do {} while (true ? 499 : x); /// conditional2do_while: static type warning |
| 186 for (var v in true ? 499 : x) {} /// conditional2for_in: static type warning |
| 187 |
| 188 (true ? x : 499); /// conditional3_parens: static type warning |
| 189 true ? x : 499; /// conditional3_stmt: ok |
| 190 true ? true ? x : 499 : true ? x : 499; /// conditional3_conditional: ok |
| 191 for (true ? x : 499; false; true ? x : 499) {} /// conditional3_for: ok |
| 192 use(true ? x : 499); /// conditional3_argument: static type warning |
| 193 void y3 = true ? x : 499; /// conditional3_void_init: static type warning |
| 194 dynamic z3 = true ? x : 499; /// conditional3_dynamic_init: static type warni
ng |
| 195 throw true ? x : 499; /// conditional3_throw: static type warning |
| 196 [true ? x : 499]; /// conditional3_literal_list_init: static type warning |
| 197 var m13 = {4: true ? x : 499 }; /// conditional3_literal_map_value_init: sta
tic type warning |
| 198 Map<dynamic, dynamic> m33 = {4: true ? x : 499 }; /// conditional3_literal_ma
p_value_init2: static type warning |
| 199 null ?? true ? x : 499; /// conditional3_null_equals2: static type warning |
| 200 return true ? x : 499; /// conditional3_return: static type warning |
| 201 while (true ? x : 499) {}; /// conditional_while: static type warning |
| 202 do {} while (true ? x : 499); /// conditional_do_while: static type warning |
| 203 for (var v in true ? x : 499) {} /// conditional_for_in: static type warning |
| 204 } |
| 205 |
| 206 |
| 207 class A<T> { |
| 208 T x; |
| 209 |
| 210 void foo() {} |
| 211 } |
| 212 |
| 213 class B implements A<void> { |
| 214 void x; |
| 215 |
| 216 int foo() => 499; |
| 217 |
| 218 void forInTest() { |
| 219 for (x in <void>[]) {} /// instance2_for_in2: static type warning |
| 220 for (x in [1, 2]) {} /// instance2_for_in3: ok |
| 221 } |
| 222 } |
| 223 |
| 224 class C implements A<void> { |
| 225 void get x => null; |
| 226 set x(void y) {} |
| 227 |
| 228 void forInTest() { |
| 229 for (x in <void>[]) {} /// instance3_for_in2: static type warning |
| 230 for (x in [1, 2]) {} /// instance3_for_in3: ok |
| 231 } |
| 232 } |
| 233 |
| 234 |
| 235 void testInstanceField() { |
| 236 A<void> a = new A<void>(); |
| 237 a.x = 499; /// field_assign: ok |
| 238 a.x; /// instance_stmt: ok |
| 239 true ? a.x : a.x; /// instance_conditional: ok |
| 240 for (a.x; false; a.x) {} /// instance_for: ok |
| 241 use(a.x); /// instance_argument: static type warning |
| 242 use(a.x as Object); /// instance_as: ok |
| 243 void y = a.x; /// instance_void_init: static type warning |
| 244 dynamic z = a.x; /// instance_dynamic_init: static type warning |
| 245 a.x is Object; /// instance_is: static type warning |
| 246 throw a.x; /// instance_throw: static type warning |
| 247 [a.x]; /// instance_literal_list_init: static type warning |
| 248 var m1 = {4: a.x}; /// instance_literal_map_value_init: static type warning |
| 249 var m2 = { a.x : 4}; /// instance_literal_map_key_init: static type warning |
| 250 Map<dynamic, dynamic> m3 = {4: a.x}; /// instance_literal_map_value_init2: st
atic type warning |
| 251 Map<dynamic, dynamic> m4 = { a.x : 4}; /// instance_literal_map_key_init2: st
atic type warning |
| 252 null ?? a.x; /// instance_null_equals2: static type warning |
| 253 a.x ?? 499; /// instance_null_equals2: static type warning |
| 254 return a.x; /// instance_return: static type warning |
| 255 while (a.x) {}; /// instance_while: static type warning |
| 256 do {} while (a.x); /// instance_do_while: static type warning |
| 257 for (var v in a.x) {} /// instance_for_in: static type warning |
| 258 a.x += 1; /// instance_plus_eq: static type warning |
| 259 a.x.toString(); /// instance_toString: static type warning |
| 260 a.x?.toString(); /// instance_null_dot: static type warning |
| 261 a.x..toString(); /// instance_cascade: static type warning |
| 262 |
| 263 B b = new B(); |
| 264 b.x = 42; /// field_assign2: ok |
| 265 b.x; /// instance2_stmt: ok |
| 266 true ? b.x : b.x; /// instance2_conditional: ok |
| 267 for (b.x; false; b.x) {} /// instance2_for: ok |
| 268 use(b.x); /// instance2_argument: static type warning |
| 269 use(b.x as Object); /// instance2_as: ok |
| 270 void y2 = b.x; /// instance2_void_init: static type warning |
| 271 dynamic z2 = b.x; /// instance2_dynamic_init: static type warning |
| 272 b.x is Object; /// instance2_is: static type warning |
| 273 throw b.x; /// instance2_throw: static type warning |
| 274 [b.x]; /// instance2_literal_list_init: static type warning |
| 275 var m12 = {4: b.x}; /// instance2_literal_map_value_init: static type warnin
g |
| 276 var m22 = { b.x : 4}; /// instance2_literal_map_key_init: static type warnin
g |
| 277 Map<dynamic, dynamic> m32 = {4: b.x}; /// instance2_literal_map_value_init2:
static type warning |
| 278 Map<dynamic, dynamic> m42 = { b.x : 4}; /// instance2_literal_map_key_init2:
static type warning |
| 279 null ?? b.x; /// instance2_null_equals2: static type warning |
| 280 b.x ?? 499; /// instance2_null_equals2: static type warning |
| 281 return b.x; /// instance2_return: static type warning |
| 282 while (b.x) {}; /// instance2_while: static type warning |
| 283 do {} while (b.x); /// instance2_do_while: static type warning |
| 284 for (var v in b.x) {} /// instance2_for_in: static type warning |
| 285 b.forInTest(); |
| 286 b.x += 1; /// instance2_plus_eq: static type warning |
| 287 b.x.toString(); /// instance2_toString: static type warning |
| 288 b.x?.toString(); /// instance2_null_dot: static type warning |
| 289 b.x..toString(); /// instance2_cascade: static type warning |
| 290 |
| 291 C c = new C(); |
| 292 c.x = 32; /// setter_assign: ok |
| 293 c.x; /// instance3_stmt: ok |
| 294 true ? c.x : c.x; /// instance3_conditional: ok |
| 295 for (c.x; false; c.x) {} /// instance3_for: ok |
| 296 use(c.x); /// instance3_argument: static type warning |
| 297 use(c.x as Object); /// instance3_as: ok |
| 298 void y3 = c.x; /// instance3_void_init: static type warning |
| 299 dynamic z3 = c.x; /// instance3_dynamic_init: static type warning |
| 300 c.x is Object; /// instance3_is: static type warning |
| 301 throw c.x; /// instance3_throw: static type warning |
| 302 [c.x]; /// instance3_literal_list_init: static type warning |
| 303 var m13 = {4: c.x}; /// instance3_literal_map_value_init: static type warnin
g |
| 304 var m23 = { c.x : 4}; /// instance3_literal_map_key_init: static type warnin
g |
| 305 Map<dynamic, dynamic> m33 = {4: c.x}; /// instance3_literal_map_value_init2:
static type warning |
| 306 Map<dynamic, dynamic> m43 = { c.x : 4}; /// instance3_literal_map_key_init2:
static type warning |
| 307 null ?? c.x; /// instance3_null_equals2: static type warning |
| 308 c.x ?? 499; /// instance3_null_equals2: static type warning |
| 309 return c.x; /// instance3_return: static type warning |
| 310 while (c.x) {}; /// instance3_while: static type warning |
| 311 do {} while (c.x); /// instance3_do_while: static type warning |
| 312 for (var v in c.x) {} /// instance3_for_in: static type warning |
| 313 c.forInTest(); |
| 314 c.x += 1; /// instance3_plus_eq: static type warning |
| 315 c.x.toString(); /// instance3_toString: static type warning |
| 316 c.x?.toString(); /// instance3_null_dot: static type warning |
| 317 c.x..toString(); /// instance3_cascade: static type warning |
| 318 } |
| 319 |
| 320 void testParenthesized() { |
| 321 void x; |
| 322 (x); /// paren_stmt: ok |
| 323 true ? (x) : (x); /// paren_conditional: ok |
| 324 for ((x); false; (x)) {} /// paren_for: ok |
| 325 use((x)); /// paren_argument: static type warning |
| 326 use((x) as Object); /// paren_as: ok |
| 327 void y = (x); /// paren_void_init: static type warning |
| 328 dynamic z = (x); /// paren_dynamic_init: static type warning |
| 329 (x) is Object; /// paren_is: static type warning |
| 330 throw (x); /// paren_throw: static type warning |
| 331 [(x)]; /// paren_literal_list_init: static type warning |
| 332 var m1 = {4: (x) }; /// paren_literal_map_value_init: static type warning |
| 333 var m2 = { (x): 4}; /// paren_literal_map_key_init: static type warning |
| 334 Map<dynamic, dynamic> m3 = {4: (x) }; /// paren_literal_map_value_init2: stat
ic type warning |
| 335 Map<dynamic, dynamic> m4 = { (x): 4}; /// paren_literal_map_key_init2: static
type warning |
| 336 (x) ?? 499; /// paren_null_equals2: static type warning |
| 337 null ?? (x); /// paren_null_equals2: static type warning |
| 338 return (x); /// paren_return: static type warning |
| 339 while ((x)) {}; /// paren_while: static type warning |
| 340 do {} while ((x)); /// paren_do_while: static type warning |
| 341 for (var v in (x)) {} /// paren_for_in: static type warning |
| 342 (x).toString(); /// paren_toString: static type warning |
| 343 (x)?.toString(); /// paren_null_dot: static type warning |
| 344 (x)..toString(); /// paren_cascade: static type warning |
| 345 } |
| 346 |
| 347 main() { |
| 348 try { |
| 349 testVoidParam(499); |
| 350 testVoidCall(() {}); |
| 351 testVoidLocal(); |
| 352 testVoidFinalLocal(); |
| 353 testVoidConditional(); |
| 354 testInstanceField(); |
| 355 testParenthesized(); |
| 356 } catch (e) { |
| 357 // Silently eat all dynamic errors. |
| 358 // This test is only testing static warnings. |
| 359 } |
| 360 } |
OLD | NEW |