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

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

Issue 673673002: Record types of invoked variables. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « pkg/analyzer/lib/src/generated/resolver.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 import 'package:analyzer/src/generated/java_core.dart'; 8 import 'package:analyzer/src/generated/java_core.dart';
9 import 'package:analyzer/src/generated/java_junit.dart'; 9 import 'package:analyzer/src/generated/java_junit.dart';
10 import 'package:analyzer/src/generated/java_engine.dart'; 10 import 'package:analyzer/src/generated/java_engine.dart';
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 ClassElementImpl futureElement = ElementFactory.classElement2("Future", ["T" ]); 125 ClassElementImpl futureElement = ElementFactory.classElement2("Future", ["T" ]);
126 InterfaceType futureType = futureElement.type; 126 InterfaceType futureType = futureElement.type;
127 // factory Future.value([value]) 127 // factory Future.value([value])
128 ConstructorElementImpl futureConstructor = ElementFactory.constructorElement 2(futureElement, "value", []); 128 ConstructorElementImpl futureConstructor = ElementFactory.constructorElement 2(futureElement, "value", []);
129 futureConstructor.parameters = <ParameterElement> [ElementFactory.positional Parameter2("value", provider.dynamicType)]; 129 futureConstructor.parameters = <ParameterElement> [ElementFactory.positional Parameter2("value", provider.dynamicType)];
130 futureConstructor.factory = true; 130 futureConstructor.factory = true;
131 (futureConstructor.type as FunctionTypeImpl).typeArguments = futureElement.t ype.typeArguments; 131 (futureConstructor.type as FunctionTypeImpl).typeArguments = futureElement.t ype.typeArguments;
132 futureElement.constructors = <ConstructorElement> [futureConstructor]; 132 futureElement.constructors = <ConstructorElement> [futureConstructor];
133 // Future then(onValue(T value), { Function onError }); 133 // Future then(onValue(T value), { Function onError });
134 List<ParameterElement> parameters = <ParameterElement> [ElementFactory.requi redParameter2("value", futureElement.typeParameters[0].type)]; 134 List<ParameterElement> parameters = <ParameterElement> [ElementFactory.requi redParameter2("value", futureElement.typeParameters[0].type)];
135 FunctionTypeAliasElementImpl aliasElement = new FunctionTypeAliasElementImpl (null); 135 FunctionTypeAliasElementImpl aliasElement = new FunctionTypeAliasElementImpl .forNode(null);
136 aliasElement.synthetic = true; 136 aliasElement.synthetic = true;
137 aliasElement.shareParameters(parameters); 137 aliasElement.shareParameters(parameters);
138 aliasElement.returnType = provider.dynamicType; 138 aliasElement.returnType = provider.dynamicType;
139 FunctionTypeImpl aliasType = new FunctionTypeImpl.con2(aliasElement); 139 FunctionTypeImpl aliasType = new FunctionTypeImpl.con2(aliasElement);
140 aliasElement.shareTypeParameters(futureElement.typeParameters); 140 aliasElement.shareTypeParameters(futureElement.typeParameters);
141 aliasType.typeArguments = futureElement.type.typeArguments; 141 aliasType.typeArguments = futureElement.type.typeArguments;
142 MethodElement thenMethod = ElementFactory.methodElementWithParameters("then" , futureElement.type.typeArguments, futureType, [ 142 MethodElement thenMethod = ElementFactory.methodElementWithParameters("then" , futureElement.type.typeArguments, futureType, [
143 ElementFactory.requiredParameter2("onValue", aliasType), 143 ElementFactory.requiredParameter2("onValue", aliasType),
144 ElementFactory.namedParameter2("onError", provider.functionType)]); 144 ElementFactory.namedParameter2("onError", provider.functionType)]);
145 futureElement.methods = <MethodElement> [thenMethod]; 145 futureElement.methods = <MethodElement> [thenMethod];
(...skipping 6261 matching lines...) Expand 10 before | Expand all | Expand 10 after
6407 functionNode.name.staticElement = functionElement; 6407 functionNode.name.staticElement = functionElement;
6408 (unit.element as CompilationUnitElementImpl).functions = <FunctionElement> [ functionElement]; 6408 (unit.element as CompilationUnitElementImpl).functions = <FunctionElement> [ functionElement];
6409 return functionNode; 6409 return functionNode;
6410 } 6410 }
6411 6411
6412 FunctionTypeAlias _createResolvedFunctionTypeAlias() { 6412 FunctionTypeAlias _createResolvedFunctionTypeAlias() {
6413 CompilationUnit unit = _createResolvedCompilationUnit(); 6413 CompilationUnit unit = _createResolvedCompilationUnit();
6414 FunctionTypeAlias aliasNode = AstFactory.typeAlias(AstFactory.typeName4("A", []), "F", AstFactory.typeParameterList([]), AstFactory.formalParameterList([])) ; 6414 FunctionTypeAlias aliasNode = AstFactory.typeAlias(AstFactory.typeName4("A", []), "F", AstFactory.typeParameterList([]), AstFactory.formalParameterList([])) ;
6415 unit.declarations.add(aliasNode); 6415 unit.declarations.add(aliasNode);
6416 SimpleIdentifier aliasName = aliasNode.name; 6416 SimpleIdentifier aliasName = aliasNode.name;
6417 FunctionTypeAliasElement aliasElement = new FunctionTypeAliasElementImpl(ali asName); 6417 FunctionTypeAliasElement aliasElement = new FunctionTypeAliasElementImpl.for Node(aliasName);
6418 aliasName.staticElement = aliasElement; 6418 aliasName.staticElement = aliasElement;
6419 (unit.element as CompilationUnitElementImpl).typeAliases = <FunctionTypeAlia sElement> [aliasElement]; 6419 (unit.element as CompilationUnitElementImpl).typeAliases = <FunctionTypeAlia sElement> [aliasElement];
6420 return aliasNode; 6420 return aliasNode;
6421 } 6421 }
6422 6422
6423 MethodDeclaration _createResolvedMethodDeclaration() { 6423 MethodDeclaration _createResolvedMethodDeclaration() {
6424 ClassDeclaration classNode = _createResolvedClassDeclaration(); 6424 ClassDeclaration classNode = _createResolvedClassDeclaration();
6425 String methodName = "f"; 6425 String methodName = "f";
6426 MethodDeclaration methodNode = AstFactory.methodDeclaration(null, null, null , null, AstFactory.identifier3(methodName), AstFactory.formalParameterList([])); 6426 MethodDeclaration methodNode = AstFactory.methodDeclaration(null, null, null , null, AstFactory.identifier3(methodName), AstFactory.formalParameterList([]));
6427 classNode.members.add(methodNode); 6427 classNode.members.add(methodNode);
(...skipping 2011 matching lines...) Expand 10 before | Expand all | Expand 10 after
8439 SimpleIdentifier identifier = parameter.identifier; 8439 SimpleIdentifier identifier = parameter.identifier;
8440 Element element = identifier.staticElement; 8440 Element element = identifier.staticElement;
8441 if (element is! ParameterElement) { 8441 if (element is! ParameterElement) {
8442 element = new ParameterElementImpl.forNode(identifier); 8442 element = new ParameterElementImpl.forNode(identifier);
8443 identifier.staticElement = element; 8443 identifier.staticElement = element;
8444 } 8444 }
8445 (element as ParameterElementImpl).type = type; 8445 (element as ParameterElementImpl).type = type;
8446 } 8446 }
8447 } 8447 }
8448 8448
8449
8450 /**
8451 * Like [StaticTypeAnalyzerTest], but as end-to-end tests.
8452 */
8453 class StaticTypeAnalyzer2Test extends ResolverTestCase {
8454 String testCode;
8455 Source testSource;
8456 CompilationUnit testUnit;
8457
8458 void test_MethodInvocation_nameType_localVariable() {
8459 String code = r"""
8460 typedef Foo();
8461 main() {
8462 Foo foo;
8463 foo();
8464 }
8465 """;
8466 _resolveTestUnit(code);
8467 // "foo" should be resolved to the "Foo" type
8468 SimpleIdentifier identifier = _findIdentifier("foo();");
8469 DartType type = identifier.staticType;
8470 _ut.expect(type, new _ut.isInstanceOf<FunctionType>());
8471 }
8472
8473 void test_MethodInvocation_nameType_parameter_FunctionTypeAlias() {
8474 String code = r"""
8475 typedef Foo();
8476 main(Foo foo) {
8477 foo();
8478 }
8479 """;
8480 _resolveTestUnit(code);
8481 // "foo" should be resolved to the "Foo" type
8482 SimpleIdentifier identifier = _findIdentifier("foo();");
8483 DartType type = identifier.staticType;
8484 _ut.expect(type, new _ut.isInstanceOf<FunctionType>());
8485 }
8486
8487 void test_MethodInvocation_nameType_parameter_propagatedType() {
8488 String code = r"""
8489 typedef Foo();
8490 main(p) {
8491 if (p is Foo) {
8492 p();
8493 }
8494 }
8495 """;
8496 _resolveTestUnit(code);
8497 SimpleIdentifier identifier = _findIdentifier("p()");
8498 _ut.expect(identifier.staticType, DynamicTypeImpl.instance);
8499 {
8500 FunctionType type = identifier.propagatedType;
8501 _ut.expect(type, _ut.isNotNull);
8502 _ut.expect(type.name, 'Foo');
8503 }
8504 }
8505
8506 SimpleIdentifier _findIdentifier(String search) {
8507 SimpleIdentifier identifier = EngineTestCase.findNode(
8508 testUnit,
8509 testCode,
8510 search,
8511 (node) => node is SimpleIdentifier);
8512 return identifier;
8513 }
8514
8515 void _resolveTestUnit(String code) {
8516 testCode = code;
8517 testSource = addSource(testCode);
8518 LibraryElement library = resolve(testSource);
8519 assertNoErrors(testSource);
8520 verify([testSource]);
8521 testUnit = resolveCompilationUnit(testSource, library);
8522 }
8523 }
8524
8449 /** 8525 /**
8450 * Instances of the class `StaticTypeVerifier` verify that all of the nodes in a n AST 8526 * Instances of the class `StaticTypeVerifier` verify that all of the nodes in a n AST
8451 * structure that should have a static type associated with them do have a stati c type. 8527 * structure that should have a static type associated with them do have a stati c type.
8452 */ 8528 */
8453 class StaticTypeVerifier extends GeneralizingAstVisitor<Object> { 8529 class StaticTypeVerifier extends GeneralizingAstVisitor<Object> {
8454 /** 8530 /**
8455 * A list containing all of the AST Expression nodes that were not resolved. 8531 * A list containing all of the AST Expression nodes that were not resolved.
8456 */ 8532 */
8457 List<Expression> _unresolvedExpressions = new List<Expression>(); 8533 List<Expression> _unresolvedExpressions = new List<Expression>();
8458 8534
(...skipping 932 matching lines...) Expand 10 before | Expand all | Expand 10 after
9391 manager.enterScope(); 9467 manager.enterScope();
9392 JUnitTestCase.assertNull(manager.getType(ElementFactory.localVariableElement 2("v"))); 9468 JUnitTestCase.assertNull(manager.getType(ElementFactory.localVariableElement 2("v")));
9393 } 9469 }
9394 9470
9395 void test_getType_noScope() { 9471 void test_getType_noScope() {
9396 TypeOverrideManager manager = new TypeOverrideManager(); 9472 TypeOverrideManager manager = new TypeOverrideManager();
9397 JUnitTestCase.assertNull(manager.getType(ElementFactory.localVariableElement 2("v"))); 9473 JUnitTestCase.assertNull(manager.getType(ElementFactory.localVariableElement 2("v")));
9398 } 9474 }
9399 } 9475 }
9400 9476
9477
9401 class TypePropagationTest extends ResolverTestCase { 9478 class TypePropagationTest extends ResolverTestCase {
9402 void fail_mergePropagatedTypesAtJoinPoint_1() { 9479 void fail_mergePropagatedTypesAtJoinPoint_1() {
9403 // https://code.google.com/p/dart/issues/detail?id=19929 9480 // https://code.google.com/p/dart/issues/detail?id=19929
9404 _assertTypeOfMarkedExpression(EngineTestCase.createSource([ 9481 _assertTypeOfMarkedExpression(EngineTestCase.createSource([
9405 "f1(x) {", 9482 "f1(x) {",
9406 " var y = [];", 9483 " var y = [];",
9407 " if (x) {", 9484 " if (x) {",
9408 " y = 0;", 9485 " y = 0;",
9409 " } else {", 9486 " } else {",
9410 " y = '';", 9487 " y = '';",
(...skipping 1655 matching lines...) Expand 10 before | Expand all | Expand 10 after
11066 runReflectiveTests(ScopeTest); 11143 runReflectiveTests(ScopeTest);
11067 runReflectiveTests(DeclarationMatcherTest); 11144 runReflectiveTests(DeclarationMatcherTest);
11068 runReflectiveTests(ElementResolverTest); 11145 runReflectiveTests(ElementResolverTest);
11069 runReflectiveTests(IncrementalResolverTest); 11146 runReflectiveTests(IncrementalResolverTest);
11070 runReflectiveTests(InheritanceManagerTest); 11147 runReflectiveTests(InheritanceManagerTest);
11071 runReflectiveTests(LibraryElementBuilderTest); 11148 runReflectiveTests(LibraryElementBuilderTest);
11072 runReflectiveTests(LibraryResolver2Test); 11149 runReflectiveTests(LibraryResolver2Test);
11073 runReflectiveTests(LibraryResolverTest); 11150 runReflectiveTests(LibraryResolverTest);
11074 runReflectiveTests(LibraryTest); 11151 runReflectiveTests(LibraryTest);
11075 runReflectiveTests(StaticTypeAnalyzerTest); 11152 runReflectiveTests(StaticTypeAnalyzerTest);
11153 runReflectiveTests(StaticTypeAnalyzer2Test);
11076 runReflectiveTests(SubtypeManagerTest); 11154 runReflectiveTests(SubtypeManagerTest);
11077 runReflectiveTests(TypeOverrideManagerTest); 11155 runReflectiveTests(TypeOverrideManagerTest);
11078 runReflectiveTests(TypeProviderImplTest); 11156 runReflectiveTests(TypeProviderImplTest);
11079 runReflectiveTests(TypeResolverVisitorTest); 11157 runReflectiveTests(TypeResolverVisitorTest);
11080 runReflectiveTests(CheckedModeCompileTimeErrorCodeTest); 11158 runReflectiveTests(CheckedModeCompileTimeErrorCodeTest);
11081 runReflectiveTests(ErrorResolverTest); 11159 runReflectiveTests(ErrorResolverTest);
11082 runReflectiveTests(HintCodeTest); 11160 runReflectiveTests(HintCodeTest);
11083 runReflectiveTests(MemberMapTest); 11161 runReflectiveTests(MemberMapTest);
11084 runReflectiveTests(NonHintCodeTest); 11162 runReflectiveTests(NonHintCodeTest);
11085 runReflectiveTests(SimpleResolverTest); 11163 runReflectiveTests(SimpleResolverTest);
11086 runReflectiveTests(StrictModeTest); 11164 runReflectiveTests(StrictModeTest);
11087 runReflectiveTests(TypePropagationTest); 11165 runReflectiveTests(TypePropagationTest);
11088 } 11166 }
OLDNEW
« no previous file with comments | « pkg/analyzer/lib/src/generated/resolver.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698