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

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

Issue 1462133005: Downwards inference. This adds support to the resolver for downwards (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Address comments 2 Created 5 years 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) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 engine.resolver_test; 5 library engine.resolver_test;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 8
9 import 'package:analyzer/src/context/context.dart' as newContext; 9 import 'package:analyzer/src/context/context.dart' as newContext;
10 import 'package:analyzer/src/generated/ast.dart'; 10 import 'package:analyzer/src/generated/ast.dart';
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 runReflectiveTests(TypeProviderImplTest); 61 runReflectiveTests(TypeProviderImplTest);
62 runReflectiveTests(TypeResolverVisitorTest); 62 runReflectiveTests(TypeResolverVisitorTest);
63 runReflectiveTests(CheckedModeCompileTimeErrorCodeTest); 63 runReflectiveTests(CheckedModeCompileTimeErrorCodeTest);
64 runReflectiveTests(ErrorResolverTest); 64 runReflectiveTests(ErrorResolverTest);
65 runReflectiveTests(HintCodeTest); 65 runReflectiveTests(HintCodeTest);
66 runReflectiveTests(MemberMapTest); 66 runReflectiveTests(MemberMapTest);
67 runReflectiveTests(NonHintCodeTest); 67 runReflectiveTests(NonHintCodeTest);
68 runReflectiveTests(SimpleResolverTest); 68 runReflectiveTests(SimpleResolverTest);
69 runReflectiveTests(StrictModeTest); 69 runReflectiveTests(StrictModeTest);
70 runReflectiveTests(TypePropagationTest); 70 runReflectiveTests(TypePropagationTest);
71 runReflectiveTests(StrongModeDownwardsInferenceTest);
71 runReflectiveTests(StrongModeStaticTypeAnalyzer2Test); 72 runReflectiveTests(StrongModeStaticTypeAnalyzer2Test);
72 runReflectiveTests(StrongModeTypePropagationTest); 73 runReflectiveTests(StrongModeTypePropagationTest);
73 } 74 }
74 75
75 /** 76 /**
76 * The class `AnalysisContextFactory` defines utility methods used to create ana lysis contexts 77 * The class `AnalysisContextFactory` defines utility methods used to create ana lysis contexts
77 * for testing purposes. 78 * for testing purposes.
78 */ 79 */
79 class AnalysisContextFactory { 80 class AnalysisContextFactory {
80 static String _DART_MATH = "dart:math"; 81 static String _DART_MATH = "dart:math";
(...skipping 12044 matching lines...) Expand 10 before | Expand all | Expand 10 after
12125 int f() { 12126 int f() {
12126 num n = 1234; 12127 num n = 1234;
12127 return n & 0x0F; 12128 return n & 0x0F;
12128 }'''); 12129 }''');
12129 computeLibrarySourceErrors(source); 12130 computeLibrarySourceErrors(source);
12130 assertErrors(source, [StaticTypeWarningCode.UNDEFINED_OPERATOR]); 12131 assertErrors(source, [StaticTypeWarningCode.UNDEFINED_OPERATOR]);
12131 } 12132 }
12132 } 12133 }
12133 12134
12134 /** 12135 /**
12136 * Strong mode static analyzer downwards inference tests
12137 */
12138 @reflectiveTest
12139 class StrongModeDownwardsInferenceTest extends ResolverTestCase {
12140 TypeAssertions _assertions;
12141 AsserterBuilder<Element, DartType> _hasElement;
12142 AsserterBuilder<DartType, DartType> _isType;
12143 AsserterBuilder2<Asserter<DartType>, Asserter<DartType>,
12144 DartType> _isFunction2Of;
12145 AsserterBuilderBuilder<Asserter<DartType>, List<Asserter<DartType>>,
12146 DartType> _isInstantiationOf;
12147 Asserter<DartType> _isInt;
12148 Asserter<DartType> _isNum;
12149 Asserter<DartType> _isString;
12150 Asserter<DartType> _isDynamic;
12151 AsserterBuilder<Asserter<DartType>, InterfaceType> _isListOf;
12152 AsserterBuilder2<Asserter<DartType>, Asserter<DartType>,
12153 InterfaceType> _isMapOf;
12154
12155 @override
12156 void setUp() {
12157 AnalysisOptionsImpl options = new AnalysisOptionsImpl();
12158 options.strongMode = true;
12159 resetWithOptions(options);
12160 _assertions = new TypeAssertions(typeProvider);
12161 _isType = _assertions.isType;
12162 _hasElement = _assertions.hasElement;
12163 _isInstantiationOf = _assertions.isInstantiationOf;
12164 _isInt = _assertions.isInt;
12165 _isNum = _assertions.isNum;
12166 _isString = _assertions.isString;
12167 _isDynamic = _assertions.isDynamic;
12168 _isListOf = _assertions.isListOf;
12169 _isMapOf = _assertions.isMapOf;
12170 _isFunction2Of = _assertions.isFunction2Of;
12171 }
12172
12173 void test_cascadeExpression() {
12174 String code = r'''
12175 class A<T> {
12176 List<T> map(T a, List<T> mapper(T x)) => mapper(a);
12177 }
12178
12179 void main () {
12180 A<int> a = new A()..map(0, (x) => [x]);
12181 }
12182 ''';
12183 CompilationUnit unit = resolveSource(code);
12184 List<Statement> statements =
12185 AstFinder.getStatementsInTopLevelFunction(unit, "main");
12186 CascadeExpression fetch(int i) {
12187 VariableDeclarationStatement stmt = statements[i];
12188 VariableDeclaration decl = stmt.variables.variables[0];
12189 CascadeExpression exp = decl.initializer;
12190 return exp;
12191 }
12192 Element elementA = AstFinder.getClass(unit, "A").element;
12193
12194 CascadeExpression cascade = fetch(0);
12195 _isInstantiationOf(_hasElement(elementA))([_isInt])(cascade.staticType);
12196 MethodInvocation invoke = cascade.cascadeSections[0];
12197 FunctionExpression function = invoke.argumentList.arguments[1];
12198 ExecutableElement f0 = function.element;
12199 _isListOf(_isInt)(f0.type.returnType);
12200 expect(f0.type.normalParameterTypes[0], typeProvider.intType);
12201 }
12202
12203 void test_constructorInitializer_propagation() {
12204 String code = r'''
12205 class A {
12206 List<String> x;
12207 A() : this.x = [];
12208 }
12209 ''';
12210 CompilationUnit unit = resolveSource(code);
12211 ConstructorDeclaration constructor =
12212 AstFinder.getConstructorInClass(unit, "A", null);
12213 ConstructorFieldInitializer assignment = constructor.initializers[0];
12214 Expression exp = assignment.expression;
12215 _isListOf(_isString)(exp.staticType);
12216 }
12217
12218 void test_factoryConstructor_propagation() {
12219 String code = r'''
12220 class A<T> {
12221 factory A() { return new B(); }
12222 }
12223 class B<S> extends A<S> {}
12224 ''';
12225 CompilationUnit unit = resolveSource(code);
12226
12227 ConstructorDeclaration constructor =
12228 AstFinder.getConstructorInClass(unit, "A", null);
12229 BlockFunctionBody body = constructor.body;
12230 ReturnStatement stmt = body.block.statements[0];
12231 InstanceCreationExpression exp = stmt.expression;
12232 ClassElement elementB = AstFinder.getClass(unit, "B").element;
12233 ClassElement elementA = AstFinder.getClass(unit, "A").element;
12234 expect(exp.constructorName.type.type.element, elementB);
12235 _isInstantiationOf(_hasElement(elementB))(
12236 [_isType(elementA.typeParameters[0].type)])(exp.staticType);
12237 }
12238
12239 void test_fieldDeclaration_propagation() {
12240 String code = r'''
12241 class A {
12242 List<String> f0 = ["hello"];
12243 }
12244 ''';
12245 CompilationUnit unit = resolveSource(code);
12246
12247 VariableDeclaration field = AstFinder.getFieldInClass(unit, "A", "f0");
12248
12249 _isListOf(_isString)(field.initializer.staticType);
12250 }
12251
12252 void test_functionDeclaration_body_propagation() {
12253 String code = r'''
12254 typedef T Function2<S, T>(S x);
12255
12256 List<int> test1() => [];
12257
12258 Function2<int, int> test2 (int x) {
12259 Function2<String, int> inner() {
12260 return (x) => x.length;
12261 }
12262 return (x) => x;
12263 }
12264 ''';
12265 CompilationUnit unit = resolveSource(code);
12266
12267 Asserter<InterfaceType> assertListOfInt = _isListOf(_isInt);
12268
12269 FunctionDeclaration test1 = AstFinder.getTopLevelFunction(unit, "test1");
12270 ExpressionFunctionBody body = test1.functionExpression.body;
12271 assertListOfInt(body.expression.staticType);
12272
12273 List<Statement> statements =
12274 AstFinder.getStatementsInTopLevelFunction(unit, "test2");
12275
12276 FunctionDeclaration inner =
12277 (statements[0] as FunctionDeclarationStatement).functionDeclaration;
12278 BlockFunctionBody body0 = inner.functionExpression.body;
12279 ReturnStatement return0 = body0.block.statements[0];
12280 Expression anon0 = return0.expression;
12281 FunctionType type0 = anon0.staticType;
12282 expect(type0.returnType, typeProvider.intType);
12283 expect(type0.normalParameterTypes[0], typeProvider.stringType);
12284
12285 FunctionExpression anon1 = (statements[1] as ReturnStatement).expression;
12286 FunctionType type1 = anon1.element.type;
12287 expect(type1.returnType, typeProvider.intType);
12288 expect(type1.normalParameterTypes[0], typeProvider.intType);
12289 }
12290
12291 void test_functionLiteral_assignment_typedArguments() {
12292 String code = r'''
12293 typedef T Function2<S, T>(S x);
12294
12295 void main () {
12296 Function2<int, String> l0 = (int x) => null;
12297 Function2<int, String> l1 = (int x) => "hello";
12298 Function2<int, String> l2 = (String x) => "hello";
12299 Function2<int, String> l3 = (int x) => 3;
12300 Function2<int, String> l4 = (int x) {return 3;};
12301 }
12302 ''';
12303 CompilationUnit unit = resolveSource(code);
12304 List<Statement> statements =
12305 AstFinder.getStatementsInTopLevelFunction(unit, "main");
12306 DartType literal(int i) {
12307 VariableDeclarationStatement stmt = statements[i];
12308 VariableDeclaration decl = stmt.variables.variables[0];
12309 FunctionExpression exp = decl.initializer;
12310 return exp.element.type;
12311 }
12312 _isFunction2Of(_isInt, _isString)(literal(0));
12313 _isFunction2Of(_isInt, _isString)(literal(1));
12314 _isFunction2Of(_isString, _isString)(literal(2));
12315 _isFunction2Of(_isInt, _isInt)(literal(3));
12316 _isFunction2Of(_isInt, _isString)(literal(4));
12317 }
12318
12319 void test_functionLiteral_assignment_unTypedArguments() {
12320 String code = r'''
12321 typedef T Function2<S, T>(S x);
12322
12323 void main () {
12324 Function2<int, String> l0 = (x) => null;
12325 Function2<int, String> l1 = (x) => "hello";
12326 Function2<int, String> l2 = (x) => "hello";
12327 Function2<int, String> l3 = (x) => 3;
12328 Function2<int, String> l4 = (x) {return 3;};
12329 }
12330 ''';
12331 CompilationUnit unit = resolveSource(code);
12332 List<Statement> statements =
12333 AstFinder.getStatementsInTopLevelFunction(unit, "main");
12334 DartType literal(int i) {
12335 VariableDeclarationStatement stmt = statements[i];
12336 VariableDeclaration decl = stmt.variables.variables[0];
12337 FunctionExpression exp = decl.initializer;
12338 return exp.element.type;
12339 }
12340 _isFunction2Of(_isInt, _isString)(literal(0));
12341 _isFunction2Of(_isInt, _isString)(literal(1));
12342 _isFunction2Of(_isInt, _isString)(literal(2));
12343 _isFunction2Of(_isInt, _isInt)(literal(3));
12344 _isFunction2Of(_isInt, _isString)(literal(4));
12345 }
12346
12347 void test_functionLiteral_body_propagation() {
12348 String code = r'''
12349 typedef T Function2<S, T>(S x);
12350
12351 void main () {
12352 Function2<int, List<String>> l0 = (int x) => ["hello"];
12353 Function2<int, List<String>> l1 = (String x) => ["hello"];
12354 Function2<int, List<String>> l2 = (int x) => [3];
12355 Function2<int, List<String>> l3 = (int x) {return [3];};
12356 }
12357 ''';
12358 CompilationUnit unit = resolveSource(code);
12359 List<Statement> statements =
12360 AstFinder.getStatementsInTopLevelFunction(unit, "main");
12361 Expression functionReturnValue(int i) {
12362 VariableDeclarationStatement stmt = statements[i];
12363 VariableDeclaration decl = stmt.variables.variables[0];
12364 FunctionExpression exp = decl.initializer;
12365 FunctionBody body = exp.body;
12366 if (body is ExpressionFunctionBody) {
12367 return body.expression;
12368 } else {
12369 Statement stmt = (body as BlockFunctionBody).block.statements[0];
12370 return (stmt as ReturnStatement).expression;
12371 }
12372 }
12373 Asserter<InterfaceType> assertListOfString = _isListOf(_isString);
12374 assertListOfString(functionReturnValue(0).staticType);
12375 assertListOfString(functionReturnValue(1).staticType);
12376 assertListOfString(functionReturnValue(2).staticType);
12377 assertListOfString(functionReturnValue(3).staticType);
12378 }
12379
12380 void test_functionLiteral_functionExpressionInvocation_typedArguments() {
12381 String code = r'''
12382 class Mapper<F, T> {
12383 T map(T mapper(F x)) => mapper(null);
12384 }
12385
12386 void main () {
12387 (new Mapper<int, String>().map)((int x) => null);
12388 (new Mapper<int, String>().map)((int x) => "hello");
12389 (new Mapper<int, String>().map)((String x) => "hello");
12390 (new Mapper<int, String>().map)((int x) => 3);
12391 (new Mapper<int, String>().map)((int x) {return 3;});
12392 }
12393 ''';
12394 CompilationUnit unit = resolveSource(code);
12395 List<Statement> statements =
12396 AstFinder.getStatementsInTopLevelFunction(unit, "main");
12397 DartType literal(int i) {
12398 ExpressionStatement stmt = statements[i];
12399 FunctionExpressionInvocation invk = stmt.expression;
12400 FunctionExpression exp = invk.argumentList.arguments[0];
12401 return exp.element.type;
12402 }
12403 _isFunction2Of(_isInt, _isString)(literal(0));
12404 _isFunction2Of(_isInt, _isString)(literal(1));
12405 _isFunction2Of(_isString, _isString)(literal(2));
12406 _isFunction2Of(_isInt, _isInt)(literal(3));
12407 _isFunction2Of(_isInt, _isString)(literal(4));
12408 }
12409
12410 void test_functionLiteral_functionExpressionInvocation_unTypedArguments() {
12411 String code = r'''
12412 class Mapper<F, T> {
12413 T map(T mapper(F x)) => mapper(null);
12414 }
12415
12416 void main () {
12417 (new Mapper<int, String>().map)((x) => null);
12418 (new Mapper<int, String>().map)((x) => "hello");
12419 (new Mapper<int, String>().map)((x) => "hello");
12420 (new Mapper<int, String>().map)((x) => 3);
12421 (new Mapper<int, String>().map)((x) {return 3;});
12422 }
12423 ''';
12424 CompilationUnit unit = resolveSource(code);
12425 List<Statement> statements =
12426 AstFinder.getStatementsInTopLevelFunction(unit, "main");
12427 DartType literal(int i) {
12428 ExpressionStatement stmt = statements[i];
12429 FunctionExpressionInvocation invk = stmt.expression;
12430 FunctionExpression exp = invk.argumentList.arguments[0];
12431 return exp.element.type;
12432 }
12433 _isFunction2Of(_isInt, _isString)(literal(0));
12434 _isFunction2Of(_isInt, _isString)(literal(1));
12435 _isFunction2Of(_isInt, _isString)(literal(2));
12436 _isFunction2Of(_isInt, _isInt)(literal(3));
12437 _isFunction2Of(_isInt, _isString)(literal(4));
12438 }
12439
12440 void test_functionLiteral_functionInvocation_typedArguments() {
12441 String code = r'''
12442 String map(String mapper(int x)) => mapper(null);
12443
12444 void main () {
12445 map((int x) => null);
12446 map((int x) => "hello");
12447 map((String x) => "hello");
12448 map((int x) => 3);
12449 map((int x) {return 3;});
12450 }
12451 ''';
12452 CompilationUnit unit = resolveSource(code);
12453 List<Statement> statements =
12454 AstFinder.getStatementsInTopLevelFunction(unit, "main");
12455 DartType literal(int i) {
12456 ExpressionStatement stmt = statements[i];
12457 MethodInvocation invk = stmt.expression;
12458 FunctionExpression exp = invk.argumentList.arguments[0];
12459 return exp.element.type;
12460 }
12461 _isFunction2Of(_isInt, _isString)(literal(0));
12462 _isFunction2Of(_isInt, _isString)(literal(1));
12463 _isFunction2Of(_isString, _isString)(literal(2));
12464 _isFunction2Of(_isInt, _isInt)(literal(3));
12465 _isFunction2Of(_isInt, _isString)(literal(4));
12466 }
12467
12468 void test_functionLiteral_functionInvocation_unTypedArguments() {
12469 String code = r'''
12470 String map(String mapper(int x)) => mapper(null);
12471
12472 void main () {
12473 map((x) => null);
12474 map((x) => "hello");
12475 map((x) => "hello");
12476 map((x) => 3);
12477 map((x) {return 3;});
12478 }
12479 ''';
12480 CompilationUnit unit = resolveSource(code);
12481 List<Statement> statements =
12482 AstFinder.getStatementsInTopLevelFunction(unit, "main");
12483 DartType literal(int i) {
12484 ExpressionStatement stmt = statements[i];
12485 MethodInvocation invk = stmt.expression;
12486 FunctionExpression exp = invk.argumentList.arguments[0];
12487 return exp.element.type;
12488 }
12489 _isFunction2Of(_isInt, _isString)(literal(0));
12490 _isFunction2Of(_isInt, _isString)(literal(1));
12491 _isFunction2Of(_isInt, _isString)(literal(2));
12492 _isFunction2Of(_isInt, _isInt)(literal(3));
12493 _isFunction2Of(_isInt, _isString)(literal(4));
12494 }
12495
12496 void test_functionLiteral_methodInvocation_typedArguments() {
12497 String code = r'''
12498 class Mapper<F, T> {
12499 T map(T mapper(F x)) => mapper(null);
12500 }
12501
12502 void main () {
12503 new Mapper<int, String>().map((int x) => null);
12504 new Mapper<int, String>().map((int x) => "hello");
12505 new Mapper<int, String>().map((String x) => "hello");
12506 new Mapper<int, String>().map((int x) => 3);
12507 new Mapper<int, String>().map((int x) {return 3;});
12508 }
12509 ''';
12510 CompilationUnit unit = resolveSource(code);
12511 List<Statement> statements =
12512 AstFinder.getStatementsInTopLevelFunction(unit, "main");
12513 DartType literal(int i) {
12514 ExpressionStatement stmt = statements[i];
12515 MethodInvocation invk = stmt.expression;
12516 FunctionExpression exp = invk.argumentList.arguments[0];
12517 return exp.element.type;
12518 }
12519 _isFunction2Of(_isInt, _isString)(literal(0));
12520 _isFunction2Of(_isInt, _isString)(literal(1));
12521 _isFunction2Of(_isString, _isString)(literal(2));
12522 _isFunction2Of(_isInt, _isInt)(literal(3));
12523 _isFunction2Of(_isInt, _isString)(literal(4));
12524 }
12525
12526 void test_functionLiteral_methodInvocation_unTypedArguments() {
12527 String code = r'''
12528 class Mapper<F, T> {
12529 T map(T mapper(F x)) => mapper(null);
12530 }
12531
12532 void main () {
12533 new Mapper<int, String>().map((x) => null);
12534 new Mapper<int, String>().map((x) => "hello");
12535 new Mapper<int, String>().map((x) => "hello");
12536 new Mapper<int, String>().map((x) => 3);
12537 new Mapper<int, String>().map((x) {return 3;});
12538 }
12539 ''';
12540 CompilationUnit unit = resolveSource(code);
12541 List<Statement> statements =
12542 AstFinder.getStatementsInTopLevelFunction(unit, "main");
12543 DartType literal(int i) {
12544 ExpressionStatement stmt = statements[i];
12545 MethodInvocation invk = stmt.expression;
12546 FunctionExpression exp = invk.argumentList.arguments[0];
12547 return exp.element.type;
12548 }
12549 _isFunction2Of(_isInt, _isString)(literal(0));
12550 _isFunction2Of(_isInt, _isString)(literal(1));
12551 _isFunction2Of(_isInt, _isString)(literal(2));
12552 _isFunction2Of(_isInt, _isInt)(literal(3));
12553 _isFunction2Of(_isInt, _isString)(literal(4));
12554 }
12555
12556 void test_functionLiteral_unTypedArgument_propagation() {
12557 String code = r'''
12558 typedef T Function2<S, T>(S x);
12559
12560 void main () {
12561 Function2<int, int> l0 = (x) => x;
12562 Function2<int, int> l1 = (x) => x+1;
12563 Function2<int, String> l2 = (x) => x;
12564 Function2<int, String> l3 = (x) => x.toLowerCase();
12565 Function2<String, String> l4 = (x) => x.toLowerCase();
12566 }
12567 ''';
12568 CompilationUnit unit = resolveSource(code);
12569 List<Statement> statements =
12570 AstFinder.getStatementsInTopLevelFunction(unit, "main");
12571 Expression functionReturnValue(int i) {
12572 VariableDeclarationStatement stmt = statements[i];
12573 VariableDeclaration decl = stmt.variables.variables[0];
12574 FunctionExpression exp = decl.initializer;
12575 FunctionBody body = exp.body;
12576 if (body is ExpressionFunctionBody) {
12577 return body.expression;
12578 } else {
12579 Statement stmt = (body as BlockFunctionBody).block.statements[0];
12580 return (stmt as ReturnStatement).expression;
12581 }
12582 }
12583 expect(functionReturnValue(0).staticType, typeProvider.intType);
12584 expect(functionReturnValue(1).staticType, typeProvider.intType);
12585 expect(functionReturnValue(2).staticType, typeProvider.intType);
12586 expect(functionReturnValue(3).staticType, typeProvider.dynamicType);
12587 expect(functionReturnValue(4).staticType, typeProvider.stringType);
12588 }
12589
12590 void test_instanceCreation() {
12591 String code = r'''
12592 class A<S, T> {
12593 S x;
12594 T y;
12595 A(this.x, this.y);
12596 A.named(this.x, this.y);
12597 }
12598
12599 class B<S, T> extends A<T, S> {
12600 B(S y, T x) : super(x, y);
12601 B.named(S y, T x) : super.named(x, y);
12602 }
12603
12604 class C<S> extends B<S, S> {
12605 C(S a) : super(a, a);
12606 C.named(S a) : super.named(a, a);
12607 }
12608
12609 class D<S, T> extends B<T, int> {
12610 D(T a) : super(a, 3);
12611 D.named(T a) : super.named(a, 3);
12612 }
12613
12614 class E<S, T> extends A<C<S>, T> {
12615 E(T a) : super(null, a);
12616 }
12617
12618 class F<S, T> extends A<S, T> {
12619 F(S x, T y, {List<S> a, List<T> b}) : super(x, y);
12620 F.named(S x, T y, [S a, T b]) : super(a, b);
12621 }
12622
12623 void test0() {
12624 A<int, String> a0 = new A(3, "hello");
12625 A<int, String> a1 = new A.named(3, "hello");
12626 A<int, String> a2 = new A<int, String>(3, "hello");
12627 A<int, String> a3 = new A<int, String>.named(3, "hello");
12628 A<int, String> a4 = new A<int, dynamic>(3, "hello");
12629 A<int, String> a5 = new A<dynamic, dynamic>.named(3, "hello");
12630 }
12631 void test1() {
12632 A<int, String> a0 = new A("hello", 3);
12633 A<int, String> a1 = new A.named("hello", 3);
12634 }
12635 void test2() {
12636 A<int, String> a0 = new B("hello", 3);
12637 A<int, String> a1 = new B.named("hello", 3);
12638 A<int, String> a2 = new B<String, int>("hello", 3);
12639 A<int, String> a3 = new B<String, int>.named("hello", 3);
12640 A<int, String> a4 = new B<String, dynamic>("hello", 3);
12641 A<int, String> a5 = new B<dynamic, dynamic>.named("hello", 3);
12642 }
12643 void test3() {
12644 A<int, String> a0 = new B(3, "hello");
12645 A<int, String> a1 = new B.named(3, "hello");
12646 }
12647 void test4() {
12648 A<int, int> a0 = new C(3);
12649 A<int, int> a1 = new C.named(3);
12650 A<int, int> a2 = new C<int>(3);
12651 A<int, int> a3 = new C<int>.named(3);
12652 A<int, int> a4 = new C<dynamic>(3);
12653 A<int, int> a5 = new C<dynamic>.named(3);
12654 }
12655 void test5() {
12656 A<int, int> a0 = new C("hello");
12657 A<int, int> a1 = new C.named("hello");
12658 }
12659 void test6() {
12660 A<int, String> a0 = new D("hello");
12661 A<int, String> a1 = new D.named("hello");
12662 A<int, String> a2 = new D<int, String>("hello");
12663 A<int, String> a3 = new D<String, String>.named("hello");
12664 A<int, String> a4 = new D<num, dynamic>("hello");
12665 A<int, String> a5 = new D<dynamic, dynamic>.named("hello");
12666 }
12667 void test7() {
12668 A<int, String> a0 = new D(3);
12669 A<int, String> a1 = new D.named(3);
12670 }
12671 void test8() {
12672 // Currently we only allow variable constraints. Test that we reject.
12673 A<C<int>, String> a0 = new E("hello");
12674 }
12675 void test9() { // Check named and optional arguments
12676 A<int, String> a0 = new F(3, "hello", a: [3], b: ["hello"]);
12677 A<int, String> a1 = new F(3, "hello", a: ["hello"], b:[3]);
12678 A<int, String> a2 = new F.named(3, "hello", 3, "hello");
12679 A<int, String> a3 = new F.named(3, "hello");
12680 A<int, String> a4 = new F.named(3, "hello", "hello", 3);
12681 A<int, String> a5 = new F.named(3, "hello", "hello");
12682 }
12683 }''';
12684 CompilationUnit unit = resolveSource(code);
12685
12686 Expression rhs(VariableDeclarationStatement stmt) {
12687 VariableDeclaration decl = stmt.variables.variables[0];
12688 Expression exp = decl.initializer;
12689 return exp;
12690 }
12691
12692 void hasType(Asserter<DartType> assertion, Expression exp) =>
12693 assertion(exp.staticType);
12694
12695 Element elementA = AstFinder.getClass(unit, "A").element;
12696 Element elementB = AstFinder.getClass(unit, "B").element;
12697 Element elementC = AstFinder.getClass(unit, "C").element;
12698 Element elementD = AstFinder.getClass(unit, "D").element;
12699 Element elementE = AstFinder.getClass(unit, "E").element;
12700 Element elementF = AstFinder.getClass(unit, "F").element;
12701
12702 AsserterBuilder<List<Asserter<DartType>>, DartType> assertAOf =
12703 _isInstantiationOf(_hasElement(elementA));
12704 AsserterBuilder<List<Asserter<DartType>>, DartType> assertBOf =
12705 _isInstantiationOf(_hasElement(elementB));
12706 AsserterBuilder<List<Asserter<DartType>>, DartType> assertCOf =
12707 _isInstantiationOf(_hasElement(elementC));
12708 AsserterBuilder<List<Asserter<DartType>>, DartType> assertDOf =
12709 _isInstantiationOf(_hasElement(elementD));
12710 AsserterBuilder<List<Asserter<DartType>>, DartType> assertEOf =
12711 _isInstantiationOf(_hasElement(elementE));
12712 AsserterBuilder<List<Asserter<DartType>>, DartType> assertFOf =
12713 _isInstantiationOf(_hasElement(elementF));
12714
12715 {
12716 List<Statement> statements =
12717 AstFinder.getStatementsInTopLevelFunction(unit, "test0");
12718
12719 hasType(assertAOf([_isInt, _isString]), rhs(statements[0]));
12720 hasType(assertAOf([_isInt, _isString]), rhs(statements[0]));
12721 hasType(assertAOf([_isInt, _isString]), rhs(statements[1]));
12722 hasType(assertAOf([_isInt, _isString]), rhs(statements[2]));
12723 hasType(assertAOf([_isInt, _isString]), rhs(statements[3]));
12724 hasType(assertAOf([_isInt, _isDynamic]), rhs(statements[4]));
12725 hasType(assertAOf([_isDynamic, _isDynamic]), rhs(statements[5]));
12726 }
12727
12728 {
12729 List<Statement> statements =
12730 AstFinder.getStatementsInTopLevelFunction(unit, "test1");
12731 hasType(assertAOf([_isInt, _isString]), rhs(statements[0]));
12732 hasType(assertAOf([_isInt, _isString]), rhs(statements[1]));
12733 }
12734
12735 {
12736 List<Statement> statements =
12737 AstFinder.getStatementsInTopLevelFunction(unit, "test2");
12738 hasType(assertBOf([_isString, _isInt]), rhs(statements[0]));
12739 hasType(assertBOf([_isString, _isInt]), rhs(statements[1]));
12740 hasType(assertBOf([_isString, _isInt]), rhs(statements[2]));
12741 hasType(assertBOf([_isString, _isInt]), rhs(statements[3]));
12742 hasType(assertBOf([_isString, _isDynamic]), rhs(statements[4]));
12743 hasType(assertBOf([_isDynamic, _isDynamic]), rhs(statements[5]));
12744 }
12745
12746 {
12747 List<Statement> statements =
12748 AstFinder.getStatementsInTopLevelFunction(unit, "test3");
12749 hasType(assertBOf([_isString, _isInt]), rhs(statements[0]));
12750 hasType(assertBOf([_isString, _isInt]), rhs(statements[1]));
12751 }
12752
12753 {
12754 List<Statement> statements =
12755 AstFinder.getStatementsInTopLevelFunction(unit, "test4");
12756 hasType(assertCOf([_isInt]), rhs(statements[0]));
12757 hasType(assertCOf([_isInt]), rhs(statements[1]));
12758 hasType(assertCOf([_isInt]), rhs(statements[2]));
12759 hasType(assertCOf([_isInt]), rhs(statements[3]));
12760 hasType(assertCOf([_isDynamic]), rhs(statements[4]));
12761 hasType(assertCOf([_isDynamic]), rhs(statements[5]));
12762 }
12763
12764 {
12765 List<Statement> statements =
12766 AstFinder.getStatementsInTopLevelFunction(unit, "test5");
12767 hasType(assertCOf([_isInt]), rhs(statements[0]));
12768 hasType(assertCOf([_isInt]), rhs(statements[1]));
12769 }
12770
12771 {
12772 // The first type parameter is not constrained by the
12773 // context. We could choose a tighter type, but currently
12774 // we just use dynamic.
12775 List<Statement> statements =
12776 AstFinder.getStatementsInTopLevelFunction(unit, "test6");
12777 hasType(assertDOf([_isDynamic, _isString]), rhs(statements[0]));
12778 hasType(assertDOf([_isDynamic, _isString]), rhs(statements[1]));
12779 hasType(assertDOf([_isInt, _isString]), rhs(statements[2]));
12780 hasType(assertDOf([_isString, _isString]), rhs(statements[3]));
12781 hasType(assertDOf([_isNum, _isDynamic]), rhs(statements[4]));
12782 hasType(assertDOf([_isDynamic, _isDynamic]), rhs(statements[5]));
12783 }
12784
12785 {
12786 List<Statement> statements =
12787 AstFinder.getStatementsInTopLevelFunction(unit, "test7");
12788 hasType(assertDOf([_isDynamic, _isString]), rhs(statements[0]));
12789 hasType(assertDOf([_isDynamic, _isString]), rhs(statements[1]));
12790 }
12791
12792 {
12793 List<Statement> statements =
12794 AstFinder.getStatementsInTopLevelFunction(unit, "test8");
12795 hasType(assertEOf([_isDynamic, _isDynamic]), rhs(statements[0]));
12796 }
12797
12798 {
12799 List<Statement> statements =
12800 AstFinder.getStatementsInTopLevelFunction(unit, "test9");
12801 hasType(assertFOf([_isInt, _isString]), rhs(statements[0]));
12802 hasType(assertFOf([_isInt, _isString]), rhs(statements[1]));
12803 hasType(assertFOf([_isInt, _isString]), rhs(statements[2]));
12804 hasType(assertFOf([_isInt, _isString]), rhs(statements[3]));
12805 hasType(assertFOf([_isInt, _isString]), rhs(statements[4]));
12806 hasType(assertFOf([_isInt, _isString]), rhs(statements[5]));
12807 }
12808 }
12809
12810 void test_listLiteral_nested() {
12811 String code = r'''
12812 void main () {
12813 List<List<int>> l0 = [[]];
12814 Iterable<List<int>> l1 = [[3]];
12815 Iterable<List<int>> l2 = [[3], [4]];
12816 List<List<int>> l3 = [["hello", 3], []];
12817 }
12818 ''';
12819 CompilationUnit unit = resolveSource(code);
12820 List<Statement> statements =
12821 AstFinder.getStatementsInTopLevelFunction(unit, "main");
12822 ListLiteral literal(int i) {
12823 VariableDeclarationStatement stmt = statements[i];
12824 VariableDeclaration decl = stmt.variables.variables[0];
12825 ListLiteral exp = decl.initializer;
12826 return exp;
12827 }
12828
12829 Asserter<InterfaceType> assertListOfInt = _isListOf(_isInt);
12830 Asserter<InterfaceType> assertListOfListOfInt = _isListOf(assertListOfInt);
12831
12832 assertListOfListOfInt(literal(0).staticType);
12833 assertListOfListOfInt(literal(1).staticType);
12834 assertListOfListOfInt(literal(2).staticType);
12835 assertListOfListOfInt(literal(3).staticType);
12836
12837 assertListOfInt(literal(1).elements[0].staticType);
12838 assertListOfInt(literal(2).elements[0].staticType);
12839 assertListOfInt(literal(3).elements[0].staticType);
12840 }
12841
12842 void test_listLiteral_simple() {
12843 String code = r'''
12844 void main () {
12845 List<int> l0 = [];
12846 List<int> l1 = [3];
12847 List<int> l2 = ["hello"];
12848 List<int> l3 = ["hello", 3];
12849 }
12850 ''';
12851 CompilationUnit unit = resolveSource(code);
12852 List<Statement> statements =
12853 AstFinder.getStatementsInTopLevelFunction(unit, "main");
12854 DartType literal(int i) {
12855 VariableDeclarationStatement stmt = statements[i];
12856 VariableDeclaration decl = stmt.variables.variables[0];
12857 ListLiteral exp = decl.initializer;
12858 return exp.staticType;
12859 }
12860
12861 Asserter<InterfaceType> assertListOfInt = _isListOf(_isInt);
12862
12863 assertListOfInt(literal(0));
12864 assertListOfInt(literal(1));
12865 assertListOfInt(literal(2));
12866 assertListOfInt(literal(3));
12867 }
12868
12869 void test_listLiteral_simple_const() {
12870 String code = r'''
12871 void main () {
12872 const List<int> c0 = const [];
12873 const List<int> c1 = const [3];
12874 const List<int> c2 = const ["hello"];
12875 const List<int> c3 = const ["hello", 3];
12876 }
12877 ''';
12878 CompilationUnit unit = resolveSource(code);
12879 List<Statement> statements =
12880 AstFinder.getStatementsInTopLevelFunction(unit, "main");
12881 DartType literal(int i) {
12882 VariableDeclarationStatement stmt = statements[i];
12883 VariableDeclaration decl = stmt.variables.variables[0];
12884 ListLiteral exp = decl.initializer;
12885 return exp.staticType;
12886 }
12887
12888 Asserter<InterfaceType> assertListOfInt = _isListOf(_isInt);
12889
12890 assertListOfInt(literal(0));
12891 assertListOfInt(literal(1));
12892 assertListOfInt(literal(2));
12893 assertListOfInt(literal(3));
12894 }
12895
12896 void test_listLiteral_simple_disabled() {
12897 String code = r'''
12898 void main () {
12899 List<int> l0 = <num>[];
12900 List<int> l1 = <num>[3];
12901 List<int> l2 = <String>["hello"];
12902 List<int> l3 = <dynamic>["hello", 3];
12903 }
12904 ''';
12905 CompilationUnit unit = resolveSource(code);
12906 List<Statement> statements =
12907 AstFinder.getStatementsInTopLevelFunction(unit, "main");
12908 DartType literal(int i) {
12909 VariableDeclarationStatement stmt = statements[i];
12910 VariableDeclaration decl = stmt.variables.variables[0];
12911 ListLiteral exp = decl.initializer;
12912 return exp.staticType;
12913 }
12914
12915 _isListOf(_isNum)(literal(0));
12916 _isListOf(_isNum)(literal(1));
12917 _isListOf(_isString)(literal(2));
12918 _isListOf(_isDynamic)(literal(3));
12919 }
12920
12921 void test_listLiteral_simple_subtype() {
12922 String code = r'''
12923 void main () {
12924 Iterable<int> l0 = [];
12925 Iterable<int> l1 = [3];
12926 Iterable<int> l2 = ["hello"];
12927 Iterable<int> l3 = ["hello", 3];
12928 }
12929 ''';
12930 CompilationUnit unit = resolveSource(code);
12931 List<Statement> statements =
12932 AstFinder.getStatementsInTopLevelFunction(unit, "main");
12933 DartType literal(int i) {
12934 VariableDeclarationStatement stmt = statements[i];
12935 VariableDeclaration decl = stmt.variables.variables[0];
12936 ListLiteral exp = decl.initializer;
12937 return exp.staticType;
12938 }
12939
12940 Asserter<InterfaceType> assertListOfInt = _isListOf(_isInt);
12941
12942 assertListOfInt(literal(0));
12943 assertListOfInt(literal(1));
12944 assertListOfInt(literal(2));
12945 assertListOfInt(literal(3));
12946 }
12947
12948 void test_mapLiteral_nested() {
12949 String code = r'''
12950 void main () {
12951 Map<int, List<String>> l0 = {};
12952 Map<int, List<String>> l1 = {3: ["hello"]};
12953 Map<int, List<String>> l2 = {"hello": ["hello"]};
12954 Map<int, List<String>> l3 = {3: [3]};
12955 Map<int, List<String>> l4 = {3:["hello"], "hello": [3]};
12956 }
12957 ''';
12958 CompilationUnit unit = resolveSource(code);
12959 List<Statement> statements =
12960 AstFinder.getStatementsInTopLevelFunction(unit, "main");
12961 MapLiteral literal(int i) {
12962 VariableDeclarationStatement stmt = statements[i];
12963 VariableDeclaration decl = stmt.variables.variables[0];
12964 MapLiteral exp = decl.initializer;
12965 return exp;
12966 }
12967
12968 Asserter<InterfaceType> assertListOfString = _isListOf(_isString);
12969 Asserter<InterfaceType> assertMapOfIntToListOfString =
12970 _isMapOf(_isInt, assertListOfString);
12971
12972 assertMapOfIntToListOfString(literal(0).staticType);
12973 assertMapOfIntToListOfString(literal(1).staticType);
12974 assertMapOfIntToListOfString(literal(2).staticType);
12975 assertMapOfIntToListOfString(literal(3).staticType);
12976 assertMapOfIntToListOfString(literal(4).staticType);
12977
12978 assertListOfString(literal(1).entries[0].value.staticType);
12979 assertListOfString(literal(2).entries[0].value.staticType);
12980 assertListOfString(literal(3).entries[0].value.staticType);
12981 assertListOfString(literal(4).entries[0].value.staticType);
12982 }
12983
12984 void test_mapLiteral_simple() {
12985 String code = r'''
12986 void main () {
12987 Map<int, String> l0 = {};
12988 Map<int, String> l1 = {3: "hello"};
12989 Map<int, String> l2 = {"hello": "hello"};
12990 Map<int, String> l3 = {3: 3};
12991 Map<int, String> l4 = {3:"hello", "hello": 3};
12992 }
12993 ''';
12994 CompilationUnit unit = resolveSource(code);
12995 List<Statement> statements =
12996 AstFinder.getStatementsInTopLevelFunction(unit, "main");
12997 DartType literal(int i) {
12998 VariableDeclarationStatement stmt = statements[i];
12999 VariableDeclaration decl = stmt.variables.variables[0];
13000 MapLiteral exp = decl.initializer;
13001 return exp.staticType;
13002 }
13003
13004 Asserter<InterfaceType> assertMapOfIntToString =
13005 _isMapOf(_isInt, _isString);
13006
13007 assertMapOfIntToString(literal(0));
13008 assertMapOfIntToString(literal(1));
13009 assertMapOfIntToString(literal(2));
13010 assertMapOfIntToString(literal(3));
13011 }
13012
13013 void test_mapLiteral_simple_disabled() {
13014 String code = r'''
13015 void main () {
13016 Map<int, String> l0 = <int, dynamic>{};
13017 Map<int, String> l1 = <int, dynamic>{3: "hello"};
13018 Map<int, String> l2 = <int, dynamic>{"hello": "hello"};
13019 Map<int, String> l3 = <int, dynamic>{3: 3};
13020 }
13021 ''';
13022 CompilationUnit unit = resolveSource(code);
13023 List<Statement> statements =
13024 AstFinder.getStatementsInTopLevelFunction(unit, "main");
13025 DartType literal(int i) {
13026 VariableDeclarationStatement stmt = statements[i];
13027 VariableDeclaration decl = stmt.variables.variables[0];
13028 MapLiteral exp = decl.initializer;
13029 return exp.staticType;
13030 }
13031
13032 Asserter<InterfaceType> assertMapOfIntToDynamic =
13033 _isMapOf(_isInt, _isDynamic);
13034
13035 assertMapOfIntToDynamic(literal(0));
13036 assertMapOfIntToDynamic(literal(1));
13037 assertMapOfIntToDynamic(literal(2));
13038 assertMapOfIntToDynamic(literal(3));
13039 }
13040
13041 void test_methodDeclaration_body_propagation() {
13042 String code = r'''
13043 class A {
13044 List<String> m0(int x) => ["hello"];
13045 List<String> m1(int x) {return [3];};
13046 }
13047 ''';
13048 CompilationUnit unit = resolveSource(code);
13049 Expression methodReturnValue(String methodName) {
13050 MethodDeclaration method =
13051 AstFinder.getMethodInClass(unit, "A", methodName);
13052 FunctionBody body = method.body;
13053 if (body is ExpressionFunctionBody) {
13054 return body.expression;
13055 } else {
13056 Statement stmt = (body as BlockFunctionBody).block.statements[0];
13057 return (stmt as ReturnStatement).expression;
13058 }
13059 }
13060 Asserter<InterfaceType> assertListOfString = _isListOf(_isString);
13061 assertListOfString(methodReturnValue("m0").staticType);
13062 assertListOfString(methodReturnValue("m1").staticType);
13063 }
13064
13065 void test_redirectingConstructor_propagation() {
13066 String code = r'''
13067 class A {
13068 A() : this.named([]);
13069 A.named(List<String> x);
13070 }
13071 ''';
13072 CompilationUnit unit = resolveSource(code);
13073
13074 ConstructorDeclaration constructor =
13075 AstFinder.getConstructorInClass(unit, "A", null);
13076 RedirectingConstructorInvocation invocation = constructor.initializers[0];
13077 Expression exp = invocation.argumentList.arguments[0];
13078 _isListOf(_isString)(exp.staticType);
13079 }
13080
13081 void test_superConstructorInvocation_propagation() {
13082 String code = r'''
13083 class B {
13084 B(List<String>);
13085 }
13086 class A extends B {
13087 A() : super([]);
13088 }
13089 ''';
13090 CompilationUnit unit = resolveSource(code);
13091
13092 ConstructorDeclaration constructor =
13093 AstFinder.getConstructorInClass(unit, "A", null);
13094 SuperConstructorInvocation invocation = constructor.initializers[0];
13095 Expression exp = invocation.argumentList.arguments[0];
13096 _isListOf(_isString)(exp.staticType);
13097 }
13098 }
13099
13100 /**
12135 * Strong mode static analyzer end to end tests 13101 * Strong mode static analyzer end to end tests
12136 */ 13102 */
12137 @reflectiveTest 13103 @reflectiveTest
12138 class StrongModeStaticTypeAnalyzer2Test extends _StaticTypeAnalyzer2TestShared { 13104 class StrongModeStaticTypeAnalyzer2Test extends _StaticTypeAnalyzer2TestShared {
12139 void fail_genericMethod_nested() { 13105 void fail_genericMethod_nested() {
12140 // TODO(jmesserly): this test currently fails because we incorrectly capture 13106 // TODO(jmesserly): this test currently fails because we incorrectly capture
12141 // S in FunctionTypeImpl.substitute. We should probably rename free 13107 // S in FunctionTypeImpl.substitute. We should probably rename free
12142 // variables during substitution to avoid it. 13108 // variables during substitution to avoid it.
12143 _resolveTestUnit(r''' 13109 _resolveTestUnit(r'''
12144 class C<T> { 13110 class C<T> {
(...skipping 3133 matching lines...) Expand 10 before | Expand all | Expand 10 after
15278 16244
15279 void _resolveTestUnit(String code) { 16245 void _resolveTestUnit(String code) {
15280 testCode = code; 16246 testCode = code;
15281 testSource = addSource(testCode); 16247 testSource = addSource(testCode);
15282 LibraryElement library = resolve2(testSource); 16248 LibraryElement library = resolve2(testSource);
15283 assertNoErrors(testSource); 16249 assertNoErrors(testSource);
15284 verify([testSource]); 16250 verify([testSource]);
15285 testUnit = resolveCompilationUnit(testSource, library); 16251 testUnit = resolveCompilationUnit(testSource, library);
15286 } 16252 }
15287 } 16253 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698