Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(203)

Side by Side Diff: pkg/analyzer/test/generated/strong_mode_test.dart

Issue 2624793002: Make error producing tests ansynchronous. (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 library analyzer.test.generated.strong_mode_test; 5 library analyzer.test.generated.strong_mode_test;
6 6
7 import 'dart:async';
7 import 'package:analyzer/dart/ast/ast.dart'; 8 import 'package:analyzer/dart/ast/ast.dart';
8 import 'package:analyzer/dart/ast/standard_resolution_map.dart'; 9 import 'package:analyzer/dart/ast/standard_resolution_map.dart';
9 import 'package:analyzer/dart/element/element.dart'; 10 import 'package:analyzer/dart/element/element.dart';
10 import 'package:analyzer/dart/element/type.dart'; 11 import 'package:analyzer/dart/element/type.dart';
11 import 'package:analyzer/src/dart/element/element.dart'; 12 import 'package:analyzer/src/dart/element/element.dart';
12 import 'package:analyzer/src/error/codes.dart'; 13 import 'package:analyzer/src/error/codes.dart';
13 import 'package:analyzer/src/generated/engine.dart'; 14 import 'package:analyzer/src/generated/engine.dart';
14 import 'package:analyzer/src/generated/source_io.dart'; 15 import 'package:analyzer/src/generated/source_io.dart';
15 import 'package:test/test.dart'; 16 import 'package:test/test.dart';
16 import 'package:test_reflective_loader/test_reflective_loader.dart'; 17 import 'package:test_reflective_loader/test_reflective_loader.dart';
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 _isListOf = _assertions.isListOf; 72 _isListOf = _assertions.isListOf;
72 _isMapOf = _assertions.isMapOf; 73 _isMapOf = _assertions.isMapOf;
73 _isFunction2Of = _assertions.isFunction2Of; 74 _isFunction2Of = _assertions.isFunction2Of;
74 _sameElement = _assertions.sameElement; 75 _sameElement = _assertions.sameElement;
75 _isFutureOf = _isInstantiationOf(_sameElement(typeProvider.futureType)); 76 _isFutureOf = _isInstantiationOf(_sameElement(typeProvider.futureType));
76 _isFutureOfDynamic = _isFutureOf([_isDynamic]); 77 _isFutureOfDynamic = _isFutureOf([_isDynamic]);
77 _isFutureOfInt = _isFutureOf([_isInt]); 78 _isFutureOfInt = _isFutureOf([_isInt]);
78 _isStreamOf = _isInstantiationOf(_sameElement(typeProvider.streamType)); 79 _isStreamOf = _isInstantiationOf(_sameElement(typeProvider.streamType));
79 } 80 }
80 81
81 void test_async_method_propagation() { 82 test_async_method_propagation() async {
82 String code = r''' 83 String code = r'''
83 import "dart:async"; 84 import "dart:async";
84 class A { 85 class A {
85 Future f0() => new Future.value(3); 86 Future f0() => new Future.value(3);
86 Future f1() async => new Future.value(3); 87 Future f1() async => new Future.value(3);
87 Future f2() async => await new Future.value(3); 88 Future f2() async => await new Future.value(3);
88 89
89 Future<int> f3() => new Future.value(3); 90 Future<int> f3() => new Future.value(3);
90 Future<int> f4() async => new Future.value(3); 91 Future<int> f4() async => new Future.value(3);
91 Future<int> f5() async => await new Future.value(3); 92 Future<int> f5() async => await new Future.value(3);
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
128 129
129 check("g0", _isFutureOfDynamic); 130 check("g0", _isFutureOfDynamic);
130 check("g1", _isFutureOfDynamic); 131 check("g1", _isFutureOfDynamic);
131 check("g2", _isFutureOfDynamic); 132 check("g2", _isFutureOfDynamic);
132 133
133 check("g3", _isFutureOfInt); 134 check("g3", _isFutureOfInt);
134 check("g4", _isFutureOfInt); 135 check("g4", _isFutureOfInt);
135 check("g5", _isFutureOfInt); 136 check("g5", _isFutureOfInt);
136 } 137 }
137 138
138 void test_async_propagation() { 139 test_async_propagation() async {
139 String code = r''' 140 String code = r'''
140 import "dart:async"; 141 import "dart:async";
141 142
142 Future f0() => new Future.value(3); 143 Future f0() => new Future.value(3);
143 Future f1() async => new Future.value(3); 144 Future f1() async => new Future.value(3);
144 Future f2() async => await new Future.value(3); 145 Future f2() async => await new Future.value(3);
145 146
146 Future<int> f3() => new Future.value(3); 147 Future<int> f3() => new Future.value(3);
147 Future<int> f4() async => new Future.value(3); 148 Future<int> f4() async => new Future.value(3);
148 Future<int> f5() async => await new Future.value(3); 149 Future<int> f5() async => await new Future.value(3);
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 185
185 check("g0", _isFutureOfDynamic); 186 check("g0", _isFutureOfDynamic);
186 check("g1", _isFutureOfDynamic); 187 check("g1", _isFutureOfDynamic);
187 check("g2", _isFutureOfDynamic); 188 check("g2", _isFutureOfDynamic);
188 189
189 check("g3", _isFutureOfInt); 190 check("g3", _isFutureOfInt);
190 check("g4", _isFutureOfInt); 191 check("g4", _isFutureOfInt);
191 check("g5", _isFutureOfInt); 192 check("g5", _isFutureOfInt);
192 } 193 }
193 194
194 void test_async_star_method_propagation() { 195 test_async_star_method_propagation() async {
195 String code = r''' 196 String code = r'''
196 import "dart:async"; 197 import "dart:async";
197 class A { 198 class A {
198 Stream g0() async* { yield []; } 199 Stream g0() async* { yield []; }
199 Stream g1() async* { yield* new Stream(); } 200 Stream g1() async* { yield* new Stream(); }
200 201
201 Stream<List<int>> g2() async* { yield []; } 202 Stream<List<int>> g2() async* { yield []; }
202 Stream<List<int>> g3() async* { yield* new Stream(); } 203 Stream<List<int>> g3() async* { yield* new Stream(); }
203 } 204 }
204 '''; 205 ''';
205 CompilationUnit unit = resolveSource(code); 206 CompilationUnit unit = resolveSource(code);
206 207
207 void check(String name, Asserter<InterfaceType> typeTest) { 208 void check(String name, Asserter<InterfaceType> typeTest) {
208 MethodDeclaration test = AstFinder.getMethodInClass(unit, "A", name); 209 MethodDeclaration test = AstFinder.getMethodInClass(unit, "A", name);
209 BlockFunctionBody body = test.body; 210 BlockFunctionBody body = test.body;
210 YieldStatement stmt = body.block.statements[0]; 211 YieldStatement stmt = body.block.statements[0];
211 Expression exp = stmt.expression; 212 Expression exp = stmt.expression;
212 typeTest(exp.staticType); 213 typeTest(exp.staticType);
213 } 214 }
214 215
215 check("g0", _isListOf(_isDynamic)); 216 check("g0", _isListOf(_isDynamic));
216 check("g1", _isStreamOf([_isDynamic])); 217 check("g1", _isStreamOf([_isDynamic]));
217 218
218 check("g2", _isListOf(_isInt)); 219 check("g2", _isListOf(_isInt));
219 check("g3", _isStreamOf([(DartType type) => _isListOf(_isInt)(type)])); 220 check("g3", _isStreamOf([(DartType type) => _isListOf(_isInt)(type)]));
220 } 221 }
221 222
222 void test_async_star_propagation() { 223 test_async_star_propagation() async {
223 String code = r''' 224 String code = r'''
224 import "dart:async"; 225 import "dart:async";
225 226
226 Stream g0() async* { yield []; } 227 Stream g0() async* { yield []; }
227 Stream g1() async* { yield* new Stream(); } 228 Stream g1() async* { yield* new Stream(); }
228 229
229 Stream<List<int>> g2() async* { yield []; } 230 Stream<List<int>> g2() async* { yield []; }
230 Stream<List<int>> g3() async* { yield* new Stream(); } 231 Stream<List<int>> g3() async* { yield* new Stream(); }
231 '''; 232 ''';
232 CompilationUnit unit = resolveSource(code); 233 CompilationUnit unit = resolveSource(code);
233 234
234 void check(String name, Asserter<InterfaceType> typeTest) { 235 void check(String name, Asserter<InterfaceType> typeTest) {
235 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, name); 236 FunctionDeclaration test = AstFinder.getTopLevelFunction(unit, name);
236 BlockFunctionBody body = test.functionExpression.body; 237 BlockFunctionBody body = test.functionExpression.body;
237 YieldStatement stmt = body.block.statements[0]; 238 YieldStatement stmt = body.block.statements[0];
238 Expression exp = stmt.expression; 239 Expression exp = stmt.expression;
239 typeTest(exp.staticType); 240 typeTest(exp.staticType);
240 } 241 }
241 242
242 check("g0", _isListOf(_isDynamic)); 243 check("g0", _isListOf(_isDynamic));
243 check("g1", _isStreamOf([_isDynamic])); 244 check("g1", _isStreamOf([_isDynamic]));
244 245
245 check("g2", _isListOf(_isInt)); 246 check("g2", _isListOf(_isInt));
246 check("g3", _isStreamOf([(DartType type) => _isListOf(_isInt)(type)])); 247 check("g3", _isStreamOf([(DartType type) => _isListOf(_isInt)(type)]));
247 } 248 }
248 249
249 void test_cascadeExpression() { 250 test_cascadeExpression() async {
250 String code = r''' 251 String code = r'''
251 class A<T> { 252 class A<T> {
252 List<T> map(T a, List<T> mapper(T x)) => mapper(a); 253 List<T> map(T a, List<T> mapper(T x)) => mapper(a);
253 } 254 }
254 255
255 void main () { 256 void main () {
256 A<int> a = new A()..map(0, (x) => [x]); 257 A<int> a = new A()..map(0, (x) => [x]);
257 } 258 }
258 '''; 259 ''';
259 CompilationUnit unit = resolveSource(code); 260 CompilationUnit unit = resolveSource(code);
(...skipping 10 matching lines...) Expand all
270 271
271 CascadeExpression cascade = fetch(0); 272 CascadeExpression cascade = fetch(0);
272 _isInstantiationOf(_hasElement(elementA))([_isInt])(cascade.staticType); 273 _isInstantiationOf(_hasElement(elementA))([_isInt])(cascade.staticType);
273 MethodInvocation invoke = cascade.cascadeSections[0]; 274 MethodInvocation invoke = cascade.cascadeSections[0];
274 FunctionExpression function = invoke.argumentList.arguments[1]; 275 FunctionExpression function = invoke.argumentList.arguments[1];
275 ExecutableElement f0 = function.element; 276 ExecutableElement f0 = function.element;
276 _isListOf(_isInt)(f0.type.returnType); 277 _isListOf(_isInt)(f0.type.returnType);
277 expect(f0.type.normalParameterTypes[0], typeProvider.intType); 278 expect(f0.type.normalParameterTypes[0], typeProvider.intType);
278 } 279 }
279 280
280 void test_constructorInitializer_propagation() { 281 test_constructorInitializer_propagation() async {
281 String code = r''' 282 String code = r'''
282 class A { 283 class A {
283 List<String> x; 284 List<String> x;
284 A() : this.x = []; 285 A() : this.x = [];
285 } 286 }
286 '''; 287 ''';
287 CompilationUnit unit = resolveSource(code); 288 CompilationUnit unit = resolveSource(code);
288 ConstructorDeclaration constructor = 289 ConstructorDeclaration constructor =
289 AstFinder.getConstructorInClass(unit, "A", null); 290 AstFinder.getConstructorInClass(unit, "A", null);
290 ConstructorFieldInitializer assignment = constructor.initializers[0]; 291 ConstructorFieldInitializer assignment = constructor.initializers[0];
291 Expression exp = assignment.expression; 292 Expression exp = assignment.expression;
292 _isListOf(_isString)(exp.staticType); 293 _isListOf(_isString)(exp.staticType);
293 } 294 }
294 295
295 void test_factoryConstructor_propagation() { 296 test_factoryConstructor_propagation() async {
296 String code = r''' 297 String code = r'''
297 class A<T> { 298 class A<T> {
298 factory A() { return new B(); } 299 factory A() { return new B(); }
299 } 300 }
300 class B<S> extends A<S> {} 301 class B<S> extends A<S> {}
301 '''; 302 ''';
302 CompilationUnit unit = resolveSource(code); 303 CompilationUnit unit = resolveSource(code);
303 304
304 ConstructorDeclaration constructor = 305 ConstructorDeclaration constructor =
305 AstFinder.getConstructorInClass(unit, "A", null); 306 AstFinder.getConstructorInClass(unit, "A", null);
306 BlockFunctionBody body = constructor.body; 307 BlockFunctionBody body = constructor.body;
307 ReturnStatement stmt = body.block.statements[0]; 308 ReturnStatement stmt = body.block.statements[0];
308 InstanceCreationExpression exp = stmt.expression; 309 InstanceCreationExpression exp = stmt.expression;
309 ClassElement elementB = AstFinder.getClass(unit, "B").element; 310 ClassElement elementB = AstFinder.getClass(unit, "B").element;
310 ClassElement elementA = AstFinder.getClass(unit, "A").element; 311 ClassElement elementA = AstFinder.getClass(unit, "A").element;
311 expect(resolutionMap.typeForTypeName(exp.constructorName.type).element, 312 expect(resolutionMap.typeForTypeName(exp.constructorName.type).element,
312 elementB); 313 elementB);
313 _isInstantiationOf(_hasElement(elementB))( 314 _isInstantiationOf(_hasElement(elementB))(
314 [_isType(elementA.typeParameters[0].type)])(exp.staticType); 315 [_isType(elementA.typeParameters[0].type)])(exp.staticType);
315 } 316 }
316 317
317 void test_fieldDeclaration_propagation() { 318 test_fieldDeclaration_propagation() async {
318 String code = r''' 319 String code = r'''
319 class A { 320 class A {
320 List<String> f0 = ["hello"]; 321 List<String> f0 = ["hello"];
321 } 322 }
322 '''; 323 ''';
323 CompilationUnit unit = resolveSource(code); 324 CompilationUnit unit = resolveSource(code);
324 325
325 VariableDeclaration field = AstFinder.getFieldInClass(unit, "A", "f0"); 326 VariableDeclaration field = AstFinder.getFieldInClass(unit, "A", "f0");
326 327
327 _isListOf(_isString)(field.initializer.staticType); 328 _isListOf(_isString)(field.initializer.staticType);
328 } 329 }
329 330
330 void test_functionDeclaration_body_propagation() { 331 test_functionDeclaration_body_propagation() async {
331 String code = r''' 332 String code = r'''
332 typedef T Function2<S, T>(S x); 333 typedef T Function2<S, T>(S x);
333 334
334 List<int> test1() => []; 335 List<int> test1() => [];
335 336
336 Function2<int, int> test2 (int x) { 337 Function2<int, int> test2 (int x) {
337 Function2<String, int> inner() { 338 Function2<String, int> inner() {
338 return (x) => x.length; 339 return (x) => x.length;
339 } 340 }
340 return (x) => x; 341 return (x) => x;
(...skipping 19 matching lines...) Expand all
360 expect(type0.returnType, typeProvider.intType); 361 expect(type0.returnType, typeProvider.intType);
361 expect(type0.normalParameterTypes[0], typeProvider.stringType); 362 expect(type0.normalParameterTypes[0], typeProvider.stringType);
362 363
363 FunctionExpression anon1 = (statements[1] as ReturnStatement).expression; 364 FunctionExpression anon1 = (statements[1] as ReturnStatement).expression;
364 FunctionType type1 = 365 FunctionType type1 =
365 resolutionMap.elementDeclaredByFunctionExpression(anon1).type; 366 resolutionMap.elementDeclaredByFunctionExpression(anon1).type;
366 expect(type1.returnType, typeProvider.intType); 367 expect(type1.returnType, typeProvider.intType);
367 expect(type1.normalParameterTypes[0], typeProvider.intType); 368 expect(type1.normalParameterTypes[0], typeProvider.intType);
368 } 369 }
369 370
370 void test_functionLiteral_assignment_typedArguments() { 371 test_functionLiteral_assignment_typedArguments() async {
371 String code = r''' 372 String code = r'''
372 typedef T Function2<S, T>(S x); 373 typedef T Function2<S, T>(S x);
373 374
374 void main () { 375 void main () {
375 Function2<int, String> l0 = (int x) => null; 376 Function2<int, String> l0 = (int x) => null;
376 Function2<int, String> l1 = (int x) => "hello"; 377 Function2<int, String> l1 = (int x) => "hello";
377 Function2<int, String> l2 = (String x) => "hello"; 378 Function2<int, String> l2 = (String x) => "hello";
378 Function2<int, String> l3 = (int x) => 3; 379 Function2<int, String> l3 = (int x) => 3;
379 Function2<int, String> l4 = (int x) {return 3;}; 380 Function2<int, String> l4 = (int x) {return 3;};
380 } 381 }
381 '''; 382 ''';
382 CompilationUnit unit = resolveSource(code); 383 CompilationUnit unit = resolveSource(code);
383 List<Statement> statements = 384 List<Statement> statements =
384 AstFinder.getStatementsInTopLevelFunction(unit, "main"); 385 AstFinder.getStatementsInTopLevelFunction(unit, "main");
385 DartType literal(int i) { 386 DartType literal(int i) {
386 VariableDeclarationStatement stmt = statements[i]; 387 VariableDeclarationStatement stmt = statements[i];
387 VariableDeclaration decl = stmt.variables.variables[0]; 388 VariableDeclaration decl = stmt.variables.variables[0];
388 FunctionExpression exp = decl.initializer; 389 FunctionExpression exp = decl.initializer;
389 return resolutionMap.elementDeclaredByFunctionExpression(exp).type; 390 return resolutionMap.elementDeclaredByFunctionExpression(exp).type;
390 } 391 }
391 392
392 _isFunction2Of(_isInt, _isString)(literal(0)); 393 _isFunction2Of(_isInt, _isString)(literal(0));
393 _isFunction2Of(_isInt, _isString)(literal(1)); 394 _isFunction2Of(_isInt, _isString)(literal(1));
394 _isFunction2Of(_isString, _isString)(literal(2)); 395 _isFunction2Of(_isString, _isString)(literal(2));
395 _isFunction2Of(_isInt, _isInt)(literal(3)); 396 _isFunction2Of(_isInt, _isInt)(literal(3));
396 _isFunction2Of(_isInt, _isString)(literal(4)); 397 _isFunction2Of(_isInt, _isString)(literal(4));
397 } 398 }
398 399
399 void test_functionLiteral_assignment_unTypedArguments() { 400 test_functionLiteral_assignment_unTypedArguments() async {
400 String code = r''' 401 String code = r'''
401 typedef T Function2<S, T>(S x); 402 typedef T Function2<S, T>(S x);
402 403
403 void main () { 404 void main () {
404 Function2<int, String> l0 = (x) => null; 405 Function2<int, String> l0 = (x) => null;
405 Function2<int, String> l1 = (x) => "hello"; 406 Function2<int, String> l1 = (x) => "hello";
406 Function2<int, String> l2 = (x) => "hello"; 407 Function2<int, String> l2 = (x) => "hello";
407 Function2<int, String> l3 = (x) => 3; 408 Function2<int, String> l3 = (x) => 3;
408 Function2<int, String> l4 = (x) {return 3;}; 409 Function2<int, String> l4 = (x) {return 3;};
409 } 410 }
410 '''; 411 ''';
411 CompilationUnit unit = resolveSource(code); 412 CompilationUnit unit = resolveSource(code);
412 List<Statement> statements = 413 List<Statement> statements =
413 AstFinder.getStatementsInTopLevelFunction(unit, "main"); 414 AstFinder.getStatementsInTopLevelFunction(unit, "main");
414 DartType literal(int i) { 415 DartType literal(int i) {
415 VariableDeclarationStatement stmt = statements[i]; 416 VariableDeclarationStatement stmt = statements[i];
416 VariableDeclaration decl = stmt.variables.variables[0]; 417 VariableDeclaration decl = stmt.variables.variables[0];
417 FunctionExpression exp = decl.initializer; 418 FunctionExpression exp = decl.initializer;
418 return resolutionMap.elementDeclaredByFunctionExpression(exp).type; 419 return resolutionMap.elementDeclaredByFunctionExpression(exp).type;
419 } 420 }
420 421
421 _isFunction2Of(_isInt, _isString)(literal(0)); 422 _isFunction2Of(_isInt, _isString)(literal(0));
422 _isFunction2Of(_isInt, _isString)(literal(1)); 423 _isFunction2Of(_isInt, _isString)(literal(1));
423 _isFunction2Of(_isInt, _isString)(literal(2)); 424 _isFunction2Of(_isInt, _isString)(literal(2));
424 _isFunction2Of(_isInt, _isInt)(literal(3)); 425 _isFunction2Of(_isInt, _isInt)(literal(3));
425 _isFunction2Of(_isInt, _isString)(literal(4)); 426 _isFunction2Of(_isInt, _isString)(literal(4));
426 } 427 }
427 428
428 void test_functionLiteral_body_propagation() { 429 test_functionLiteral_body_propagation() async {
429 String code = r''' 430 String code = r'''
430 typedef T Function2<S, T>(S x); 431 typedef T Function2<S, T>(S x);
431 432
432 void main () { 433 void main () {
433 Function2<int, List<String>> l0 = (int x) => ["hello"]; 434 Function2<int, List<String>> l0 = (int x) => ["hello"];
434 Function2<int, List<String>> l1 = (String x) => ["hello"]; 435 Function2<int, List<String>> l1 = (String x) => ["hello"];
435 Function2<int, List<String>> l2 = (int x) => [3]; 436 Function2<int, List<String>> l2 = (int x) => [3];
436 Function2<int, List<String>> l3 = (int x) {return [3];}; 437 Function2<int, List<String>> l3 = (int x) {return [3];};
437 } 438 }
438 '''; 439 ''';
(...skipping 13 matching lines...) Expand all
452 } 453 }
453 } 454 }
454 455
455 Asserter<InterfaceType> assertListOfString = _isListOf(_isString); 456 Asserter<InterfaceType> assertListOfString = _isListOf(_isString);
456 assertListOfString(functionReturnValue(0).staticType); 457 assertListOfString(functionReturnValue(0).staticType);
457 assertListOfString(functionReturnValue(1).staticType); 458 assertListOfString(functionReturnValue(1).staticType);
458 assertListOfString(functionReturnValue(2).staticType); 459 assertListOfString(functionReturnValue(2).staticType);
459 assertListOfString(functionReturnValue(3).staticType); 460 assertListOfString(functionReturnValue(3).staticType);
460 } 461 }
461 462
462 void test_functionLiteral_functionExpressionInvocation_typedArguments() { 463 test_functionLiteral_functionExpressionInvocation_typedArguments() async {
463 String code = r''' 464 String code = r'''
464 class Mapper<F, T> { 465 class Mapper<F, T> {
465 T map(T mapper(F x)) => mapper(null); 466 T map(T mapper(F x)) => mapper(null);
466 } 467 }
467 468
468 void main () { 469 void main () {
469 (new Mapper<int, String>().map)((int x) => null); 470 (new Mapper<int, String>().map)((int x) => null);
470 (new Mapper<int, String>().map)((int x) => "hello"); 471 (new Mapper<int, String>().map)((int x) => "hello");
471 (new Mapper<int, String>().map)((String x) => "hello"); 472 (new Mapper<int, String>().map)((String x) => "hello");
472 (new Mapper<int, String>().map)((int x) => 3); 473 (new Mapper<int, String>().map)((int x) => 3);
(...skipping 10 matching lines...) Expand all
483 return resolutionMap.elementDeclaredByFunctionExpression(exp).type; 484 return resolutionMap.elementDeclaredByFunctionExpression(exp).type;
484 } 485 }
485 486
486 _isFunction2Of(_isInt, _isString)(literal(0)); 487 _isFunction2Of(_isInt, _isString)(literal(0));
487 _isFunction2Of(_isInt, _isString)(literal(1)); 488 _isFunction2Of(_isInt, _isString)(literal(1));
488 _isFunction2Of(_isString, _isString)(literal(2)); 489 _isFunction2Of(_isString, _isString)(literal(2));
489 _isFunction2Of(_isInt, _isInt)(literal(3)); 490 _isFunction2Of(_isInt, _isInt)(literal(3));
490 _isFunction2Of(_isInt, _isString)(literal(4)); 491 _isFunction2Of(_isInt, _isString)(literal(4));
491 } 492 }
492 493
493 void test_functionLiteral_functionExpressionInvocation_unTypedArguments() { 494 test_functionLiteral_functionExpressionInvocation_unTypedArguments() async {
494 String code = r''' 495 String code = r'''
495 class Mapper<F, T> { 496 class Mapper<F, T> {
496 T map(T mapper(F x)) => mapper(null); 497 T map(T mapper(F x)) => mapper(null);
497 } 498 }
498 499
499 void main () { 500 void main () {
500 (new Mapper<int, String>().map)((x) => null); 501 (new Mapper<int, String>().map)((x) => null);
501 (new Mapper<int, String>().map)((x) => "hello"); 502 (new Mapper<int, String>().map)((x) => "hello");
502 (new Mapper<int, String>().map)((x) => "hello"); 503 (new Mapper<int, String>().map)((x) => "hello");
503 (new Mapper<int, String>().map)((x) => 3); 504 (new Mapper<int, String>().map)((x) => 3);
(...skipping 10 matching lines...) Expand all
514 return resolutionMap.elementDeclaredByFunctionExpression(exp).type; 515 return resolutionMap.elementDeclaredByFunctionExpression(exp).type;
515 } 516 }
516 517
517 _isFunction2Of(_isInt, _isString)(literal(0)); 518 _isFunction2Of(_isInt, _isString)(literal(0));
518 _isFunction2Of(_isInt, _isString)(literal(1)); 519 _isFunction2Of(_isInt, _isString)(literal(1));
519 _isFunction2Of(_isInt, _isString)(literal(2)); 520 _isFunction2Of(_isInt, _isString)(literal(2));
520 _isFunction2Of(_isInt, _isInt)(literal(3)); 521 _isFunction2Of(_isInt, _isInt)(literal(3));
521 _isFunction2Of(_isInt, _isString)(literal(4)); 522 _isFunction2Of(_isInt, _isString)(literal(4));
522 } 523 }
523 524
524 void test_functionLiteral_functionInvocation_typedArguments() { 525 test_functionLiteral_functionInvocation_typedArguments() async {
525 String code = r''' 526 String code = r'''
526 String map(String mapper(int x)) => mapper(null); 527 String map(String mapper(int x)) => mapper(null);
527 528
528 void main () { 529 void main () {
529 map((int x) => null); 530 map((int x) => null);
530 map((int x) => "hello"); 531 map((int x) => "hello");
531 map((String x) => "hello"); 532 map((String x) => "hello");
532 map((int x) => 3); 533 map((int x) => 3);
533 map((int x) {return 3;}); 534 map((int x) {return 3;});
534 } 535 }
535 '''; 536 ''';
536 CompilationUnit unit = resolveSource(code); 537 CompilationUnit unit = resolveSource(code);
537 List<Statement> statements = 538 List<Statement> statements =
538 AstFinder.getStatementsInTopLevelFunction(unit, "main"); 539 AstFinder.getStatementsInTopLevelFunction(unit, "main");
539 DartType literal(int i) { 540 DartType literal(int i) {
540 ExpressionStatement stmt = statements[i]; 541 ExpressionStatement stmt = statements[i];
541 MethodInvocation invk = stmt.expression; 542 MethodInvocation invk = stmt.expression;
542 FunctionExpression exp = invk.argumentList.arguments[0]; 543 FunctionExpression exp = invk.argumentList.arguments[0];
543 return resolutionMap.elementDeclaredByFunctionExpression(exp).type; 544 return resolutionMap.elementDeclaredByFunctionExpression(exp).type;
544 } 545 }
545 546
546 _isFunction2Of(_isInt, _isString)(literal(0)); 547 _isFunction2Of(_isInt, _isString)(literal(0));
547 _isFunction2Of(_isInt, _isString)(literal(1)); 548 _isFunction2Of(_isInt, _isString)(literal(1));
548 _isFunction2Of(_isString, _isString)(literal(2)); 549 _isFunction2Of(_isString, _isString)(literal(2));
549 _isFunction2Of(_isInt, _isInt)(literal(3)); 550 _isFunction2Of(_isInt, _isInt)(literal(3));
550 _isFunction2Of(_isInt, _isString)(literal(4)); 551 _isFunction2Of(_isInt, _isString)(literal(4));
551 } 552 }
552 553
553 void test_functionLiteral_functionInvocation_unTypedArguments() { 554 test_functionLiteral_functionInvocation_unTypedArguments() async {
554 String code = r''' 555 String code = r'''
555 String map(String mapper(int x)) => mapper(null); 556 String map(String mapper(int x)) => mapper(null);
556 557
557 void main () { 558 void main () {
558 map((x) => null); 559 map((x) => null);
559 map((x) => "hello"); 560 map((x) => "hello");
560 map((x) => "hello"); 561 map((x) => "hello");
561 map((x) => 3); 562 map((x) => 3);
562 map((x) {return 3;}); 563 map((x) {return 3;});
563 } 564 }
564 '''; 565 ''';
565 CompilationUnit unit = resolveSource(code); 566 CompilationUnit unit = resolveSource(code);
566 List<Statement> statements = 567 List<Statement> statements =
567 AstFinder.getStatementsInTopLevelFunction(unit, "main"); 568 AstFinder.getStatementsInTopLevelFunction(unit, "main");
568 DartType literal(int i) { 569 DartType literal(int i) {
569 ExpressionStatement stmt = statements[i]; 570 ExpressionStatement stmt = statements[i];
570 MethodInvocation invk = stmt.expression; 571 MethodInvocation invk = stmt.expression;
571 FunctionExpression exp = invk.argumentList.arguments[0]; 572 FunctionExpression exp = invk.argumentList.arguments[0];
572 return resolutionMap.elementDeclaredByFunctionExpression(exp).type; 573 return resolutionMap.elementDeclaredByFunctionExpression(exp).type;
573 } 574 }
574 575
575 _isFunction2Of(_isInt, _isString)(literal(0)); 576 _isFunction2Of(_isInt, _isString)(literal(0));
576 _isFunction2Of(_isInt, _isString)(literal(1)); 577 _isFunction2Of(_isInt, _isString)(literal(1));
577 _isFunction2Of(_isInt, _isString)(literal(2)); 578 _isFunction2Of(_isInt, _isString)(literal(2));
578 _isFunction2Of(_isInt, _isInt)(literal(3)); 579 _isFunction2Of(_isInt, _isInt)(literal(3));
579 _isFunction2Of(_isInt, _isString)(literal(4)); 580 _isFunction2Of(_isInt, _isString)(literal(4));
580 } 581 }
581 582
582 void test_functionLiteral_methodInvocation_typedArguments() { 583 test_functionLiteral_methodInvocation_typedArguments() async {
583 String code = r''' 584 String code = r'''
584 class Mapper<F, T> { 585 class Mapper<F, T> {
585 T map(T mapper(F x)) => mapper(null); 586 T map(T mapper(F x)) => mapper(null);
586 } 587 }
587 588
588 void main () { 589 void main () {
589 new Mapper<int, String>().map((int x) => null); 590 new Mapper<int, String>().map((int x) => null);
590 new Mapper<int, String>().map((int x) => "hello"); 591 new Mapper<int, String>().map((int x) => "hello");
591 new Mapper<int, String>().map((String x) => "hello"); 592 new Mapper<int, String>().map((String x) => "hello");
592 new Mapper<int, String>().map((int x) => 3); 593 new Mapper<int, String>().map((int x) => 3);
(...skipping 10 matching lines...) Expand all
603 return resolutionMap.elementDeclaredByFunctionExpression(exp).type; 604 return resolutionMap.elementDeclaredByFunctionExpression(exp).type;
604 } 605 }
605 606
606 _isFunction2Of(_isInt, _isString)(literal(0)); 607 _isFunction2Of(_isInt, _isString)(literal(0));
607 _isFunction2Of(_isInt, _isString)(literal(1)); 608 _isFunction2Of(_isInt, _isString)(literal(1));
608 _isFunction2Of(_isString, _isString)(literal(2)); 609 _isFunction2Of(_isString, _isString)(literal(2));
609 _isFunction2Of(_isInt, _isInt)(literal(3)); 610 _isFunction2Of(_isInt, _isInt)(literal(3));
610 _isFunction2Of(_isInt, _isString)(literal(4)); 611 _isFunction2Of(_isInt, _isString)(literal(4));
611 } 612 }
612 613
613 void test_functionLiteral_methodInvocation_unTypedArguments() { 614 test_functionLiteral_methodInvocation_unTypedArguments() async {
614 String code = r''' 615 String code = r'''
615 class Mapper<F, T> { 616 class Mapper<F, T> {
616 T map(T mapper(F x)) => mapper(null); 617 T map(T mapper(F x)) => mapper(null);
617 } 618 }
618 619
619 void main () { 620 void main () {
620 new Mapper<int, String>().map((x) => null); 621 new Mapper<int, String>().map((x) => null);
621 new Mapper<int, String>().map((x) => "hello"); 622 new Mapper<int, String>().map((x) => "hello");
622 new Mapper<int, String>().map((x) => "hello"); 623 new Mapper<int, String>().map((x) => "hello");
623 new Mapper<int, String>().map((x) => 3); 624 new Mapper<int, String>().map((x) => 3);
(...skipping 10 matching lines...) Expand all
634 return resolutionMap.elementDeclaredByFunctionExpression(exp).type; 635 return resolutionMap.elementDeclaredByFunctionExpression(exp).type;
635 } 636 }
636 637
637 _isFunction2Of(_isInt, _isString)(literal(0)); 638 _isFunction2Of(_isInt, _isString)(literal(0));
638 _isFunction2Of(_isInt, _isString)(literal(1)); 639 _isFunction2Of(_isInt, _isString)(literal(1));
639 _isFunction2Of(_isInt, _isString)(literal(2)); 640 _isFunction2Of(_isInt, _isString)(literal(2));
640 _isFunction2Of(_isInt, _isInt)(literal(3)); 641 _isFunction2Of(_isInt, _isInt)(literal(3));
641 _isFunction2Of(_isInt, _isString)(literal(4)); 642 _isFunction2Of(_isInt, _isString)(literal(4));
642 } 643 }
643 644
644 void test_functionLiteral_unTypedArgument_propagation() { 645 test_functionLiteral_unTypedArgument_propagation() async {
645 String code = r''' 646 String code = r'''
646 typedef T Function2<S, T>(S x); 647 typedef T Function2<S, T>(S x);
647 648
648 void main () { 649 void main () {
649 Function2<int, int> l0 = (x) => x; 650 Function2<int, int> l0 = (x) => x;
650 Function2<int, int> l1 = (x) => x+1; 651 Function2<int, int> l1 = (x) => x+1;
651 Function2<int, String> l2 = (x) => x; 652 Function2<int, String> l2 = (x) => x;
652 Function2<int, String> l3 = (x) => x.toLowerCase(); 653 Function2<int, String> l3 = (x) => x.toLowerCase();
653 Function2<String, String> l4 = (x) => x.toLowerCase(); 654 Function2<String, String> l4 = (x) => x.toLowerCase();
654 } 655 }
(...skipping 14 matching lines...) Expand all
669 } 670 }
670 } 671 }
671 672
672 expect(functionReturnValue(0).staticType, typeProvider.intType); 673 expect(functionReturnValue(0).staticType, typeProvider.intType);
673 expect(functionReturnValue(1).staticType, typeProvider.intType); 674 expect(functionReturnValue(1).staticType, typeProvider.intType);
674 expect(functionReturnValue(2).staticType, typeProvider.intType); 675 expect(functionReturnValue(2).staticType, typeProvider.intType);
675 expect(functionReturnValue(3).staticType, typeProvider.dynamicType); 676 expect(functionReturnValue(3).staticType, typeProvider.dynamicType);
676 expect(functionReturnValue(4).staticType, typeProvider.stringType); 677 expect(functionReturnValue(4).staticType, typeProvider.stringType);
677 } 678 }
678 679
679 void test_inference_hints() { 680 test_inference_hints() async {
680 Source source = addSource(r''' 681 Source source = addSource(r'''
681 void main () { 682 void main () {
682 var x = 3; 683 var x = 3;
683 List<int> l0 = []; 684 List<int> l0 = [];
684 } 685 }
685 '''); 686 ''');
686 resolve2(source); 687 resolve2(source);
687 assertNoErrors(source); 688 await assertNoErrors(source);
688 verify([source]); 689 verify([source]);
689 } 690 }
690 691
691 void test_inferredFieldDeclaration_propagation() { 692 test_inferredFieldDeclaration_propagation() async {
692 // Regression test for https://github.com/dart-lang/sdk/issues/25546 693 // Regression test for https://github.com/dart-lang/sdk/issues/25546
693 String code = r''' 694 String code = r'''
694 abstract class A { 695 abstract class A {
695 Map<int, List<int>> get map; 696 Map<int, List<int>> get map;
696 } 697 }
697 class B extends A { 698 class B extends A {
698 var map = { 42: [] }; 699 var map = { 42: [] };
699 } 700 }
700 class C extends A { 701 class C extends A {
701 get map => { 43: [] }; 702 get map => { 43: [] };
(...skipping 16 matching lines...) Expand all
718 MapLiteral mapLiteralC = (mapC.body as ExpressionFunctionBody).expression; 719 MapLiteral mapLiteralC = (mapC.body as ExpressionFunctionBody).expression;
719 assertMapOfIntToListOfInt(mapLiteralB.staticType); 720 assertMapOfIntToListOfInt(mapLiteralB.staticType);
720 assertMapOfIntToListOfInt(mapLiteralC.staticType); 721 assertMapOfIntToListOfInt(mapLiteralC.staticType);
721 722
722 ListLiteral listLiteralB = mapLiteralB.entries[0].value; 723 ListLiteral listLiteralB = mapLiteralB.entries[0].value;
723 ListLiteral listLiteralC = mapLiteralC.entries[0].value; 724 ListLiteral listLiteralC = mapLiteralC.entries[0].value;
724 assertListOfInt(listLiteralB.staticType); 725 assertListOfInt(listLiteralB.staticType);
725 assertListOfInt(listLiteralC.staticType); 726 assertListOfInt(listLiteralC.staticType);
726 } 727 }
727 728
728 void test_instanceCreation() { 729 test_instanceCreation() async {
729 String code = r''' 730 String code = r'''
730 class A<S, T> { 731 class A<S, T> {
731 S x; 732 S x;
732 T y; 733 T y;
733 A(this.x, this.y); 734 A(this.x, this.y);
734 A.named(this.x, this.y); 735 A.named(this.x, this.y);
735 } 736 }
736 737
737 class B<S, T> extends A<T, S> { 738 class B<S, T> extends A<T, S> {
738 B(S y, T x) : super(x, y); 739 B(S y, T x) : super(x, y);
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
937 AstFinder.getStatementsInTopLevelFunction(unit, "test9"); 938 AstFinder.getStatementsInTopLevelFunction(unit, "test9");
938 hasType(assertFOf([_isInt, _isString]), rhs(statements[0])); 939 hasType(assertFOf([_isInt, _isString]), rhs(statements[0]));
939 hasType(assertFOf([_isInt, _isString]), rhs(statements[1])); 940 hasType(assertFOf([_isInt, _isString]), rhs(statements[1]));
940 hasType(assertFOf([_isInt, _isString]), rhs(statements[2])); 941 hasType(assertFOf([_isInt, _isString]), rhs(statements[2]));
941 hasType(assertFOf([_isInt, _isString]), rhs(statements[3])); 942 hasType(assertFOf([_isInt, _isString]), rhs(statements[3]));
942 hasType(assertFOf([_isInt, _isString]), rhs(statements[4])); 943 hasType(assertFOf([_isInt, _isString]), rhs(statements[4]));
943 hasType(assertFOf([_isInt, _isString]), rhs(statements[5])); 944 hasType(assertFOf([_isInt, _isString]), rhs(statements[5]));
944 } 945 }
945 } 946 }
946 947
947 void test_listLiteral_nested() { 948 test_listLiteral_nested() async {
948 String code = r''' 949 String code = r'''
949 void main () { 950 void main () {
950 List<List<int>> l0 = [[]]; 951 List<List<int>> l0 = [[]];
951 Iterable<List<int>> l1 = [[3]]; 952 Iterable<List<int>> l1 = [[3]];
952 Iterable<List<int>> l2 = [[3], [4]]; 953 Iterable<List<int>> l2 = [[3], [4]];
953 List<List<int>> l3 = [["hello", 3], []]; 954 List<List<int>> l3 = [["hello", 3], []];
954 } 955 }
955 '''; 956 ''';
956 CompilationUnit unit = resolveSource(code); 957 CompilationUnit unit = resolveSource(code);
957 List<Statement> statements = 958 List<Statement> statements =
(...skipping 12 matching lines...) Expand all
970 assertListOfListOfInt(literal(0).staticType); 971 assertListOfListOfInt(literal(0).staticType);
971 assertListOfListOfInt(literal(1).staticType); 972 assertListOfListOfInt(literal(1).staticType);
972 assertListOfListOfInt(literal(2).staticType); 973 assertListOfListOfInt(literal(2).staticType);
973 assertListOfListOfInt(literal(3).staticType); 974 assertListOfListOfInt(literal(3).staticType);
974 975
975 assertListOfInt(literal(1).elements[0].staticType); 976 assertListOfInt(literal(1).elements[0].staticType);
976 assertListOfInt(literal(2).elements[0].staticType); 977 assertListOfInt(literal(2).elements[0].staticType);
977 assertListOfInt(literal(3).elements[0].staticType); 978 assertListOfInt(literal(3).elements[0].staticType);
978 } 979 }
979 980
980 void test_listLiteral_simple() { 981 test_listLiteral_simple() async {
981 String code = r''' 982 String code = r'''
982 void main () { 983 void main () {
983 List<int> l0 = []; 984 List<int> l0 = [];
984 List<int> l1 = [3]; 985 List<int> l1 = [3];
985 List<int> l2 = ["hello"]; 986 List<int> l2 = ["hello"];
986 List<int> l3 = ["hello", 3]; 987 List<int> l3 = ["hello", 3];
987 } 988 }
988 '''; 989 ''';
989 CompilationUnit unit = resolveSource(code); 990 CompilationUnit unit = resolveSource(code);
990 List<Statement> statements = 991 List<Statement> statements =
991 AstFinder.getStatementsInTopLevelFunction(unit, "main"); 992 AstFinder.getStatementsInTopLevelFunction(unit, "main");
992 DartType literal(int i) { 993 DartType literal(int i) {
993 VariableDeclarationStatement stmt = statements[i]; 994 VariableDeclarationStatement stmt = statements[i];
994 VariableDeclaration decl = stmt.variables.variables[0]; 995 VariableDeclaration decl = stmt.variables.variables[0];
995 ListLiteral exp = decl.initializer; 996 ListLiteral exp = decl.initializer;
996 return exp.staticType; 997 return exp.staticType;
997 } 998 }
998 999
999 Asserter<InterfaceType> assertListOfInt = _isListOf(_isInt); 1000 Asserter<InterfaceType> assertListOfInt = _isListOf(_isInt);
1000 1001
1001 assertListOfInt(literal(0)); 1002 assertListOfInt(literal(0));
1002 assertListOfInt(literal(1)); 1003 assertListOfInt(literal(1));
1003 assertListOfInt(literal(2)); 1004 assertListOfInt(literal(2));
1004 assertListOfInt(literal(3)); 1005 assertListOfInt(literal(3));
1005 } 1006 }
1006 1007
1007 void test_listLiteral_simple_const() { 1008 test_listLiteral_simple_const() async {
1008 String code = r''' 1009 String code = r'''
1009 void main () { 1010 void main () {
1010 const List<int> c0 = const []; 1011 const List<int> c0 = const [];
1011 const List<int> c1 = const [3]; 1012 const List<int> c1 = const [3];
1012 const List<int> c2 = const ["hello"]; 1013 const List<int> c2 = const ["hello"];
1013 const List<int> c3 = const ["hello", 3]; 1014 const List<int> c3 = const ["hello", 3];
1014 } 1015 }
1015 '''; 1016 ''';
1016 CompilationUnit unit = resolveSource(code); 1017 CompilationUnit unit = resolveSource(code);
1017 List<Statement> statements = 1018 List<Statement> statements =
1018 AstFinder.getStatementsInTopLevelFunction(unit, "main"); 1019 AstFinder.getStatementsInTopLevelFunction(unit, "main");
1019 DartType literal(int i) { 1020 DartType literal(int i) {
1020 VariableDeclarationStatement stmt = statements[i]; 1021 VariableDeclarationStatement stmt = statements[i];
1021 VariableDeclaration decl = stmt.variables.variables[0]; 1022 VariableDeclaration decl = stmt.variables.variables[0];
1022 ListLiteral exp = decl.initializer; 1023 ListLiteral exp = decl.initializer;
1023 return exp.staticType; 1024 return exp.staticType;
1024 } 1025 }
1025 1026
1026 Asserter<InterfaceType> assertListOfInt = _isListOf(_isInt); 1027 Asserter<InterfaceType> assertListOfInt = _isListOf(_isInt);
1027 1028
1028 assertListOfInt(literal(0)); 1029 assertListOfInt(literal(0));
1029 assertListOfInt(literal(1)); 1030 assertListOfInt(literal(1));
1030 assertListOfInt(literal(2)); 1031 assertListOfInt(literal(2));
1031 assertListOfInt(literal(3)); 1032 assertListOfInt(literal(3));
1032 } 1033 }
1033 1034
1034 void test_listLiteral_simple_disabled() { 1035 test_listLiteral_simple_disabled() async {
1035 String code = r''' 1036 String code = r'''
1036 void main () { 1037 void main () {
1037 List<int> l0 = <num>[]; 1038 List<int> l0 = <num>[];
1038 List<int> l1 = <num>[3]; 1039 List<int> l1 = <num>[3];
1039 List<int> l2 = <String>["hello"]; 1040 List<int> l2 = <String>["hello"];
1040 List<int> l3 = <dynamic>["hello", 3]; 1041 List<int> l3 = <dynamic>["hello", 3];
1041 } 1042 }
1042 '''; 1043 ''';
1043 CompilationUnit unit = resolveSource(code); 1044 CompilationUnit unit = resolveSource(code);
1044 List<Statement> statements = 1045 List<Statement> statements =
1045 AstFinder.getStatementsInTopLevelFunction(unit, "main"); 1046 AstFinder.getStatementsInTopLevelFunction(unit, "main");
1046 DartType literal(int i) { 1047 DartType literal(int i) {
1047 VariableDeclarationStatement stmt = statements[i]; 1048 VariableDeclarationStatement stmt = statements[i];
1048 VariableDeclaration decl = stmt.variables.variables[0]; 1049 VariableDeclaration decl = stmt.variables.variables[0];
1049 ListLiteral exp = decl.initializer; 1050 ListLiteral exp = decl.initializer;
1050 return exp.staticType; 1051 return exp.staticType;
1051 } 1052 }
1052 1053
1053 _isListOf(_isNum)(literal(0)); 1054 _isListOf(_isNum)(literal(0));
1054 _isListOf(_isNum)(literal(1)); 1055 _isListOf(_isNum)(literal(1));
1055 _isListOf(_isString)(literal(2)); 1056 _isListOf(_isString)(literal(2));
1056 _isListOf(_isDynamic)(literal(3)); 1057 _isListOf(_isDynamic)(literal(3));
1057 } 1058 }
1058 1059
1059 void test_listLiteral_simple_subtype() { 1060 test_listLiteral_simple_subtype() async {
1060 String code = r''' 1061 String code = r'''
1061 void main () { 1062 void main () {
1062 Iterable<int> l0 = []; 1063 Iterable<int> l0 = [];
1063 Iterable<int> l1 = [3]; 1064 Iterable<int> l1 = [3];
1064 Iterable<int> l2 = ["hello"]; 1065 Iterable<int> l2 = ["hello"];
1065 Iterable<int> l3 = ["hello", 3]; 1066 Iterable<int> l3 = ["hello", 3];
1066 } 1067 }
1067 '''; 1068 ''';
1068 CompilationUnit unit = resolveSource(code); 1069 CompilationUnit unit = resolveSource(code);
1069 List<Statement> statements = 1070 List<Statement> statements =
1070 AstFinder.getStatementsInTopLevelFunction(unit, "main"); 1071 AstFinder.getStatementsInTopLevelFunction(unit, "main");
1071 DartType literal(int i) { 1072 DartType literal(int i) {
1072 VariableDeclarationStatement stmt = statements[i]; 1073 VariableDeclarationStatement stmt = statements[i];
1073 VariableDeclaration decl = stmt.variables.variables[0]; 1074 VariableDeclaration decl = stmt.variables.variables[0];
1074 ListLiteral exp = decl.initializer; 1075 ListLiteral exp = decl.initializer;
1075 return exp.staticType; 1076 return exp.staticType;
1076 } 1077 }
1077 1078
1078 Asserter<InterfaceType> assertListOfInt = _isListOf(_isInt); 1079 Asserter<InterfaceType> assertListOfInt = _isListOf(_isInt);
1079 1080
1080 assertListOfInt(literal(0)); 1081 assertListOfInt(literal(0));
1081 assertListOfInt(literal(1)); 1082 assertListOfInt(literal(1));
1082 assertListOfInt(literal(2)); 1083 assertListOfInt(literal(2));
1083 assertListOfInt(literal(3)); 1084 assertListOfInt(literal(3));
1084 } 1085 }
1085 1086
1086 void test_mapLiteral_nested() { 1087 test_mapLiteral_nested() async {
1087 String code = r''' 1088 String code = r'''
1088 void main () { 1089 void main () {
1089 Map<int, List<String>> l0 = {}; 1090 Map<int, List<String>> l0 = {};
1090 Map<int, List<String>> l1 = {3: ["hello"]}; 1091 Map<int, List<String>> l1 = {3: ["hello"]};
1091 Map<int, List<String>> l2 = {"hello": ["hello"]}; 1092 Map<int, List<String>> l2 = {"hello": ["hello"]};
1092 Map<int, List<String>> l3 = {3: [3]}; 1093 Map<int, List<String>> l3 = {3: [3]};
1093 Map<int, List<String>> l4 = {3:["hello"], "hello": [3]}; 1094 Map<int, List<String>> l4 = {3:["hello"], "hello": [3]};
1094 } 1095 }
1095 '''; 1096 ''';
1096 CompilationUnit unit = resolveSource(code); 1097 CompilationUnit unit = resolveSource(code);
(...skipping 15 matching lines...) Expand all
1112 assertMapOfIntToListOfString(literal(2).staticType); 1113 assertMapOfIntToListOfString(literal(2).staticType);
1113 assertMapOfIntToListOfString(literal(3).staticType); 1114 assertMapOfIntToListOfString(literal(3).staticType);
1114 assertMapOfIntToListOfString(literal(4).staticType); 1115 assertMapOfIntToListOfString(literal(4).staticType);
1115 1116
1116 assertListOfString(literal(1).entries[0].value.staticType); 1117 assertListOfString(literal(1).entries[0].value.staticType);
1117 assertListOfString(literal(2).entries[0].value.staticType); 1118 assertListOfString(literal(2).entries[0].value.staticType);
1118 assertListOfString(literal(3).entries[0].value.staticType); 1119 assertListOfString(literal(3).entries[0].value.staticType);
1119 assertListOfString(literal(4).entries[0].value.staticType); 1120 assertListOfString(literal(4).entries[0].value.staticType);
1120 } 1121 }
1121 1122
1122 void test_mapLiteral_simple() { 1123 test_mapLiteral_simple() async {
1123 String code = r''' 1124 String code = r'''
1124 void main () { 1125 void main () {
1125 Map<int, String> l0 = {}; 1126 Map<int, String> l0 = {};
1126 Map<int, String> l1 = {3: "hello"}; 1127 Map<int, String> l1 = {3: "hello"};
1127 Map<int, String> l2 = {"hello": "hello"}; 1128 Map<int, String> l2 = {"hello": "hello"};
1128 Map<int, String> l3 = {3: 3}; 1129 Map<int, String> l3 = {3: 3};
1129 Map<int, String> l4 = {3:"hello", "hello": 3}; 1130 Map<int, String> l4 = {3:"hello", "hello": 3};
1130 } 1131 }
1131 '''; 1132 ''';
1132 CompilationUnit unit = resolveSource(code); 1133 CompilationUnit unit = resolveSource(code);
1133 List<Statement> statements = 1134 List<Statement> statements =
1134 AstFinder.getStatementsInTopLevelFunction(unit, "main"); 1135 AstFinder.getStatementsInTopLevelFunction(unit, "main");
1135 DartType literal(int i) { 1136 DartType literal(int i) {
1136 VariableDeclarationStatement stmt = statements[i]; 1137 VariableDeclarationStatement stmt = statements[i];
1137 VariableDeclaration decl = stmt.variables.variables[0]; 1138 VariableDeclaration decl = stmt.variables.variables[0];
1138 MapLiteral exp = decl.initializer; 1139 MapLiteral exp = decl.initializer;
1139 return exp.staticType; 1140 return exp.staticType;
1140 } 1141 }
1141 1142
1142 Asserter<InterfaceType> assertMapOfIntToString = 1143 Asserter<InterfaceType> assertMapOfIntToString =
1143 _isMapOf(_isInt, _isString); 1144 _isMapOf(_isInt, _isString);
1144 1145
1145 assertMapOfIntToString(literal(0)); 1146 assertMapOfIntToString(literal(0));
1146 assertMapOfIntToString(literal(1)); 1147 assertMapOfIntToString(literal(1));
1147 assertMapOfIntToString(literal(2)); 1148 assertMapOfIntToString(literal(2));
1148 assertMapOfIntToString(literal(3)); 1149 assertMapOfIntToString(literal(3));
1149 } 1150 }
1150 1151
1151 void test_mapLiteral_simple_disabled() { 1152 test_mapLiteral_simple_disabled() async {
1152 String code = r''' 1153 String code = r'''
1153 void main () { 1154 void main () {
1154 Map<int, String> l0 = <int, dynamic>{}; 1155 Map<int, String> l0 = <int, dynamic>{};
1155 Map<int, String> l1 = <int, dynamic>{3: "hello"}; 1156 Map<int, String> l1 = <int, dynamic>{3: "hello"};
1156 Map<int, String> l2 = <int, dynamic>{"hello": "hello"}; 1157 Map<int, String> l2 = <int, dynamic>{"hello": "hello"};
1157 Map<int, String> l3 = <int, dynamic>{3: 3}; 1158 Map<int, String> l3 = <int, dynamic>{3: 3};
1158 } 1159 }
1159 '''; 1160 ''';
1160 CompilationUnit unit = resolveSource(code); 1161 CompilationUnit unit = resolveSource(code);
1161 List<Statement> statements = 1162 List<Statement> statements =
1162 AstFinder.getStatementsInTopLevelFunction(unit, "main"); 1163 AstFinder.getStatementsInTopLevelFunction(unit, "main");
1163 DartType literal(int i) { 1164 DartType literal(int i) {
1164 VariableDeclarationStatement stmt = statements[i]; 1165 VariableDeclarationStatement stmt = statements[i];
1165 VariableDeclaration decl = stmt.variables.variables[0]; 1166 VariableDeclaration decl = stmt.variables.variables[0];
1166 MapLiteral exp = decl.initializer; 1167 MapLiteral exp = decl.initializer;
1167 return exp.staticType; 1168 return exp.staticType;
1168 } 1169 }
1169 1170
1170 Asserter<InterfaceType> assertMapOfIntToDynamic = 1171 Asserter<InterfaceType> assertMapOfIntToDynamic =
1171 _isMapOf(_isInt, _isDynamic); 1172 _isMapOf(_isInt, _isDynamic);
1172 1173
1173 assertMapOfIntToDynamic(literal(0)); 1174 assertMapOfIntToDynamic(literal(0));
1174 assertMapOfIntToDynamic(literal(1)); 1175 assertMapOfIntToDynamic(literal(1));
1175 assertMapOfIntToDynamic(literal(2)); 1176 assertMapOfIntToDynamic(literal(2));
1176 assertMapOfIntToDynamic(literal(3)); 1177 assertMapOfIntToDynamic(literal(3));
1177 } 1178 }
1178 1179
1179 void test_methodDeclaration_body_propagation() { 1180 test_methodDeclaration_body_propagation() async {
1180 String code = r''' 1181 String code = r'''
1181 class A { 1182 class A {
1182 List<String> m0(int x) => ["hello"]; 1183 List<String> m0(int x) => ["hello"];
1183 List<String> m1(int x) {return [3];}; 1184 List<String> m1(int x) {return [3];};
1184 } 1185 }
1185 '''; 1186 ''';
1186 CompilationUnit unit = resolveSource(code); 1187 CompilationUnit unit = resolveSource(code);
1187 Expression methodReturnValue(String methodName) { 1188 Expression methodReturnValue(String methodName) {
1188 MethodDeclaration method = 1189 MethodDeclaration method =
1189 AstFinder.getMethodInClass(unit, "A", methodName); 1190 AstFinder.getMethodInClass(unit, "A", methodName);
1190 FunctionBody body = method.body; 1191 FunctionBody body = method.body;
1191 if (body is ExpressionFunctionBody) { 1192 if (body is ExpressionFunctionBody) {
1192 return body.expression; 1193 return body.expression;
1193 } else { 1194 } else {
1194 Statement stmt = (body as BlockFunctionBody).block.statements[0]; 1195 Statement stmt = (body as BlockFunctionBody).block.statements[0];
1195 return (stmt as ReturnStatement).expression; 1196 return (stmt as ReturnStatement).expression;
1196 } 1197 }
1197 } 1198 }
1198 1199
1199 Asserter<InterfaceType> assertListOfString = _isListOf(_isString); 1200 Asserter<InterfaceType> assertListOfString = _isListOf(_isString);
1200 assertListOfString(methodReturnValue("m0").staticType); 1201 assertListOfString(methodReturnValue("m0").staticType);
1201 assertListOfString(methodReturnValue("m1").staticType); 1202 assertListOfString(methodReturnValue("m1").staticType);
1202 } 1203 }
1203 1204
1204 void test_redirectingConstructor_propagation() { 1205 test_redirectingConstructor_propagation() async {
1205 String code = r''' 1206 String code = r'''
1206 class A { 1207 class A {
1207 A() : this.named([]); 1208 A() : this.named([]);
1208 A.named(List<String> x); 1209 A.named(List<String> x);
1209 } 1210 }
1210 '''; 1211 ''';
1211 CompilationUnit unit = resolveSource(code); 1212 CompilationUnit unit = resolveSource(code);
1212 1213
1213 ConstructorDeclaration constructor = 1214 ConstructorDeclaration constructor =
1214 AstFinder.getConstructorInClass(unit, "A", null); 1215 AstFinder.getConstructorInClass(unit, "A", null);
1215 RedirectingConstructorInvocation invocation = constructor.initializers[0]; 1216 RedirectingConstructorInvocation invocation = constructor.initializers[0];
1216 Expression exp = invocation.argumentList.arguments[0]; 1217 Expression exp = invocation.argumentList.arguments[0];
1217 _isListOf(_isString)(exp.staticType); 1218 _isListOf(_isString)(exp.staticType);
1218 } 1219 }
1219 1220
1220 void test_superConstructorInvocation_propagation() { 1221 test_superConstructorInvocation_propagation() async {
1221 String code = r''' 1222 String code = r'''
1222 class B { 1223 class B {
1223 B(List<String>); 1224 B(List<String>);
1224 } 1225 }
1225 class A extends B { 1226 class A extends B {
1226 A() : super([]); 1227 A() : super([]);
1227 } 1228 }
1228 '''; 1229 ''';
1229 CompilationUnit unit = resolveSource(code); 1230 CompilationUnit unit = resolveSource(code);
1230 1231
1231 ConstructorDeclaration constructor = 1232 ConstructorDeclaration constructor =
1232 AstFinder.getConstructorInClass(unit, "A", null); 1233 AstFinder.getConstructorInClass(unit, "A", null);
1233 SuperConstructorInvocation invocation = constructor.initializers[0]; 1234 SuperConstructorInvocation invocation = constructor.initializers[0];
1234 Expression exp = invocation.argumentList.arguments[0]; 1235 Expression exp = invocation.argumentList.arguments[0];
1235 _isListOf(_isString)(exp.staticType); 1236 _isListOf(_isString)(exp.staticType);
1236 } 1237 }
1237 1238
1238 void test_sync_star_method_propagation() { 1239 test_sync_star_method_propagation() async {
1239 String code = r''' 1240 String code = r'''
1240 import "dart:async"; 1241 import "dart:async";
1241 class A { 1242 class A {
1242 Iterable f0() sync* { yield []; } 1243 Iterable f0() sync* { yield []; }
1243 Iterable f1() sync* { yield* new List(); } 1244 Iterable f1() sync* { yield* new List(); }
1244 1245
1245 Iterable<List<int>> f2() sync* { yield []; } 1246 Iterable<List<int>> f2() sync* { yield []; }
1246 Iterable<List<int>> f3() sync* { yield* new List(); } 1247 Iterable<List<int>> f3() sync* { yield* new List(); }
1247 } 1248 }
1248 '''; 1249 ''';
1249 CompilationUnit unit = resolveSource(code); 1250 CompilationUnit unit = resolveSource(code);
1250 1251
1251 void check(String name, Asserter<InterfaceType> typeTest) { 1252 void check(String name, Asserter<InterfaceType> typeTest) {
1252 MethodDeclaration test = AstFinder.getMethodInClass(unit, "A", name); 1253 MethodDeclaration test = AstFinder.getMethodInClass(unit, "A", name);
1253 BlockFunctionBody body = test.body; 1254 BlockFunctionBody body = test.body;
1254 YieldStatement stmt = body.block.statements[0]; 1255 YieldStatement stmt = body.block.statements[0];
1255 Expression exp = stmt.expression; 1256 Expression exp = stmt.expression;
1256 typeTest(exp.staticType); 1257 typeTest(exp.staticType);
1257 } 1258 }
1258 1259
1259 check("f0", _isListOf(_isDynamic)); 1260 check("f0", _isListOf(_isDynamic));
1260 check("f1", _isListOf(_isDynamic)); 1261 check("f1", _isListOf(_isDynamic));
1261 1262
1262 check("f2", _isListOf(_isInt)); 1263 check("f2", _isListOf(_isInt));
1263 check("f3", _isListOf((DartType type) => _isListOf(_isInt)(type))); 1264 check("f3", _isListOf((DartType type) => _isListOf(_isInt)(type)));
1264 } 1265 }
1265 1266
1266 void test_sync_star_propagation() { 1267 test_sync_star_propagation() async {
1267 String code = r''' 1268 String code = r'''
1268 import "dart:async"; 1269 import "dart:async";
1269 1270
1270 Iterable f0() sync* { yield []; } 1271 Iterable f0() sync* { yield []; }
1271 Iterable f1() sync* { yield* new List(); } 1272 Iterable f1() sync* { yield* new List(); }
1272 1273
1273 Iterable<List<int>> f2() sync* { yield []; } 1274 Iterable<List<int>> f2() sync* { yield []; }
1274 Iterable<List<int>> f3() sync* { yield* new List(); } 1275 Iterable<List<int>> f3() sync* { yield* new List(); }
1275 '''; 1276 ''';
1276 CompilationUnit unit = resolveSource(code); 1277 CompilationUnit unit = resolveSource(code);
(...skipping 12 matching lines...) Expand all
1289 check("f2", _isListOf(_isInt)); 1290 check("f2", _isListOf(_isInt));
1290 check("f3", _isListOf((DartType type) => _isListOf(_isInt)(type))); 1291 check("f3", _isListOf((DartType type) => _isListOf(_isInt)(type)));
1291 } 1292 }
1292 } 1293 }
1293 1294
1294 /** 1295 /**
1295 * Strong mode static analyzer end to end tests 1296 * Strong mode static analyzer end to end tests
1296 */ 1297 */
1297 @reflectiveTest 1298 @reflectiveTest
1298 class StrongModeStaticTypeAnalyzer2Test extends StaticTypeAnalyzer2TestShared { 1299 class StrongModeStaticTypeAnalyzer2Test extends StaticTypeAnalyzer2TestShared {
1299 void fail_genericMethod_tearoff_instantiated() { 1300 fail_genericMethod_tearoff_instantiated() async {
1300 resolveTestUnit(r''' 1301 await resolveTestUnit(r'''
1301 class C<E> { 1302 class C<E> {
1302 /*=T*/ f/*<T>*/(E e) => null; 1303 /*=T*/ f/*<T>*/(E e) => null;
1303 static /*=T*/ g/*<T>*/(/*=T*/ e) => null; 1304 static /*=T*/ g/*<T>*/(/*=T*/ e) => null;
1304 static final h = g; 1305 static final h = g;
1305 } 1306 }
1306 1307
1307 /*=T*/ topF/*<T>*/(/*=T*/ e) => null; 1308 /*=T*/ topF/*<T>*/(/*=T*/ e) => null;
1308 var topG = topF; 1309 var topG = topF;
1309 void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) { 1310 void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) {
1310 var c = new C<int>(); 1311 var c = new C<int>();
1311 /*=T*/ lf/*<T>*/(/*=T*/ e) => null; 1312 /*=T*/ lf/*<T>*/(/*=T*/ e) => null;
1312 var methodTearOffInst = c.f/*<int>*/; 1313 var methodTearOffInst = c.f/*<int>*/;
1313 var staticTearOffInst = C.g/*<int>*/; 1314 var staticTearOffInst = C.g/*<int>*/;
1314 var staticFieldTearOffInst = C.h/*<int>*/; 1315 var staticFieldTearOffInst = C.h/*<int>*/;
1315 var topFunTearOffInst = topF/*<int>*/; 1316 var topFunTearOffInst = topF/*<int>*/;
1316 var topFieldTearOffInst = topG/*<int>*/; 1317 var topFieldTearOffInst = topG/*<int>*/;
1317 var localTearOffInst = lf/*<int>*/; 1318 var localTearOffInst = lf/*<int>*/;
1318 var paramTearOffInst = pf/*<int>*/; 1319 var paramTearOffInst = pf/*<int>*/;
1319 } 1320 }
1320 '''); 1321 ''');
1321 expectIdentifierType('methodTearOffInst', "(int) → int"); 1322 expectIdentifierType('methodTearOffInst', "(int) → int");
1322 expectIdentifierType('staticTearOffInst', "(int) → int"); 1323 expectIdentifierType('staticTearOffInst', "(int) → int");
1323 expectIdentifierType('staticFieldTearOffInst', "(int) → int"); 1324 expectIdentifierType('staticFieldTearOffInst', "(int) → int");
1324 expectIdentifierType('topFunTearOffInst', "(int) → int"); 1325 expectIdentifierType('topFunTearOffInst', "(int) → int");
1325 expectIdentifierType('topFieldTearOffInst', "(int) → int"); 1326 expectIdentifierType('topFieldTearOffInst', "(int) → int");
1326 expectIdentifierType('localTearOffInst', "(int) → int"); 1327 expectIdentifierType('localTearOffInst', "(int) → int");
1327 expectIdentifierType('paramTearOffInst', "(int) → int"); 1328 expectIdentifierType('paramTearOffInst', "(int) → int");
1328 } 1329 }
1329 1330
1330 void objectMethodOnFunctions_helper(String code) { 1331 Future<Null> _objectMethodOnFunctions_helper2(String code) async {
1331 resolveTestUnit(code); 1332 await resolveTestUnit(code);
1332 expectIdentifierType('t0', "String"); 1333 expectIdentifierType('t0', "String");
1333 expectIdentifierType('t1', "() → String"); 1334 expectIdentifierType('t1', "() → String");
1334 expectIdentifierType('t2', "int"); 1335 expectIdentifierType('t2', "int");
1335 expectIdentifierType('t3', "String"); 1336 expectIdentifierType('t3', "String");
1336 expectIdentifierType('t4', "() → String"); 1337 expectIdentifierType('t4', "() → String");
1337 expectIdentifierType('t5', "int"); 1338 expectIdentifierType('t5', "int");
1338 } 1339 }
1339 1340
1340 void setUp() { 1341 void setUp() {
1341 super.setUp(); 1342 super.setUp();
1342 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); 1343 AnalysisOptionsImpl options = new AnalysisOptionsImpl();
1343 options.strongMode = true; 1344 options.strongMode = true;
1344 resetWithOptions(options); 1345 resetWithOptions(options);
1345 } 1346 }
1346 1347
1347 void test_dynamicObjectGetter_hashCode() { 1348 test_dynamicObjectGetter_hashCode() async {
1348 String code = r''' 1349 String code = r'''
1349 main() { 1350 main() {
1350 dynamic a = null; 1351 dynamic a = null;
1351 var foo = a.hashCode; 1352 var foo = a.hashCode;
1352 } 1353 }
1353 '''; 1354 ''';
1354 resolveTestUnit(code); 1355 await resolveTestUnit(code);
1355 expectInitializerType('foo', 'int', isNull); 1356 expectInitializerType('foo', 'int', isNull);
1356 } 1357 }
1357 1358
1358 void test_dynamicObjectMethod_toString() { 1359 test_dynamicObjectMethod_toString() async {
1359 String code = r''' 1360 String code = r'''
1360 main() { 1361 main() {
1361 dynamic a = null; 1362 dynamic a = null;
1362 var foo = a.toString(); 1363 var foo = a.toString();
1363 } 1364 }
1364 '''; 1365 ''';
1365 resolveTestUnit(code); 1366 await resolveTestUnit(code);
1366 expectInitializerType('foo', 'String', isNull); 1367 expectInitializerType('foo', 'String', isNull);
1367 } 1368 }
1368 1369
1369 void test_genericFunction() { 1370 test_genericFunction() async {
1370 resolveTestUnit(r'/*=T*/ f/*<T>*/(/*=T*/ x) => null;'); 1371 await resolveTestUnit(r'/*=T*/ f/*<T>*/(/*=T*/ x) => null;');
1371 expectFunctionType('f', '<T>(T) → T', 1372 expectFunctionType('f', '<T>(T) → T',
1372 elementTypeParams: '[T]', typeFormals: '[T]'); 1373 elementTypeParams: '[T]', typeFormals: '[T]');
1373 SimpleIdentifier f = findIdentifier('f'); 1374 SimpleIdentifier f = findIdentifier('f');
1374 FunctionElementImpl e = f.staticElement; 1375 FunctionElementImpl e = f.staticElement;
1375 FunctionType ft = e.type.instantiate([typeProvider.stringType]); 1376 FunctionType ft = e.type.instantiate([typeProvider.stringType]);
1376 expect(ft.toString(), '(String) → String'); 1377 expect(ft.toString(), '(String) → String');
1377 } 1378 }
1378 1379
1379 void test_genericFunction_bounds() { 1380 test_genericFunction_bounds() async {
1380 resolveTestUnit(r'/*=T*/ f/*<T extends num>*/(/*=T*/ x) => null;'); 1381 await resolveTestUnit(r'/*=T*/ f/*<T extends num>*/(/*=T*/ x) => null;');
1381 expectFunctionType('f', '<T extends num>(T) → T', 1382 expectFunctionType('f', '<T extends num>(T) → T',
1382 elementTypeParams: '[T extends num]', typeFormals: '[T extends num]'); 1383 elementTypeParams: '[T extends num]', typeFormals: '[T extends num]');
1383 } 1384 }
1384 1385
1385 void test_genericFunction_parameter() { 1386 test_genericFunction_parameter() async {
1386 resolveTestUnit(r''' 1387 await resolveTestUnit(r'''
1387 void g(/*=T*/ f/*<T>*/(/*=T*/ x)) {} 1388 void g(/*=T*/ f/*<T>*/(/*=T*/ x)) {}
1388 '''); 1389 ''');
1389 expectFunctionType('f', '<T>(T) → T', 1390 expectFunctionType('f', '<T>(T) → T',
1390 elementTypeParams: '[T]', typeFormals: '[T]'); 1391 elementTypeParams: '[T]', typeFormals: '[T]');
1391 SimpleIdentifier f = findIdentifier('f'); 1392 SimpleIdentifier f = findIdentifier('f');
1392 ParameterElementImpl e = f.staticElement; 1393 ParameterElementImpl e = f.staticElement;
1393 FunctionType type = e.type; 1394 FunctionType type = e.type;
1394 FunctionType ft = type.instantiate([typeProvider.stringType]); 1395 FunctionType ft = type.instantiate([typeProvider.stringType]);
1395 expect(ft.toString(), '(String) → String'); 1396 expect(ft.toString(), '(String) → String');
1396 } 1397 }
1397 1398
1398 void test_genericFunction_static() { 1399 test_genericFunction_static() async {
1399 resolveTestUnit(r''' 1400 await resolveTestUnit(r'''
1400 class C<E> { 1401 class C<E> {
1401 static /*=T*/ f/*<T>*/(/*=T*/ x) => null; 1402 static /*=T*/ f/*<T>*/(/*=T*/ x) => null;
1402 } 1403 }
1403 '''); 1404 ''');
1404 expectFunctionType('f', '<T>(T) → T', 1405 expectFunctionType('f', '<T>(T) → T',
1405 elementTypeParams: '[T]', typeFormals: '[T]'); 1406 elementTypeParams: '[T]', typeFormals: '[T]');
1406 SimpleIdentifier f = findIdentifier('f'); 1407 SimpleIdentifier f = findIdentifier('f');
1407 MethodElementImpl e = f.staticElement; 1408 MethodElementImpl e = f.staticElement;
1408 FunctionType ft = e.type.instantiate([typeProvider.stringType]); 1409 FunctionType ft = e.type.instantiate([typeProvider.stringType]);
1409 expect(ft.toString(), '(String) → String'); 1410 expect(ft.toString(), '(String) → String');
1410 } 1411 }
1411 1412
1412 void test_genericFunction_typedef() { 1413 test_genericFunction_typedef() async {
1413 String code = r''' 1414 String code = r'''
1414 typedef T F<T>(T x); 1415 typedef T F<T>(T x);
1415 F f0; 1416 F f0;
1416 1417
1417 class C { 1418 class C {
1418 static F f1; 1419 static F f1;
1419 F f2; 1420 F f2;
1420 void g(F f3) { 1421 void g(F f3) {
1421 F f4; 1422 F f4;
1422 f0(3); 1423 f0(3);
(...skipping 10 matching lines...) Expand all
1433 void g(F f3) { 1434 void g(F f3) {
1434 F f4; 1435 F f4;
1435 f0(3); 1436 f0(3);
1436 f1(3); 1437 f1(3);
1437 f2(3); 1438 f2(3);
1438 f3(3); 1439 f3(3);
1439 f4(3); 1440 f4(3);
1440 } 1441 }
1441 } 1442 }
1442 '''; 1443 ''';
1443 resolveTestUnit(code); 1444 await resolveTestUnit(code);
1444 1445
1445 checkBody(String className) { 1446 checkBody(String className) {
1446 List<Statement> statements = 1447 List<Statement> statements =
1447 AstFinder.getStatementsInMethod(testUnit, className, "g"); 1448 AstFinder.getStatementsInMethod(testUnit, className, "g");
1448 1449
1449 for (int i = 1; i <= 5; i++) { 1450 for (int i = 1; i <= 5; i++) {
1450 Expression exp = (statements[i] as ExpressionStatement).expression; 1451 Expression exp = (statements[i] as ExpressionStatement).expression;
1451 expect(exp.staticType, typeProvider.dynamicType); 1452 expect(exp.staticType, typeProvider.dynamicType);
1452 } 1453 }
1453 } 1454 }
1454 1455
1455 checkBody("C"); 1456 checkBody("C");
1456 checkBody("D"); 1457 checkBody("D");
1457 } 1458 }
1458 1459
1459 void test_genericFunction_upwardsAndDownwards() { 1460 test_genericFunction_upwardsAndDownwards() async {
1460 // Regression tests for https://github.com/dart-lang/sdk/issues/27151. 1461 // Regression tests for https://github.com/dart-lang/sdk/issues/27151.
1461 resolveTestUnit(r'List<num> x = [1, 2];'); 1462 await resolveTestUnit(r'List<num> x = [1, 2];');
1462 expectInitializerType('x', 'List<int>'); 1463 expectInitializerType('x', 'List<int>');
1463 } 1464 }
1464 1465
1465 void test_genericMethod() { 1466 test_genericMethod() async {
1466 resolveTestUnit(r''' 1467 await resolveTestUnit(r'''
1467 class C<E> { 1468 class C<E> {
1468 List/*<T>*/ f/*<T>*/(E e) => null; 1469 List/*<T>*/ f/*<T>*/(E e) => null;
1469 } 1470 }
1470 main() { 1471 main() {
1471 C<String> cOfString; 1472 C<String> cOfString;
1472 } 1473 }
1473 '''); 1474 ''');
1474 expectFunctionType('f', '<T>(E) → List<T>', 1475 expectFunctionType('f', '<T>(E) → List<T>',
1475 elementTypeParams: '[T]', 1476 elementTypeParams: '[T]',
1476 typeParams: '[E]', 1477 typeParams: '[E]',
1477 typeArgs: '[E]', 1478 typeArgs: '[E]',
1478 typeFormals: '[T]'); 1479 typeFormals: '[T]');
1479 SimpleIdentifier c = findIdentifier('cOfString'); 1480 SimpleIdentifier c = findIdentifier('cOfString');
1480 FunctionType ft = (c.staticType as InterfaceType).getMethod('f').type; 1481 FunctionType ft = (c.staticType as InterfaceType).getMethod('f').type;
1481 expect(ft.toString(), '<T>(String) → List<T>'); 1482 expect(ft.toString(), '<T>(String) → List<T>');
1482 ft = ft.instantiate([typeProvider.intType]); 1483 ft = ft.instantiate([typeProvider.intType]);
1483 expect(ft.toString(), '(String) → List<int>'); 1484 expect(ft.toString(), '(String) → List<int>');
1484 expect('${ft.typeArguments}/${ft.typeParameters}', '[String, int]/[E, T]'); 1485 expect('${ft.typeArguments}/${ft.typeParameters}', '[String, int]/[E, T]');
1485 } 1486 }
1486 1487
1487 void test_genericMethod_explicitTypeParams() { 1488 test_genericMethod_explicitTypeParams() async {
1488 resolveTestUnit(r''' 1489 await resolveTestUnit(r'''
1489 class C<E> { 1490 class C<E> {
1490 List/*<T>*/ f/*<T>*/(E e) => null; 1491 List/*<T>*/ f/*<T>*/(E e) => null;
1491 } 1492 }
1492 main() { 1493 main() {
1493 C<String> cOfString; 1494 C<String> cOfString;
1494 var x = cOfString.f/*<int>*/('hi'); 1495 var x = cOfString.f/*<int>*/('hi');
1495 } 1496 }
1496 '''); 1497 ''');
1497 MethodInvocation f = findIdentifier('f/*<int>*/').parent; 1498 MethodInvocation f = findIdentifier('f/*<int>*/').parent;
1498 FunctionType ft = f.staticInvokeType; 1499 FunctionType ft = f.staticInvokeType;
1499 expect(ft.toString(), '(String) → List<int>'); 1500 expect(ft.toString(), '(String) → List<int>');
1500 expect('${ft.typeArguments}/${ft.typeParameters}', '[String, int]/[E, T]'); 1501 expect('${ft.typeArguments}/${ft.typeParameters}', '[String, int]/[E, T]');
1501 1502
1502 SimpleIdentifier x = findIdentifier('x'); 1503 SimpleIdentifier x = findIdentifier('x');
1503 expect(x.staticType, 1504 expect(x.staticType,
1504 typeProvider.listType.instantiate([typeProvider.intType])); 1505 typeProvider.listType.instantiate([typeProvider.intType]));
1505 } 1506 }
1506 1507
1507 void test_genericMethod_functionExpressionInvocation_explicit() { 1508 test_genericMethod_functionExpressionInvocation_explicit() async {
1508 resolveTestUnit(r''' 1509 await resolveTestUnit(r'''
1509 class C<E> { 1510 class C<E> {
1510 /*=T*/ f/*<T>*/(/*=T*/ e) => null; 1511 /*=T*/ f/*<T>*/(/*=T*/ e) => null;
1511 static /*=T*/ g/*<T>*/(/*=T*/ e) => null; 1512 static /*=T*/ g/*<T>*/(/*=T*/ e) => null;
1512 static final h = g; 1513 static final h = g;
1513 } 1514 }
1514 1515
1515 /*=T*/ topF/*<T>*/(/*=T*/ e) => null; 1516 /*=T*/ topF/*<T>*/(/*=T*/ e) => null;
1516 var topG = topF; 1517 var topG = topF;
1517 void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) { 1518 void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) {
1518 var c = new C<int>(); 1519 var c = new C<int>();
(...skipping 12 matching lines...) Expand all
1531 expectIdentifierType('methodCall', "int"); 1532 expectIdentifierType('methodCall', "int");
1532 expectIdentifierType('staticCall', "int"); 1533 expectIdentifierType('staticCall', "int");
1533 expectIdentifierType('staticFieldCall', "int"); 1534 expectIdentifierType('staticFieldCall', "int");
1534 expectIdentifierType('topFunCall', "int"); 1535 expectIdentifierType('topFunCall', "int");
1535 expectIdentifierType('topFieldCall', "int"); 1536 expectIdentifierType('topFieldCall', "int");
1536 expectIdentifierType('localCall', "int"); 1537 expectIdentifierType('localCall', "int");
1537 expectIdentifierType('paramCall', "int"); 1538 expectIdentifierType('paramCall', "int");
1538 expectIdentifierType('lambdaCall', "int"); 1539 expectIdentifierType('lambdaCall', "int");
1539 } 1540 }
1540 1541
1541 void test_genericMethod_functionExpressionInvocation_inferred() { 1542 test_genericMethod_functionExpressionInvocation_inferred() async {
1542 resolveTestUnit(r''' 1543 await resolveTestUnit(r'''
1543 class C<E> { 1544 class C<E> {
1544 /*=T*/ f/*<T>*/(/*=T*/ e) => null; 1545 /*=T*/ f/*<T>*/(/*=T*/ e) => null;
1545 static /*=T*/ g/*<T>*/(/*=T*/ e) => null; 1546 static /*=T*/ g/*<T>*/(/*=T*/ e) => null;
1546 static final h = g; 1547 static final h = g;
1547 } 1548 }
1548 1549
1549 /*=T*/ topF/*<T>*/(/*=T*/ e) => null; 1550 /*=T*/ topF/*<T>*/(/*=T*/ e) => null;
1550 var topG = topF; 1551 var topG = topF;
1551 void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) { 1552 void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) {
1552 var c = new C<int>(); 1553 var c = new C<int>();
(...skipping 12 matching lines...) Expand all
1565 expectIdentifierType('methodCall', "int"); 1566 expectIdentifierType('methodCall', "int");
1566 expectIdentifierType('staticCall', "int"); 1567 expectIdentifierType('staticCall', "int");
1567 expectIdentifierType('staticFieldCall', "int"); 1568 expectIdentifierType('staticFieldCall', "int");
1568 expectIdentifierType('topFunCall', "int"); 1569 expectIdentifierType('topFunCall', "int");
1569 expectIdentifierType('topFieldCall', "int"); 1570 expectIdentifierType('topFieldCall', "int");
1570 expectIdentifierType('localCall', "int"); 1571 expectIdentifierType('localCall', "int");
1571 expectIdentifierType('paramCall', "int"); 1572 expectIdentifierType('paramCall', "int");
1572 expectIdentifierType('lambdaCall', "int"); 1573 expectIdentifierType('lambdaCall', "int");
1573 } 1574 }
1574 1575
1575 void test_genericMethod_functionInvocation_explicit() { 1576 test_genericMethod_functionInvocation_explicit() async {
1576 resolveTestUnit(r''' 1577 await resolveTestUnit(r'''
1577 class C<E> { 1578 class C<E> {
1578 /*=T*/ f/*<T>*/(/*=T*/ e) => null; 1579 /*=T*/ f/*<T>*/(/*=T*/ e) => null;
1579 static /*=T*/ g/*<T>*/(/*=T*/ e) => null; 1580 static /*=T*/ g/*<T>*/(/*=T*/ e) => null;
1580 static final h = g; 1581 static final h = g;
1581 } 1582 }
1582 1583
1583 /*=T*/ topF/*<T>*/(/*=T*/ e) => null; 1584 /*=T*/ topF/*<T>*/(/*=T*/ e) => null;
1584 var topG = topF; 1585 var topG = topF;
1585 void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) { 1586 void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) {
1586 var c = new C<int>(); 1587 var c = new C<int>();
1587 /*=T*/ lf/*<T>*/(/*=T*/ e) => null; 1588 /*=T*/ lf/*<T>*/(/*=T*/ e) => null;
1588 var methodCall = c.f/*<int>*/(3); 1589 var methodCall = c.f/*<int>*/(3);
1589 var staticCall = C.g/*<int>*/(3); 1590 var staticCall = C.g/*<int>*/(3);
1590 var staticFieldCall = C.h/*<int>*/(3); 1591 var staticFieldCall = C.h/*<int>*/(3);
1591 var topFunCall = topF/*<int>*/(3); 1592 var topFunCall = topF/*<int>*/(3);
1592 var topFieldCall = topG/*<int>*/(3); 1593 var topFieldCall = topG/*<int>*/(3);
1593 var localCall = lf/*<int>*/(3); 1594 var localCall = lf/*<int>*/(3);
1594 var paramCall = pf/*<int>*/(3); 1595 var paramCall = pf/*<int>*/(3);
1595 } 1596 }
1596 '''); 1597 ''');
1597 expectIdentifierType('methodCall', "int"); 1598 expectIdentifierType('methodCall', "int");
1598 expectIdentifierType('staticCall', "int"); 1599 expectIdentifierType('staticCall', "int");
1599 expectIdentifierType('staticFieldCall', "int"); 1600 expectIdentifierType('staticFieldCall', "int");
1600 expectIdentifierType('topFunCall', "int"); 1601 expectIdentifierType('topFunCall', "int");
1601 expectIdentifierType('topFieldCall', "int"); 1602 expectIdentifierType('topFieldCall', "int");
1602 expectIdentifierType('localCall', "int"); 1603 expectIdentifierType('localCall', "int");
1603 expectIdentifierType('paramCall', "int"); 1604 expectIdentifierType('paramCall', "int");
1604 } 1605 }
1605 1606
1606 void test_genericMethod_functionInvocation_inferred() { 1607 test_genericMethod_functionInvocation_inferred() async {
1607 resolveTestUnit(r''' 1608 await resolveTestUnit(r'''
1608 class C<E> { 1609 class C<E> {
1609 /*=T*/ f/*<T>*/(/*=T*/ e) => null; 1610 /*=T*/ f/*<T>*/(/*=T*/ e) => null;
1610 static /*=T*/ g/*<T>*/(/*=T*/ e) => null; 1611 static /*=T*/ g/*<T>*/(/*=T*/ e) => null;
1611 static final h = g; 1612 static final h = g;
1612 } 1613 }
1613 1614
1614 /*=T*/ topF/*<T>*/(/*=T*/ e) => null; 1615 /*=T*/ topF/*<T>*/(/*=T*/ e) => null;
1615 var topG = topF; 1616 var topG = topF;
1616 void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) { 1617 void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) {
1617 var c = new C<int>(); 1618 var c = new C<int>();
1618 /*=T*/ lf/*<T>*/(/*=T*/ e) => null; 1619 /*=T*/ lf/*<T>*/(/*=T*/ e) => null;
1619 var methodCall = c.f(3); 1620 var methodCall = c.f(3);
1620 var staticCall = C.g(3); 1621 var staticCall = C.g(3);
1621 var staticFieldCall = C.h(3); 1622 var staticFieldCall = C.h(3);
1622 var topFunCall = topF(3); 1623 var topFunCall = topF(3);
1623 var topFieldCall = topG(3); 1624 var topFieldCall = topG(3);
1624 var localCall = lf(3); 1625 var localCall = lf(3);
1625 var paramCall = pf(3); 1626 var paramCall = pf(3);
1626 } 1627 }
1627 '''); 1628 ''');
1628 expectIdentifierType('methodCall', "int"); 1629 expectIdentifierType('methodCall', "int");
1629 expectIdentifierType('staticCall', "int"); 1630 expectIdentifierType('staticCall', "int");
1630 expectIdentifierType('staticFieldCall', "int"); 1631 expectIdentifierType('staticFieldCall', "int");
1631 expectIdentifierType('topFunCall', "int"); 1632 expectIdentifierType('topFunCall', "int");
1632 expectIdentifierType('topFieldCall', "int"); 1633 expectIdentifierType('topFieldCall', "int");
1633 expectIdentifierType('localCall', "int"); 1634 expectIdentifierType('localCall', "int");
1634 expectIdentifierType('paramCall', "int"); 1635 expectIdentifierType('paramCall', "int");
1635 } 1636 }
1636 1637
1637 void test_genericMethod_functionTypedParameter() { 1638 test_genericMethod_functionTypedParameter() async {
1638 resolveTestUnit(r''' 1639 await resolveTestUnit(r'''
1639 class C<E> { 1640 class C<E> {
1640 List/*<T>*/ f/*<T>*/(/*=T*/ f(E e)) => null; 1641 List/*<T>*/ f/*<T>*/(/*=T*/ f(E e)) => null;
1641 } 1642 }
1642 main() { 1643 main() {
1643 C<String> cOfString; 1644 C<String> cOfString;
1644 } 1645 }
1645 '''); 1646 ''');
1646 expectFunctionType('f', '<T>((E) → T) → List<T>', 1647 expectFunctionType('f', '<T>((E) → T) → List<T>',
1647 elementTypeParams: '[T]', 1648 elementTypeParams: '[T]',
1648 typeParams: '[E]', 1649 typeParams: '[E]',
1649 typeArgs: '[E]', 1650 typeArgs: '[E]',
1650 typeFormals: '[T]'); 1651 typeFormals: '[T]');
1651 1652
1652 SimpleIdentifier c = findIdentifier('cOfString'); 1653 SimpleIdentifier c = findIdentifier('cOfString');
1653 FunctionType ft = (c.staticType as InterfaceType).getMethod('f').type; 1654 FunctionType ft = (c.staticType as InterfaceType).getMethod('f').type;
1654 expect(ft.toString(), '<T>((String) → T) → List<T>'); 1655 expect(ft.toString(), '<T>((String) → T) → List<T>');
1655 ft = ft.instantiate([typeProvider.intType]); 1656 ft = ft.instantiate([typeProvider.intType]);
1656 expect(ft.toString(), '((String) → int) → List<int>'); 1657 expect(ft.toString(), '((String) → int) → List<int>');
1657 } 1658 }
1658 1659
1659 void test_genericMethod_implicitDynamic() { 1660 test_genericMethod_implicitDynamic() async {
1660 // Regression test for: 1661 // Regression test for:
1661 // https://github.com/dart-lang/sdk/issues/25100#issuecomment-162047588 1662 // https://github.com/dart-lang/sdk/issues/25100#issuecomment-162047588
1662 // These should not cause any hints or warnings. 1663 // These should not cause any hints or warnings.
1663 resolveTestUnit(r''' 1664 await resolveTestUnit(r'''
1664 class List<E> { 1665 class List<E> {
1665 /*=T*/ map/*<T>*/(/*=T*/ f(E e)) => null; 1666 /*=T*/ map/*<T>*/(/*=T*/ f(E e)) => null;
1666 } 1667 }
1667 void foo() { 1668 void foo() {
1668 List list = null; 1669 List list = null;
1669 list.map((e) => e); 1670 list.map((e) => e);
1670 list.map((e) => 3); 1671 list.map((e) => 3);
1671 }'''); 1672 }''');
1672 expectIdentifierType('map((e) => e);', '<T>((dynamic) → T) → T', isNull); 1673 expectIdentifierType('map((e) => e);', '<T>((dynamic) → T) → T', isNull);
1673 expectIdentifierType('map((e) => 3);', '<T>((dynamic) → T) → T', isNull); 1674 expectIdentifierType('map((e) => 3);', '<T>((dynamic) → T) → T', isNull);
1674 1675
1675 MethodInvocation m1 = findIdentifier('map((e) => e);').parent; 1676 MethodInvocation m1 = findIdentifier('map((e) => e);').parent;
1676 expect(m1.staticInvokeType.toString(), '((dynamic) → dynamic) → dynamic'); 1677 expect(m1.staticInvokeType.toString(), '((dynamic) → dynamic) → dynamic');
1677 MethodInvocation m2 = findIdentifier('map((e) => 3);').parent; 1678 MethodInvocation m2 = findIdentifier('map((e) => 3);').parent;
1678 expect(m2.staticInvokeType.toString(), '((dynamic) → int) → int'); 1679 expect(m2.staticInvokeType.toString(), '((dynamic) → int) → int');
1679 } 1680 }
1680 1681
1681 void test_genericMethod_max_doubleDouble() { 1682 test_genericMethod_max_doubleDouble() async {
1682 String code = r''' 1683 String code = r'''
1683 import 'dart:math'; 1684 import 'dart:math';
1684 main() { 1685 main() {
1685 var foo = max(1.0, 2.0); 1686 var foo = max(1.0, 2.0);
1686 } 1687 }
1687 '''; 1688 ''';
1688 resolveTestUnit(code); 1689 await resolveTestUnit(code);
1689 expectInitializerType('foo', 'double', isNull); 1690 expectInitializerType('foo', 'double', isNull);
1690 } 1691 }
1691 1692
1692 void test_genericMethod_max_doubleDouble_prefixed() { 1693 test_genericMethod_max_doubleDouble_prefixed() async {
1693 String code = r''' 1694 String code = r'''
1694 import 'dart:math' as math; 1695 import 'dart:math' as math;
1695 main() { 1696 main() {
1696 var foo = math.max(1.0, 2.0); 1697 var foo = math.max(1.0, 2.0);
1697 } 1698 }
1698 '''; 1699 ''';
1699 resolveTestUnit(code); 1700 await resolveTestUnit(code);
1700 expectInitializerType('foo', 'double', isNull); 1701 expectInitializerType('foo', 'double', isNull);
1701 } 1702 }
1702 1703
1703 void test_genericMethod_max_doubleInt() { 1704 test_genericMethod_max_doubleInt() async {
1704 String code = r''' 1705 String code = r'''
1705 import 'dart:math'; 1706 import 'dart:math';
1706 main() { 1707 main() {
1707 var foo = max(1.0, 2); 1708 var foo = max(1.0, 2);
1708 } 1709 }
1709 '''; 1710 ''';
1710 resolveTestUnit(code); 1711 await resolveTestUnit(code);
1711 expectInitializerType('foo', 'num', isNull); 1712 expectInitializerType('foo', 'num', isNull);
1712 } 1713 }
1713 1714
1714 void test_genericMethod_max_intDouble() { 1715 test_genericMethod_max_intDouble() async {
1715 String code = r''' 1716 String code = r'''
1716 import 'dart:math'; 1717 import 'dart:math';
1717 main() { 1718 main() {
1718 var foo = max(1, 2.0); 1719 var foo = max(1, 2.0);
1719 } 1720 }
1720 '''; 1721 ''';
1721 resolveTestUnit(code); 1722 await resolveTestUnit(code);
1722 expectInitializerType('foo', 'num', isNull); 1723 expectInitializerType('foo', 'num', isNull);
1723 } 1724 }
1724 1725
1725 void test_genericMethod_max_intInt() { 1726 test_genericMethod_max_intInt() async {
1726 String code = r''' 1727 String code = r'''
1727 import 'dart:math'; 1728 import 'dart:math';
1728 main() { 1729 main() {
1729 var foo = max(1, 2); 1730 var foo = max(1, 2);
1730 } 1731 }
1731 '''; 1732 ''';
1732 resolveTestUnit(code); 1733 await resolveTestUnit(code);
1733 expectInitializerType('foo', 'int', isNull); 1734 expectInitializerType('foo', 'int', isNull);
1734 } 1735 }
1735 1736
1736 void test_genericMethod_nestedBound() { 1737 test_genericMethod_nestedBound() async {
1737 String code = r''' 1738 String code = r'''
1738 class Foo<T extends num> { 1739 class Foo<T extends num> {
1739 void method/*<U extends T>*/(dynamic/*=U*/ u) { 1740 void method/*<U extends T>*/(dynamic/*=U*/ u) {
1740 u.abs(); 1741 u.abs();
1741 } 1742 }
1742 } 1743 }
1743 '''; 1744 ''';
1744 // Just validate that there is no warning on the call to `.abs()`. 1745 // Just validate that there is no warning on the call to `.abs()`.
1745 resolveTestUnit(code); 1746 await resolveTestUnit(code);
1746 } 1747 }
1747 1748
1748 void test_genericMethod_nestedCapture() { 1749 test_genericMethod_nestedCapture() async {
1749 resolveTestUnit(r''' 1750 await resolveTestUnit(r'''
1750 class C<T> { 1751 class C<T> {
1751 /*=T*/ f/*<S>*/(/*=S*/ x) { 1752 /*=T*/ f/*<S>*/(/*=S*/ x) {
1752 new C<S>().f/*<int>*/(3); 1753 new C<S>().f/*<int>*/(3);
1753 new C<S>().f; // tear-off 1754 new C<S>().f; // tear-off
1754 return null; 1755 return null;
1755 } 1756 }
1756 } 1757 }
1757 '''); 1758 ''');
1758 MethodInvocation f = findIdentifier('f/*<int>*/(3);').parent; 1759 MethodInvocation f = findIdentifier('f/*<int>*/(3);').parent;
1759 expect(f.staticInvokeType.toString(), '(int) → S'); 1760 expect(f.staticInvokeType.toString(), '(int) → S');
1760 FunctionType ft = f.staticInvokeType; 1761 FunctionType ft = f.staticInvokeType;
1761 expect('${ft.typeArguments}/${ft.typeParameters}', '[S, int]/[T, S]'); 1762 expect('${ft.typeArguments}/${ft.typeParameters}', '[S, int]/[T, S]');
1762 1763
1763 expectIdentifierType('f;', '<S₀>(S₀) → S'); 1764 expectIdentifierType('f;', '<S₀>(S₀) → S');
1764 } 1765 }
1765 1766
1766 void test_genericMethod_nestedFunctions() { 1767 test_genericMethod_nestedFunctions() async {
1767 resolveTestUnit(r''' 1768 await resolveTestUnit(r'''
1768 /*=S*/ f/*<S>*/(/*=S*/ x) { 1769 /*=S*/ f/*<S>*/(/*=S*/ x) {
1769 g/*<S>*/(/*=S*/ x) => f; 1770 g/*<S>*/(/*=S*/ x) => f;
1770 return null; 1771 return null;
1771 } 1772 }
1772 '''); 1773 ''');
1773 expectIdentifierType('f', '<S>(S) → S'); 1774 expectIdentifierType('f', '<S>(S) → S');
1774 expectIdentifierType('g', '<S>(S) → <S>(S) → S'); 1775 expectIdentifierType('g', '<S>(S) → <S>(S) → S');
1775 } 1776 }
1776 1777
1777 void test_genericMethod_override() { 1778 test_genericMethod_override() async {
1778 resolveTestUnit(r''' 1779 await resolveTestUnit(r'''
1779 class C { 1780 class C {
1780 /*=T*/ f/*<T>*/(/*=T*/ x) => null; 1781 /*=T*/ f/*<T>*/(/*=T*/ x) => null;
1781 } 1782 }
1782 class D extends C { 1783 class D extends C {
1783 /*=T*/ f/*<T>*/(/*=T*/ x) => null; // from D 1784 /*=T*/ f/*<T>*/(/*=T*/ x) => null; // from D
1784 } 1785 }
1785 '''); 1786 ''');
1786 expectFunctionType('f/*<T>*/(/*=T*/ x) => null; // from D', '<T>(T) → T', 1787 expectFunctionType('f/*<T>*/(/*=T*/ x) => null; // from D', '<T>(T) → T',
1787 elementTypeParams: '[T]', typeFormals: '[T]'); 1788 elementTypeParams: '[T]', typeFormals: '[T]');
1788 SimpleIdentifier f = 1789 SimpleIdentifier f =
1789 findIdentifier('f/*<T>*/(/*=T*/ x) => null; // from D'); 1790 findIdentifier('f/*<T>*/(/*=T*/ x) => null; // from D');
1790 MethodElementImpl e = f.staticElement; 1791 MethodElementImpl e = f.staticElement;
1791 FunctionType ft = e.type.instantiate([typeProvider.stringType]); 1792 FunctionType ft = e.type.instantiate([typeProvider.stringType]);
1792 expect(ft.toString(), '(String) → String'); 1793 expect(ft.toString(), '(String) → String');
1793 } 1794 }
1794 1795
1795 void test_genericMethod_override_bounds() { 1796 test_genericMethod_override_bounds() async {
1796 resolveTestUnit(r''' 1797 await resolveTestUnit(r'''
1797 class A {} 1798 class A {}
1798 class B extends A {} 1799 class B extends A {}
1799 class C { 1800 class C {
1800 /*=T*/ f/*<T extends B>*/(/*=T*/ x) => null; 1801 /*=T*/ f/*<T extends B>*/(/*=T*/ x) => null;
1801 } 1802 }
1802 class D extends C { 1803 class D extends C {
1803 /*=T*/ f/*<T extends A>*/(/*=T*/ x) => null; 1804 /*=T*/ f/*<T extends A>*/(/*=T*/ x) => null;
1804 } 1805 }
1805 '''); 1806 ''');
1806 } 1807 }
1807 1808
1808 void test_genericMethod_override_invalidReturnType() { 1809 test_genericMethod_override_invalidReturnType() async {
1809 Source source = addSource(r''' 1810 Source source = addSource(r'''
1810 class C { 1811 class C {
1811 Iterable/*<T>*/ f/*<T>*/(/*=T*/ x) => null; 1812 Iterable/*<T>*/ f/*<T>*/(/*=T*/ x) => null;
1812 } 1813 }
1813 class D extends C { 1814 class D extends C {
1814 String f/*<S>*/(/*=S*/ x) => null; 1815 String f/*<S>*/(/*=S*/ x) => null;
1815 }'''); 1816 }''');
1816 assertErrors(source, [StrongModeCode.INVALID_METHOD_OVERRIDE]); 1817 await assertErrors(source, [StrongModeCode.INVALID_METHOD_OVERRIDE]);
1817 verify([source]); 1818 verify([source]);
1818 } 1819 }
1819 1820
1820 void test_genericMethod_override_invalidTypeParamBounds() { 1821 test_genericMethod_override_invalidTypeParamBounds() async {
1821 Source source = addSource(r''' 1822 Source source = addSource(r'''
1822 class A {} 1823 class A {}
1823 class B extends A {} 1824 class B extends A {}
1824 class C { 1825 class C {
1825 /*=T*/ f/*<T extends A>*/(/*=T*/ x) => null; 1826 /*=T*/ f/*<T extends A>*/(/*=T*/ x) => null;
1826 } 1827 }
1827 class D extends C { 1828 class D extends C {
1828 /*=T*/ f/*<T extends B>*/(/*=T*/ x) => null; 1829 /*=T*/ f/*<T extends B>*/(/*=T*/ x) => null;
1829 }'''); 1830 }''');
1830 assertErrors(source, [StrongModeCode.INVALID_METHOD_OVERRIDE]); 1831 await assertErrors(source, [StrongModeCode.INVALID_METHOD_OVERRIDE]);
1831 verify([source]); 1832 verify([source]);
1832 } 1833 }
1833 1834
1834 void test_genericMethod_override_invalidTypeParamCount() { 1835 test_genericMethod_override_invalidTypeParamCount() async {
1835 Source source = addSource(r''' 1836 Source source = addSource(r'''
1836 class C { 1837 class C {
1837 /*=T*/ f/*<T>*/(/*=T*/ x) => null; 1838 /*=T*/ f/*<T>*/(/*=T*/ x) => null;
1838 } 1839 }
1839 class D extends C { 1840 class D extends C {
1840 /*=S*/ f/*<T, S>*/(/*=T*/ x) => null; 1841 /*=S*/ f/*<T, S>*/(/*=T*/ x) => null;
1841 }'''); 1842 }''');
1842 assertErrors(source, [StrongModeCode.INVALID_METHOD_OVERRIDE]); 1843 await assertErrors(source, [StrongModeCode.INVALID_METHOD_OVERRIDE]);
1843 verify([source]); 1844 verify([source]);
1844 } 1845 }
1845 1846
1846 void test_genericMethod_propagatedType_promotion() { 1847 test_genericMethod_propagatedType_promotion() async {
1847 // Regression test for: 1848 // Regression test for:
1848 // https://github.com/dart-lang/sdk/issues/25340 1849 // https://github.com/dart-lang/sdk/issues/25340
1849 1850
1850 // Note, after https://github.com/dart-lang/sdk/issues/25486 the original 1851 // Note, after https://github.com/dart-lang/sdk/issues/25486 the original
1851 // example won't work, as we now compute a static type and therefore discard 1852 // example won't work, as we now compute a static type and therefore discard
1852 // the propagated type. So a new test was created that doesn't run under 1853 // the propagated type. So a new test was created that doesn't run under
1853 // strong mode. 1854 // strong mode.
1854 resolveTestUnit(r''' 1855 await resolveTestUnit(r'''
1855 abstract class Iter { 1856 abstract class Iter {
1856 List/*<S>*/ map/*<S>*/(/*=S*/ f(x)); 1857 List/*<S>*/ map/*<S>*/(/*=S*/ f(x));
1857 } 1858 }
1858 class C {} 1859 class C {}
1859 C toSpan(dynamic element) { 1860 C toSpan(dynamic element) {
1860 if (element is Iter) { 1861 if (element is Iter) {
1861 var y = element.map(toSpan); 1862 var y = element.map(toSpan);
1862 } 1863 }
1863 return null; 1864 return null;
1864 }'''); 1865 }''');
1865 expectIdentifierType('y = ', 'List<C>', isNull); 1866 expectIdentifierType('y = ', 'List<C>', isNull);
1866 } 1867 }
1867 1868
1868 void test_genericMethod_tearoff() { 1869 test_genericMethod_tearoff() async {
1869 resolveTestUnit(r''' 1870 await resolveTestUnit(r'''
1870 class C<E> { 1871 class C<E> {
1871 /*=T*/ f/*<T>*/(E e) => null; 1872 /*=T*/ f/*<T>*/(E e) => null;
1872 static /*=T*/ g/*<T>*/(/*=T*/ e) => null; 1873 static /*=T*/ g/*<T>*/(/*=T*/ e) => null;
1873 static final h = g; 1874 static final h = g;
1874 } 1875 }
1875 1876
1876 /*=T*/ topF/*<T>*/(/*=T*/ e) => null; 1877 /*=T*/ topF/*<T>*/(/*=T*/ e) => null;
1877 var topG = topF; 1878 var topG = topF;
1878 void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) { 1879 void test/*<S>*/(/*=T*/ pf/*<T>*/(/*=T*/ e)) {
1879 var c = new C<int>(); 1880 var c = new C<int>();
1880 /*=T*/ lf/*<T>*/(/*=T*/ e) => null; 1881 /*=T*/ lf/*<T>*/(/*=T*/ e) => null;
1881 var methodTearOff = c.f; 1882 var methodTearOff = c.f;
1882 var staticTearOff = C.g; 1883 var staticTearOff = C.g;
1883 var staticFieldTearOff = C.h; 1884 var staticFieldTearOff = C.h;
1884 var topFunTearOff = topF; 1885 var topFunTearOff = topF;
1885 var topFieldTearOff = topG; 1886 var topFieldTearOff = topG;
1886 var localTearOff = lf; 1887 var localTearOff = lf;
1887 var paramTearOff = pf; 1888 var paramTearOff = pf;
1888 } 1889 }
1889 '''); 1890 ''');
1890 expectIdentifierType('methodTearOff', "<T>(int) → T"); 1891 expectIdentifierType('methodTearOff', "<T>(int) → T");
1891 expectIdentifierType('staticTearOff', "<T>(T) → T"); 1892 expectIdentifierType('staticTearOff', "<T>(T) → T");
1892 expectIdentifierType('staticFieldTearOff', "<T>(T) → T"); 1893 expectIdentifierType('staticFieldTearOff', "<T>(T) → T");
1893 expectIdentifierType('topFunTearOff', "<T>(T) → T"); 1894 expectIdentifierType('topFunTearOff', "<T>(T) → T");
1894 expectIdentifierType('topFieldTearOff', "<T>(T) → T"); 1895 expectIdentifierType('topFieldTearOff', "<T>(T) → T");
1895 expectIdentifierType('localTearOff', "<T>(T) → T"); 1896 expectIdentifierType('localTearOff', "<T>(T) → T");
1896 expectIdentifierType('paramTearOff', "<T>(T) → T"); 1897 expectIdentifierType('paramTearOff', "<T>(T) → T");
1897 } 1898 }
1898 1899
1899 void test_genericMethod_then() { 1900 test_genericMethod_then() async {
1900 String code = r''' 1901 String code = r'''
1901 import 'dart:async'; 1902 import 'dart:async';
1902 String toString(int x) => x.toString(); 1903 String toString(int x) => x.toString();
1903 main() { 1904 main() {
1904 Future<int> bar = null; 1905 Future<int> bar = null;
1905 var foo = bar.then(toString); 1906 var foo = bar.then(toString);
1906 } 1907 }
1907 '''; 1908 ''';
1908 resolveTestUnit(code); 1909 await resolveTestUnit(code);
1909 expectInitializerType('foo', 'Future<String>', isNull); 1910 expectInitializerType('foo', 'Future<String>', isNull);
1910 } 1911 }
1911 1912
1912 void test_genericMethod_then_prefixed() { 1913 test_genericMethod_then_prefixed() async {
1913 String code = r''' 1914 String code = r'''
1914 import 'dart:async' as async; 1915 import 'dart:async' as async;
1915 String toString(int x) => x.toString(); 1916 String toString(int x) => x.toString();
1916 main() { 1917 main() {
1917 async.Future<int> bar = null; 1918 async.Future<int> bar = null;
1918 var foo = bar.then(toString); 1919 var foo = bar.then(toString);
1919 } 1920 }
1920 '''; 1921 ''';
1921 resolveTestUnit(code); 1922 await resolveTestUnit(code);
1922 expectInitializerType('foo', 'Future<String>', isNull); 1923 expectInitializerType('foo', 'Future<String>', isNull);
1923 } 1924 }
1924 1925
1925 void test_genericMethod_then_propagatedType() { 1926 test_genericMethod_then_propagatedType() async {
1926 // Regression test for https://github.com/dart-lang/sdk/issues/25482. 1927 // Regression test for https://github.com/dart-lang/sdk/issues/25482.
1927 String code = r''' 1928 String code = r'''
1928 import 'dart:async'; 1929 import 'dart:async';
1929 void main() { 1930 void main() {
1930 Future<String> p; 1931 Future<String> p;
1931 var foo = p.then((r) => new Future<String>.value(3)); 1932 var foo = p.then((r) => new Future<String>.value(3));
1932 } 1933 }
1933 '''; 1934 ''';
1934 // This should produce no hints or warnings. 1935 // This should produce no hints or warnings.
1935 resolveTestUnit(code); 1936 await resolveTestUnit(code);
1936 expectInitializerType('foo', 'Future<String>', isNull); 1937 expectInitializerType('foo', 'Future<String>', isNull);
1937 } 1938 }
1938 1939
1939 void test_implicitBounds() { 1940 test_implicitBounds() async {
1940 String code = r''' 1941 String code = r'''
1941 class A<T> {} 1942 class A<T> {}
1942 1943
1943 class B<T extends num> {} 1944 class B<T extends num> {}
1944 1945
1945 class C<S extends int, T extends B<S>, U extends B> {} 1946 class C<S extends int, T extends B<S>, U extends B> {}
1946 1947
1947 void test() { 1948 void test() {
1948 // 1949 //
1949 A ai; 1950 A ai;
1950 B bi; 1951 B bi;
1951 C ci; 1952 C ci;
1952 var aa = new A(); 1953 var aa = new A();
1953 var bb = new B(); 1954 var bb = new B();
1954 var cc = new C(); 1955 var cc = new C();
1955 } 1956 }
1956 '''; 1957 ''';
1957 resolveTestUnit(code); 1958 await resolveTestUnit(code);
1958 expectIdentifierType('ai', "A<dynamic>"); 1959 expectIdentifierType('ai', "A<dynamic>");
1959 expectIdentifierType('bi', "B<num>"); 1960 expectIdentifierType('bi', "B<num>");
1960 expectIdentifierType('ci', "C<int, B<int>, B<dynamic>>"); 1961 expectIdentifierType('ci', "C<int, B<int>, B<dynamic>>");
1961 expectIdentifierType('aa', "A<dynamic>"); 1962 expectIdentifierType('aa', "A<dynamic>");
1962 expectIdentifierType('bb', "B<num>"); 1963 expectIdentifierType('bb', "B<num>");
1963 expectIdentifierType('cc', "C<int, B<int>, B<dynamic>>"); 1964 expectIdentifierType('cc', "C<int, B<int>, B<dynamic>>");
1964 } 1965 }
1965 1966
1966 void test_objectMethodOnFunctions_Anonymous() { 1967 test_objectMethodOnFunctions_Anonymous() async {
1967 String code = r''' 1968 String code = r'''
1968 void main() { 1969 void main() {
1969 var f = (x) => 3; 1970 var f = (x) => 3;
1970 // No errors, correct type 1971 // No errors, correct type
1971 var t0 = f.toString(); 1972 var t0 = f.toString();
1972 var t1 = f.toString; 1973 var t1 = f.toString;
1973 var t2 = f.hashCode; 1974 var t2 = f.hashCode;
1974 1975
1975 // Expressions, no errors, correct type 1976 // Expressions, no errors, correct type
1976 var t3 = (f).toString(); 1977 var t3 = (f).toString();
1977 var t4 = (f).toString; 1978 var t4 = (f).toString;
1978 var t5 = (f).hashCode; 1979 var t5 = (f).hashCode;
1979 1980
1980 // Cascades, no errors 1981 // Cascades, no errors
1981 f..toString(); 1982 f..toString();
1982 f..toString; 1983 f..toString;
1983 f..hashCode; 1984 f..hashCode;
1984 1985
1985 // Expression cascades, no errors 1986 // Expression cascades, no errors
1986 (f)..toString(); 1987 (f)..toString();
1987 (f)..toString; 1988 (f)..toString;
1988 (f)..hashCode; 1989 (f)..hashCode;
1989 }'''; 1990 }''';
1990 objectMethodOnFunctions_helper(code); 1991 await _objectMethodOnFunctions_helper2(code);
1991 } 1992 }
1992 1993
1993 void test_objectMethodOnFunctions_Function() { 1994 test_objectMethodOnFunctions_Function() async {
1994 String code = r''' 1995 String code = r'''
1995 void main() { 1996 void main() {
1996 Function f; 1997 Function f;
1997 // No errors, correct type 1998 // No errors, correct type
1998 var t0 = f.toString(); 1999 var t0 = f.toString();
1999 var t1 = f.toString; 2000 var t1 = f.toString;
2000 var t2 = f.hashCode; 2001 var t2 = f.hashCode;
2001 2002
2002 // Expressions, no errors, correct type 2003 // Expressions, no errors, correct type
2003 var t3 = (f).toString(); 2004 var t3 = (f).toString();
2004 var t4 = (f).toString; 2005 var t4 = (f).toString;
2005 var t5 = (f).hashCode; 2006 var t5 = (f).hashCode;
2006 2007
2007 // Cascades, no errors 2008 // Cascades, no errors
2008 f..toString(); 2009 f..toString();
2009 f..toString; 2010 f..toString;
2010 f..hashCode; 2011 f..hashCode;
2011 2012
2012 // Expression cascades, no errors 2013 // Expression cascades, no errors
2013 (f)..toString(); 2014 (f)..toString();
2014 (f)..toString; 2015 (f)..toString;
2015 (f)..hashCode; 2016 (f)..hashCode;
2016 }'''; 2017 }''';
2017 objectMethodOnFunctions_helper(code); 2018 await _objectMethodOnFunctions_helper2(code);
2018 } 2019 }
2019 2020
2020 void test_objectMethodOnFunctions_Static() { 2021 test_objectMethodOnFunctions_Static() async {
2021 String code = r''' 2022 String code = r'''
2022 int f(int x) => null; 2023 int f(int x) => null;
2023 void main() { 2024 void main() {
2024 // No errors, correct type 2025 // No errors, correct type
2025 var t0 = f.toString(); 2026 var t0 = f.toString();
2026 var t1 = f.toString; 2027 var t1 = f.toString;
2027 var t2 = f.hashCode; 2028 var t2 = f.hashCode;
2028 2029
2029 // Expressions, no errors, correct type 2030 // Expressions, no errors, correct type
2030 var t3 = (f).toString(); 2031 var t3 = (f).toString();
2031 var t4 = (f).toString; 2032 var t4 = (f).toString;
2032 var t5 = (f).hashCode; 2033 var t5 = (f).hashCode;
2033 2034
2034 // Cascades, no errors 2035 // Cascades, no errors
2035 f..toString(); 2036 f..toString();
2036 f..toString; 2037 f..toString;
2037 f..hashCode; 2038 f..hashCode;
2038 2039
2039 // Expression cascades, no errors 2040 // Expression cascades, no errors
2040 (f)..toString(); 2041 (f)..toString();
2041 (f)..toString; 2042 (f)..toString;
2042 (f)..hashCode; 2043 (f)..hashCode;
2043 }'''; 2044 }''';
2044 objectMethodOnFunctions_helper(code); 2045 await _objectMethodOnFunctions_helper2(code);
2045 } 2046 }
2046 2047
2047 void test_objectMethodOnFunctions_Typedef() { 2048 test_objectMethodOnFunctions_Typedef() async {
2048 String code = r''' 2049 String code = r'''
2049 typedef bool Predicate<T>(T object); 2050 typedef bool Predicate<T>(T object);
2050 2051
2051 void main() { 2052 void main() {
2052 Predicate<int> f; 2053 Predicate<int> f;
2053 // No errors, correct type 2054 // No errors, correct type
2054 var t0 = f.toString(); 2055 var t0 = f.toString();
2055 var t1 = f.toString; 2056 var t1 = f.toString;
2056 var t2 = f.hashCode; 2057 var t2 = f.hashCode;
2057 2058
2058 // Expressions, no errors, correct type 2059 // Expressions, no errors, correct type
2059 var t3 = (f).toString(); 2060 var t3 = (f).toString();
2060 var t4 = (f).toString; 2061 var t4 = (f).toString;
2061 var t5 = (f).hashCode; 2062 var t5 = (f).hashCode;
2062 2063
2063 // Cascades, no errors 2064 // Cascades, no errors
2064 f..toString(); 2065 f..toString();
2065 f..toString; 2066 f..toString;
2066 f..hashCode; 2067 f..hashCode;
2067 2068
2068 // Expression cascades, no errors 2069 // Expression cascades, no errors
2069 (f)..toString(); 2070 (f)..toString();
2070 (f)..toString; 2071 (f)..toString;
2071 (f)..hashCode; 2072 (f)..hashCode;
2072 }'''; 2073 }''';
2073 objectMethodOnFunctions_helper(code); 2074 await _objectMethodOnFunctions_helper2(code);
2074 } 2075 }
2075 2076
2076 void test_setterWithDynamicTypeIsError() { 2077 test_setterWithDynamicTypeIsError() async {
2077 Source source = addSource(r''' 2078 Source source = addSource(r'''
2078 class A { 2079 class A {
2079 dynamic set f(String s) => null; 2080 dynamic set f(String s) => null;
2080 } 2081 }
2081 dynamic set g(int x) => null; 2082 dynamic set g(int x) => null;
2082 '''); 2083 ''');
2083 assertErrors(source, [ 2084 await assertErrors(source, [
2084 StaticWarningCode.NON_VOID_RETURN_FOR_SETTER, 2085 StaticWarningCode.NON_VOID_RETURN_FOR_SETTER,
2085 StaticWarningCode.NON_VOID_RETURN_FOR_SETTER 2086 StaticWarningCode.NON_VOID_RETURN_FOR_SETTER
2086 ]); 2087 ]);
2087 verify([source]); 2088 verify([source]);
2088 } 2089 }
2089 2090
2090 void test_setterWithExplicitVoidType_returningVoid() { 2091 test_setterWithExplicitVoidType_returningVoid() async {
2091 Source source = addSource(r''' 2092 Source source = addSource(r'''
2092 void returnsVoid() {} 2093 void returnsVoid() {}
2093 class A { 2094 class A {
2094 void set f(String s) => returnsVoid(); 2095 void set f(String s) => returnsVoid();
2095 } 2096 }
2096 void set g(int x) => returnsVoid(); 2097 void set g(int x) => returnsVoid();
2097 '''); 2098 ''');
2098 assertNoErrors(source); 2099 await assertNoErrors(source);
2099 verify([source]); 2100 verify([source]);
2100 } 2101 }
2101 2102
2102 void test_setterWithNoVoidType() { 2103 test_setterWithNoVoidType() async {
2103 Source source = addSource(r''' 2104 Source source = addSource(r'''
2104 class A { 2105 class A {
2105 set f(String s) { 2106 set f(String s) {
2106 return '42'; 2107 return '42';
2107 } 2108 }
2108 } 2109 }
2109 set g(int x) => 42; 2110 set g(int x) => 42;
2110 '''); 2111 ''');
2111 assertErrors(source, [ 2112 await assertErrors(source, [
2112 StaticTypeWarningCode.RETURN_OF_INVALID_TYPE, 2113 StaticTypeWarningCode.RETURN_OF_INVALID_TYPE,
2113 ]); 2114 ]);
2114 verify([source]); 2115 verify([source]);
2115 } 2116 }
2116 2117
2117 void test_setterWithNoVoidType_returningVoid() { 2118 test_setterWithNoVoidType_returningVoid() async {
2118 Source source = addSource(r''' 2119 Source source = addSource(r'''
2119 void returnsVoid() {} 2120 void returnsVoid() {}
2120 class A { 2121 class A {
2121 set f(String s) => returnsVoid(); 2122 set f(String s) => returnsVoid();
2122 } 2123 }
2123 set g(int x) => returnsVoid(); 2124 set g(int x) => returnsVoid();
2124 '''); 2125 ''');
2125 assertNoErrors(source); 2126 await assertNoErrors(source);
2126 verify([source]); 2127 verify([source]);
2127 } 2128 }
2128 2129
2129 void test_setterWithOtherTypeIsError() { 2130 test_setterWithOtherTypeIsError() async {
2130 Source source = addSource(r''' 2131 Source source = addSource(r'''
2131 class A { 2132 class A {
2132 String set f(String s) => null; 2133 String set f(String s) => null;
2133 } 2134 }
2134 Object set g(x) => null; 2135 Object set g(x) => null;
2135 '''); 2136 ''');
2136 assertErrors(source, [ 2137 await assertErrors(source, [
2137 StaticWarningCode.NON_VOID_RETURN_FOR_SETTER, 2138 StaticWarningCode.NON_VOID_RETURN_FOR_SETTER,
2138 StaticWarningCode.NON_VOID_RETURN_FOR_SETTER 2139 StaticWarningCode.NON_VOID_RETURN_FOR_SETTER
2139 ]); 2140 ]);
2140 verify([source]); 2141 verify([source]);
2141 } 2142 }
2142 2143
2143 void test_ternaryOperator_null_left() { 2144 test_ternaryOperator_null_left() async {
2144 String code = r''' 2145 String code = r'''
2145 main() { 2146 main() {
2146 var foo = (true) ? null : 3; 2147 var foo = (true) ? null : 3;
2147 } 2148 }
2148 '''; 2149 ''';
2149 resolveTestUnit(code); 2150 await resolveTestUnit(code);
2150 expectInitializerType('foo', 'int', isNull); 2151 expectInitializerType('foo', 'int', isNull);
2151 } 2152 }
2152 2153
2153 void test_ternaryOperator_null_right() { 2154 test_ternaryOperator_null_right() async {
2154 String code = r''' 2155 String code = r'''
2155 main() { 2156 main() {
2156 var foo = (true) ? 3 : null; 2157 var foo = (true) ? 3 : null;
2157 } 2158 }
2158 '''; 2159 ''';
2159 resolveTestUnit(code); 2160 await resolveTestUnit(code);
2160 expectInitializerType('foo', 'int', isNull); 2161 expectInitializerType('foo', 'int', isNull);
2161 } 2162 }
2162 } 2163 }
2163 2164
2164 @reflectiveTest 2165 @reflectiveTest
2165 class StrongModeTypePropagationTest extends ResolverTestCase { 2166 class StrongModeTypePropagationTest extends ResolverTestCase {
2166 @override 2167 @override
2167 void setUp() { 2168 void setUp() {
2168 super.setUp(); 2169 super.setUp();
2169 AnalysisOptionsImpl options = new AnalysisOptionsImpl(); 2170 AnalysisOptionsImpl options = new AnalysisOptionsImpl();
2170 options.strongMode = true; 2171 options.strongMode = true;
2171 resetWithOptions(options); 2172 resetWithOptions(options);
2172 } 2173 }
2173 2174
2174 void test_foreachInference_dynamic_disabled() { 2175 test_foreachInference_dynamic_disabled() async {
2175 String code = r''' 2176 String code = r'''
2176 main() { 2177 main() {
2177 var list = <int>[]; 2178 var list = <int>[];
2178 for (dynamic v in list) { 2179 for (dynamic v in list) {
2179 v; // marker 2180 v; // marker
2180 } 2181 }
2181 }'''; 2182 }''';
2182 assertPropagatedIterationType(code, typeProvider.dynamicType, null); 2183 await assertPropagatedIterationType(code, typeProvider.dynamicType, null);
2183 assertTypeOfMarkedExpression(code, typeProvider.dynamicType, null); 2184 await assertTypeOfMarkedExpression(code, typeProvider.dynamicType, null);
2184 } 2185 }
2185 2186
2186 void test_foreachInference_reusedVar_disabled() { 2187 test_foreachInference_reusedVar_disabled() async {
2187 String code = r''' 2188 String code = r'''
2188 main() { 2189 main() {
2189 var list = <int>[]; 2190 var list = <int>[];
2190 var v; 2191 var v;
2191 for (v in list) { 2192 for (v in list) {
2192 v; // marker 2193 v; // marker
2193 } 2194 }
2194 }'''; 2195 }''';
2195 assertPropagatedIterationType(code, typeProvider.dynamicType, null); 2196 await assertPropagatedIterationType(code, typeProvider.dynamicType, null);
2196 assertTypeOfMarkedExpression(code, typeProvider.dynamicType, null); 2197 await assertTypeOfMarkedExpression(code, typeProvider.dynamicType, null);
2197 } 2198 }
2198 2199
2199 void test_foreachInference_var() { 2200 test_foreachInference_var() async {
2200 String code = r''' 2201 String code = r'''
2201 main() { 2202 main() {
2202 var list = <int>[]; 2203 var list = <int>[];
2203 for (var v in list) { 2204 for (var v in list) {
2204 v; // marker 2205 v; // marker
2205 } 2206 }
2206 }'''; 2207 }''';
2207 assertPropagatedIterationType(code, typeProvider.intType, null); 2208 await assertPropagatedIterationType(code, typeProvider.intType, null);
2208 assertTypeOfMarkedExpression(code, typeProvider.intType, null); 2209 await assertTypeOfMarkedExpression(code, typeProvider.intType, null);
2209 } 2210 }
2210 2211
2211 void test_foreachInference_var_iterable() { 2212 test_foreachInference_var_iterable() async {
2212 String code = r''' 2213 String code = r'''
2213 main() { 2214 main() {
2214 Iterable<int> list = <int>[]; 2215 Iterable<int> list = <int>[];
2215 for (var v in list) { 2216 for (var v in list) {
2216 v; // marker 2217 v; // marker
2217 } 2218 }
2218 }'''; 2219 }''';
2219 assertPropagatedIterationType(code, typeProvider.intType, null); 2220 await assertPropagatedIterationType(code, typeProvider.intType, null);
2220 assertTypeOfMarkedExpression(code, typeProvider.intType, null); 2221 await assertTypeOfMarkedExpression(code, typeProvider.intType, null);
2221 } 2222 }
2222 2223
2223 void test_foreachInference_var_stream() { 2224 test_foreachInference_var_stream() async {
2224 String code = r''' 2225 String code = r'''
2225 import 'dart:async'; 2226 import 'dart:async';
2226 main() async { 2227 main() async {
2227 Stream<int> stream = null; 2228 Stream<int> stream = null;
2228 await for (var v in stream) { 2229 await for (var v in stream) {
2229 v; // marker 2230 v; // marker
2230 } 2231 }
2231 }'''; 2232 }''';
2232 assertPropagatedIterationType(code, typeProvider.intType, null); 2233 await assertPropagatedIterationType(code, typeProvider.intType, null);
2233 assertTypeOfMarkedExpression(code, typeProvider.intType, null); 2234 await assertTypeOfMarkedExpression(code, typeProvider.intType, null);
2234 } 2235 }
2235 2236
2236 void test_localVariableInference_bottom_disabled() { 2237 test_localVariableInference_bottom_disabled() async {
2237 String code = r''' 2238 String code = r'''
2238 main() { 2239 main() {
2239 var v = null; 2240 var v = null;
2240 v; // marker 2241 v; // marker
2241 }'''; 2242 }''';
2242 assertPropagatedAssignedType(code, typeProvider.dynamicType, null); 2243 await assertPropagatedAssignedType(code, typeProvider.dynamicType, null);
2243 assertTypeOfMarkedExpression(code, typeProvider.dynamicType, null); 2244 await assertTypeOfMarkedExpression(code, typeProvider.dynamicType, null);
2244 } 2245 }
2245 2246
2246 void test_localVariableInference_constant() { 2247 test_localVariableInference_constant() async {
2247 String code = r''' 2248 String code = r'''
2248 main() { 2249 main() {
2249 var v = 3; 2250 var v = 3;
2250 v; // marker 2251 v; // marker
2251 }'''; 2252 }''';
2252 assertPropagatedAssignedType(code, typeProvider.intType, null); 2253 await assertPropagatedAssignedType(code, typeProvider.intType, null);
2253 assertTypeOfMarkedExpression(code, typeProvider.intType, null); 2254 await assertTypeOfMarkedExpression(code, typeProvider.intType, null);
2254 } 2255 }
2255 2256
2256 void test_localVariableInference_declaredType_disabled() { 2257 test_localVariableInference_declaredType_disabled() async {
2257 String code = r''' 2258 String code = r'''
2258 main() { 2259 main() {
2259 dynamic v = 3; 2260 dynamic v = 3;
2260 v; // marker 2261 v; // marker
2261 }'''; 2262 }''';
2262 assertPropagatedAssignedType(code, typeProvider.dynamicType, null); 2263 await assertPropagatedAssignedType(code, typeProvider.dynamicType, null);
2263 assertTypeOfMarkedExpression(code, typeProvider.dynamicType, null); 2264 await assertTypeOfMarkedExpression(code, typeProvider.dynamicType, null);
2264 } 2265 }
2265 2266
2266 void test_localVariableInference_noInitializer_disabled() { 2267 test_localVariableInference_noInitializer_disabled() async {
2267 String code = r''' 2268 String code = r'''
2268 main() { 2269 main() {
2269 var v; 2270 var v;
2270 v = 3; 2271 v = 3;
2271 v; // marker 2272 v; // marker
2272 }'''; 2273 }''';
2273 assertPropagatedAssignedType(code, typeProvider.dynamicType, null); 2274 await assertPropagatedAssignedType(code, typeProvider.dynamicType, null);
2274 assertTypeOfMarkedExpression(code, typeProvider.dynamicType, null); 2275 await assertTypeOfMarkedExpression(code, typeProvider.dynamicType, null);
2275 } 2276 }
2276 2277
2277 void test_localVariableInference_transitive_field_inferred_lexical() { 2278 test_localVariableInference_transitive_field_inferred_lexical() async {
2278 String code = r''' 2279 String code = r'''
2279 class A { 2280 class A {
2280 final x = 3; 2281 final x = 3;
2281 f() { 2282 f() {
2282 var v = x; 2283 var v = x;
2283 return v; // marker 2284 return v; // marker
2284 } 2285 }
2285 } 2286 }
2286 main() { 2287 main() {
2287 } 2288 }
2288 '''; 2289 ''';
2289 assertPropagatedAssignedType(code, typeProvider.intType, null); 2290 await assertPropagatedAssignedType(code, typeProvider.intType, null);
2290 assertTypeOfMarkedExpression(code, typeProvider.intType, null); 2291 await assertTypeOfMarkedExpression(code, typeProvider.intType, null);
2291 } 2292 }
2292 2293
2293 void test_localVariableInference_transitive_field_inferred_reversed() { 2294 test_localVariableInference_transitive_field_inferred_reversed() async {
2294 String code = r''' 2295 String code = r'''
2295 class A { 2296 class A {
2296 f() { 2297 f() {
2297 var v = x; 2298 var v = x;
2298 return v; // marker 2299 return v; // marker
2299 } 2300 }
2300 final x = 3; 2301 final x = 3;
2301 } 2302 }
2302 main() { 2303 main() {
2303 } 2304 }
2304 '''; 2305 ''';
2305 assertPropagatedAssignedType(code, typeProvider.intType, null); 2306 await assertPropagatedAssignedType(code, typeProvider.intType, null);
2306 assertTypeOfMarkedExpression(code, typeProvider.intType, null); 2307 await assertTypeOfMarkedExpression(code, typeProvider.intType, null);
2307 } 2308 }
2308 2309
2309 void test_localVariableInference_transitive_field_lexical() { 2310 test_localVariableInference_transitive_field_lexical() async {
2310 String code = r''' 2311 String code = r'''
2311 class A { 2312 class A {
2312 int x = 3; 2313 int x = 3;
2313 f() { 2314 f() {
2314 var v = x; 2315 var v = x;
2315 return v; // marker 2316 return v; // marker
2316 } 2317 }
2317 } 2318 }
2318 main() { 2319 main() {
2319 } 2320 }
2320 '''; 2321 ''';
2321 assertPropagatedAssignedType(code, typeProvider.intType, null); 2322 await assertPropagatedAssignedType(code, typeProvider.intType, null);
2322 assertTypeOfMarkedExpression(code, typeProvider.intType, null); 2323 await assertTypeOfMarkedExpression(code, typeProvider.intType, null);
2323 } 2324 }
2324 2325
2325 void test_localVariableInference_transitive_field_reversed() { 2326 test_localVariableInference_transitive_field_reversed() async {
2326 String code = r''' 2327 String code = r'''
2327 class A { 2328 class A {
2328 f() { 2329 f() {
2329 var v = x; 2330 var v = x;
2330 return v; // marker 2331 return v; // marker
2331 } 2332 }
2332 int x = 3; 2333 int x = 3;
2333 } 2334 }
2334 main() { 2335 main() {
2335 } 2336 }
2336 '''; 2337 ''';
2337 assertPropagatedAssignedType(code, typeProvider.intType, null); 2338 await assertPropagatedAssignedType(code, typeProvider.intType, null);
2338 assertTypeOfMarkedExpression(code, typeProvider.intType, null); 2339 await assertTypeOfMarkedExpression(code, typeProvider.intType, null);
2339 } 2340 }
2340 2341
2341 void test_localVariableInference_transitive_list_local() { 2342 test_localVariableInference_transitive_list_local() async {
2342 String code = r''' 2343 String code = r'''
2343 main() { 2344 main() {
2344 var x = <int>[3]; 2345 var x = <int>[3];
2345 var v = x[0]; 2346 var v = x[0];
2346 v; // marker 2347 v; // marker
2347 }'''; 2348 }''';
2348 assertPropagatedAssignedType(code, typeProvider.intType, null); 2349 await assertPropagatedAssignedType(code, typeProvider.intType, null);
2349 assertTypeOfMarkedExpression(code, typeProvider.intType, null); 2350 await assertTypeOfMarkedExpression(code, typeProvider.intType, null);
2350 } 2351 }
2351 2352
2352 void test_localVariableInference_transitive_local() { 2353 test_localVariableInference_transitive_local() async {
2353 String code = r''' 2354 String code = r'''
2354 main() { 2355 main() {
2355 var x = 3; 2356 var x = 3;
2356 var v = x; 2357 var v = x;
2357 v; // marker 2358 v; // marker
2358 }'''; 2359 }''';
2359 assertPropagatedAssignedType(code, typeProvider.intType, null); 2360 await assertPropagatedAssignedType(code, typeProvider.intType, null);
2360 assertTypeOfMarkedExpression(code, typeProvider.intType, null); 2361 await assertTypeOfMarkedExpression(code, typeProvider.intType, null);
2361 } 2362 }
2362 2363
2363 void test_localVariableInference_transitive_toplevel_inferred_lexical() { 2364 test_localVariableInference_transitive_toplevel_inferred_lexical() async {
2364 String code = r''' 2365 String code = r'''
2365 final x = 3; 2366 final x = 3;
2366 main() { 2367 main() {
2367 var v = x; 2368 var v = x;
2368 v; // marker 2369 v; // marker
2369 } 2370 }
2370 '''; 2371 ''';
2371 assertPropagatedAssignedType(code, typeProvider.intType, null); 2372 await assertPropagatedAssignedType(code, typeProvider.intType, null);
2372 assertTypeOfMarkedExpression(code, typeProvider.intType, null); 2373 await assertTypeOfMarkedExpression(code, typeProvider.intType, null);
2373 } 2374 }
2374 2375
2375 void test_localVariableInference_transitive_toplevel_inferred_reversed() { 2376 test_localVariableInference_transitive_toplevel_inferred_reversed() async {
2376 String code = r''' 2377 String code = r'''
2377 main() { 2378 main() {
2378 var v = x; 2379 var v = x;
2379 v; // marker 2380 v; // marker
2380 } 2381 }
2381 final x = 3; 2382 final x = 3;
2382 '''; 2383 ''';
2383 assertPropagatedAssignedType(code, typeProvider.intType, null); 2384 await assertPropagatedAssignedType(code, typeProvider.intType, null);
2384 assertTypeOfMarkedExpression(code, typeProvider.intType, null); 2385 await assertTypeOfMarkedExpression(code, typeProvider.intType, null);
2385 } 2386 }
2386 2387
2387 void test_localVariableInference_transitive_toplevel_lexical() { 2388 test_localVariableInference_transitive_toplevel_lexical() async {
2388 String code = r''' 2389 String code = r'''
2389 int x = 3; 2390 int x = 3;
2390 main() { 2391 main() {
2391 var v = x; 2392 var v = x;
2392 v; // marker 2393 v; // marker
2393 } 2394 }
2394 '''; 2395 ''';
2395 assertPropagatedAssignedType(code, typeProvider.intType, null); 2396 await assertPropagatedAssignedType(code, typeProvider.intType, null);
2396 assertTypeOfMarkedExpression(code, typeProvider.intType, null); 2397 await assertTypeOfMarkedExpression(code, typeProvider.intType, null);
2397 } 2398 }
2398 2399
2399 void test_localVariableInference_transitive_toplevel_reversed() { 2400 test_localVariableInference_transitive_toplevel_reversed() async {
2400 String code = r''' 2401 String code = r'''
2401 main() { 2402 main() {
2402 var v = x; 2403 var v = x;
2403 v; // marker 2404 v; // marker
2404 } 2405 }
2405 int x = 3; 2406 int x = 3;
2406 '''; 2407 ''';
2407 assertPropagatedAssignedType(code, typeProvider.intType, null); 2408 await assertPropagatedAssignedType(code, typeProvider.intType, null);
2408 assertTypeOfMarkedExpression(code, typeProvider.intType, null); 2409 await assertTypeOfMarkedExpression(code, typeProvider.intType, null);
2409 } 2410 }
2410 } 2411 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698