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

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

Issue 1099953004: In constant evaluation, handle final vars initialized at declaration site. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 8 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 | Annotate | Revision Log
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 // This code was auto-generated, is not intended to be edited, and is subject to 5 // This code was auto-generated, is not intended to be edited, and is subject to
6 // significant change. Please see the README file for more information. 6 // significant change. Please see the README file for more information.
7 7
8 library engine.all_the_rest_test; 8 library engine.all_the_rest_test;
9 9
10 import 'dart:collection'; 10 import 'dart:collection';
(...skipping 833 matching lines...) Expand 10 before | Expand all | Expand 10 after
844 ConstantEvaluator evaluator = 844 ConstantEvaluator evaluator =
845 new ConstantEvaluator(source, analysisContext.typeProvider); 845 new ConstantEvaluator(source, analysisContext.typeProvider);
846 return evaluator.evaluate(variables[0].initializer); 846 return evaluator.evaluate(variables[0].initializer);
847 } 847 }
848 } 848 }
849 849
850 @reflectiveTest 850 @reflectiveTest
851 class ConstantFinderTest extends EngineTestCase { 851 class ConstantFinderTest extends EngineTestCase {
852 AstNode _node; 852 AstNode _node;
853 853
854 TypeProvider _typeProvider;
855
856 void setUp() {
857 super.setUp();
858 _typeProvider = new TestTypeProvider();
859 }
860
854 /** 861 /**
855 * Test an annotation that consists solely of an identifier (and hence 862 * Test an annotation that consists solely of an identifier (and hence
856 * represents a reference to a compile-time constant variable). 863 * represents a reference to a compile-time constant variable).
857 */ 864 */
858 void test_visitAnnotation_constantVariable() { 865 void test_visitAnnotation_constantVariable() {
859 _node = AstFactory.annotation(AstFactory.identifier3('x')); 866 _node = AstFactory.annotation(AstFactory.identifier3('x'));
860 expect(_findAnnotations(), contains(_node)); 867 expect(_findAnnotations(), contains(_node));
861 } 868 }
862 869
863 /** 870 /**
(...skipping 24 matching lines...) Expand all
888 void test_visitInstanceCreationExpression_nonConst() { 895 void test_visitInstanceCreationExpression_nonConst() {
889 _setupInstanceCreationExpression("A", false); 896 _setupInstanceCreationExpression("A", false);
890 expect(_findConstructorInvocations().isEmpty, isTrue); 897 expect(_findConstructorInvocations().isEmpty, isTrue);
891 } 898 }
892 899
893 void test_visitVariableDeclaration_const() { 900 void test_visitVariableDeclaration_const() {
894 VariableElement element = _setupVariableDeclaration("v", true, true); 901 VariableElement element = _setupVariableDeclaration("v", true, true);
895 expect(_findVariableDeclarations()[element], same(_node)); 902 expect(_findVariableDeclarations()[element], same(_node));
896 } 903 }
897 904
905 void test_visitVariableDeclaration_final_inClass() {
906 _setupFieldDeclaration('C', 'f', Keyword.FINAL);
907 expect(_findVariableDeclarations(), isEmpty);
908 }
909
910 void test_visitVariableDeclaration_final_inClassWithConstConstructor() {
911 VariableDeclaration field = _setupFieldDeclaration('C', 'f', Keyword.FINAL,
912 hasConstConstructor: true);
913 expect(_findVariableDeclarations()[field.element], same(field));
914 }
915
916 void test_visitVariableDeclaration_final_outsideClass() {
917 _setupVariableDeclaration('v', false, true, isFinal: true);
918 expect(_findVariableDeclarations(), isEmpty);
919 }
920
898 void test_visitVariableDeclaration_noInitializer() { 921 void test_visitVariableDeclaration_noInitializer() {
899 _setupVariableDeclaration("v", true, false); 922 _setupVariableDeclaration("v", true, false);
900 expect(_findVariableDeclarations().isEmpty, isTrue); 923 expect(_findVariableDeclarations().isEmpty, isTrue);
901 } 924 }
902 925
903 void test_visitVariableDeclaration_nonConst() { 926 void test_visitVariableDeclaration_nonConst() {
904 _setupVariableDeclaration("v", false, true); 927 _setupVariableDeclaration("v", false, true);
905 expect(_findVariableDeclarations().isEmpty, isTrue); 928 expect(_findVariableDeclarations().isEmpty, isTrue);
906 } 929 }
907 930
931 void test_visitVariableDeclaration_static_const_inClass() {
932 VariableDeclaration field =
933 _setupFieldDeclaration('C', 'f', Keyword.CONST, isStatic: true);
934 expect(_findVariableDeclarations()[field.element], same(field));
935 }
936
937 void test_visitVariableDeclaration_static_const_inClassWithConstConstructor() {
938 VariableDeclaration field = _setupFieldDeclaration('C', 'f', Keyword.CONST,
939 isStatic: true, hasConstConstructor: true);
940 expect(_findVariableDeclarations()[field.element], same(field));
941 }
942
943 void test_visitVariableDeclaration_static_final_inClassWithConstConstructor() {
944 _setupFieldDeclaration('C', 'f', Keyword.FINAL,
945 isStatic: true, hasConstConstructor: true);
946 expect(_findVariableDeclarations(), isEmpty);
947 }
948
949 void test_visitVariableDeclaration_uninitialized_final_inClassWithConstConstru ctor() {
950 _setupFieldDeclaration('C', 'f', Keyword.FINAL,
951 isInitialized: false, hasConstConstructor: true);
952 expect(_findVariableDeclarations(), isEmpty);
953 }
954
955 void test_visitVariableDeclaration_uninitialized_static_const_inClass() {
956 _setupFieldDeclaration('C', 'f', Keyword.CONST,
957 isStatic: true, isInitialized: false);
958 expect(_findVariableDeclarations(), isEmpty);
959 }
960
908 List<Annotation> _findAnnotations() { 961 List<Annotation> _findAnnotations() {
909 ConstantFinder finder = new ConstantFinder(); 962 ConstantFinder finder = new ConstantFinder();
910 _node.accept(finder); 963 _node.accept(finder);
911 List<Annotation> annotations = finder.annotations; 964 List<Annotation> annotations = finder.annotations;
912 expect(annotations, isNotNull); 965 expect(annotations, isNotNull);
913 return annotations; 966 return annotations;
914 } 967 }
915 968
916 Map<ConstructorElement, ConstructorDeclaration> _findConstantDeclarations() { 969 Map<ConstructorElement, ConstructorDeclaration> _findConstantDeclarations() {
917 ConstantFinder finder = new ConstantFinder(); 970 ConstantFinder finder = new ConstantFinder();
(...skipping 29 matching lines...) Expand all
947 AstFactory.formalParameterList(), null, 1000 AstFactory.formalParameterList(), null,
948 AstFactory.blockFunctionBody2()); 1001 AstFactory.blockFunctionBody2());
949 ClassElement classElement = ElementFactory.classElement2(name); 1002 ClassElement classElement = ElementFactory.classElement2(name);
950 ConstructorElement element = 1003 ConstructorElement element =
951 ElementFactory.constructorElement(classElement, name, isConst); 1004 ElementFactory.constructorElement(classElement, name, isConst);
952 constructorDeclaration.element = element; 1005 constructorDeclaration.element = element;
953 _node = constructorDeclaration; 1006 _node = constructorDeclaration;
954 return element; 1007 return element;
955 } 1008 }
956 1009
1010 VariableDeclaration _setupFieldDeclaration(
1011 String className, String fieldName, Keyword keyword,
1012 {bool isInitialized: true, bool isStatic: false,
1013 bool hasConstConstructor: false}) {
1014 VariableDeclaration variableDeclaration = isInitialized
1015 ? AstFactory.variableDeclaration2(fieldName, AstFactory.integer(0))
1016 : AstFactory.variableDeclaration(fieldName);
1017 VariableElement fieldElement = ElementFactory.fieldElement(fieldName,
1018 isStatic, keyword == Keyword.FINAL, keyword == Keyword.CONST,
1019 _typeProvider.intType);
1020 variableDeclaration.name.staticElement = fieldElement;
1021 FieldDeclaration fieldDeclaration = AstFactory.fieldDeclaration2(
1022 isStatic, keyword, <VariableDeclaration>[variableDeclaration]);
1023 ClassDeclaration classDeclaration =
1024 AstFactory.classDeclaration(null, className, null, null, null, null);
1025 classDeclaration.members.add(fieldDeclaration);
1026 _node = classDeclaration;
1027 ClassElementImpl classElement = ElementFactory.classElement2(className);
1028 classElement.fields = <FieldElement>[fieldElement];
1029 classDeclaration.name.staticElement = classElement;
1030 if (hasConstConstructor) {
1031 ConstructorDeclaration constructorDeclaration = AstFactory
1032 .constructorDeclaration2(Keyword.CONST, null,
1033 AstFactory.identifier3(className), null,
1034 AstFactory.formalParameterList(), null,
1035 AstFactory.blockFunctionBody2());
1036 classDeclaration.members.add(constructorDeclaration);
1037 ConstructorElement constructorElement =
1038 ElementFactory.constructorElement(classElement, '', true);
1039 constructorDeclaration.element = constructorElement;
1040 classElement.constructors = <ConstructorElement>[constructorElement];
1041 }
1042 return variableDeclaration;
1043 }
1044
957 void _setupInstanceCreationExpression(String name, bool isConst) { 1045 void _setupInstanceCreationExpression(String name, bool isConst) {
958 _node = AstFactory.instanceCreationExpression2( 1046 _node = AstFactory.instanceCreationExpression2(
959 isConst ? Keyword.CONST : null, 1047 isConst ? Keyword.CONST : null,
960 AstFactory.typeName3(AstFactory.identifier3(name))); 1048 AstFactory.typeName3(AstFactory.identifier3(name)));
961 } 1049 }
962 1050
963 VariableElement _setupVariableDeclaration( 1051 VariableElement _setupVariableDeclaration(
964 String name, bool isConst, bool isInitialized) { 1052 String name, bool isConst, bool isInitialized, {isFinal: false}) {
965 VariableDeclaration variableDeclaration = isInitialized 1053 VariableDeclaration variableDeclaration = isInitialized
966 ? AstFactory.variableDeclaration2(name, AstFactory.integer(0)) 1054 ? AstFactory.variableDeclaration2(name, AstFactory.integer(0))
967 : AstFactory.variableDeclaration(name); 1055 : AstFactory.variableDeclaration(name);
968 SimpleIdentifier identifier = variableDeclaration.name; 1056 SimpleIdentifier identifier = variableDeclaration.name;
969 VariableElement element = ElementFactory.localVariableElement(identifier); 1057 VariableElement element = ElementFactory.localVariableElement(identifier);
970 identifier.staticElement = element; 1058 identifier.staticElement = element;
971 AstFactory.variableDeclarationList2( 1059 Keyword keyword = isConst ? Keyword.CONST : isFinal ? Keyword.FINAL : null;
972 isConst ? Keyword.CONST : null, [variableDeclaration]); 1060 AstFactory.variableDeclarationList2(keyword, [variableDeclaration]);
973 _node = variableDeclaration; 1061 _node = variableDeclaration;
974 return element; 1062 return element;
975 } 1063 }
976 } 1064 }
977 1065
978 @reflectiveTest 1066 @reflectiveTest
979 class ConstantValueComputerTest extends ResolverTestCase { 1067 class ConstantValueComputerTest extends ResolverTestCase {
980 void test_annotation_constConstructor() { 1068 void test_annotation_constConstructor() {
981 CompilationUnit compilationUnit = resolveSource(r''' 1069 CompilationUnit compilationUnit = resolveSource(r'''
982 class A { 1070 class A {
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after
1338 class A { 1426 class A {
1339 const A() : x = 5; 1427 const A() : x = 5;
1340 final int x; 1428 final int x;
1341 } 1429 }
1342 class B extends A { 1430 class B extends A {
1343 const B(); 1431 const B();
1344 } 1432 }
1345 const B b = const B();'''); 1433 const B b = const B();''');
1346 } 1434 }
1347 1435
1436 void test_dependencyOnInitializedNonStaticConst() {
1437 // Even though non-static consts are not allowed by the language, we need
1438 // to handle them for error recovery purposes.
1439 // a depends on A() depends on A.x
1440 _assertProperDependencies('''
1441 class A {
1442 const A();
1443 const int x = 1;
1444 }
1445 const A a = const A();
1446 ''', [CompileTimeErrorCode.CONST_INSTANCE_FIELD]);
1447 }
1448
1449 void test_dependencyOnInitializedFinal() {
1450 // a depends on A() depends on A.x
1451 _assertProperDependencies('''
1452 class A {
1453 const A();
1454 final int x = 1;
1455 }
1456 const A a = const A();
1457 ''');
1458 }
1459
1348 void test_dependencyOnNonFactoryRedirect() { 1460 void test_dependencyOnNonFactoryRedirect() {
1349 // a depends on A.foo() depends on A.bar() 1461 // a depends on A.foo() depends on A.bar()
1350 _assertProperDependencies(r''' 1462 _assertProperDependencies(r'''
1351 const A a = const A.foo(); 1463 const A a = const A.foo();
1352 class A { 1464 class A {
1353 const A.foo() : this.bar(); 1465 const A.foo() : this.bar();
1354 const A.bar(); 1466 const A.bar();
1355 }'''); 1467 }''');
1356 } 1468 }
1357 1469
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
1423 const A a = const A();'''); 1535 const A a = const A();''');
1424 } 1536 }
1425 1537
1426 void test_dependencyOnVariable() { 1538 void test_dependencyOnVariable() {
1427 // x depends on y 1539 // x depends on y
1428 _assertProperDependencies(r''' 1540 _assertProperDependencies(r'''
1429 const x = y + 1; 1541 const x = y + 1;
1430 const y = 2;'''); 1542 const y = 2;''');
1431 } 1543 }
1432 1544
1545 void test_final_initialized_at_declaration() {
1546 CompilationUnit compilationUnit = resolveSource('''
1547 class A {
1548 final int i = 123;
1549 const A();
1550 }
1551
1552 const A a = const A();
1553 ''');
1554 EvaluationResultImpl result =
1555 _evaluateInstanceCreationExpression(compilationUnit, 'a');
1556 Map<String, DartObjectImpl> fields = _assertType(result, "A");
1557 expect(fields, hasLength(1));
1558 _assertIntField(fields, "i", 123);
1559 }
1560
1561 void test_non_static_const_initialized_at_declaration() {
1562 // Even though non-static consts are not allowed by the language, we need
1563 // to handle them for error recovery purposes.
1564 CompilationUnit compilationUnit = resolveSource('''
1565 class A {
1566 const int i = 123;
1567 const A();
1568 }
1569
1570 const A a = const A();
1571 ''');
1572 EvaluationResultImpl result =
1573 _evaluateInstanceCreationExpression(compilationUnit, 'a');
1574 Map<String, DartObjectImpl> fields = _assertType(result, "A");
1575 expect(fields, hasLength(1));
1576 _assertIntField(fields, "i", 123);
1577 }
1578
1433 void test_fromEnvironment_bool_default_false() { 1579 void test_fromEnvironment_bool_default_false() {
1434 expect(_assertValidBool(_check_fromEnvironment_bool(null, "false")), false); 1580 expect(_assertValidBool(_check_fromEnvironment_bool(null, "false")), false);
1435 } 1581 }
1436 1582
1437 void test_fromEnvironment_bool_default_overridden() { 1583 void test_fromEnvironment_bool_default_overridden() {
1438 expect( 1584 expect(
1439 _assertValidBool(_check_fromEnvironment_bool("false", "true")), false); 1585 _assertValidBool(_check_fromEnvironment_bool("false", "true")), false);
1440 } 1586 }
1441 1587
1442 void test_fromEnvironment_bool_default_parseError() { 1588 void test_fromEnvironment_bool_default_parseError() {
(...skipping 6859 matching lines...) Expand 10 before | Expand all | Expand 10 after
8302 TypeProvider typeProvider, DeclaredVariables declaredVariables) 8448 TypeProvider typeProvider, DeclaredVariables declaredVariables)
8303 : super(typeProvider, declaredVariables); 8449 : super(typeProvider, declaredVariables);
8304 8450
8305 @override 8451 @override
8306 void beforeComputeValue(AstNode constNode) { 8452 void beforeComputeValue(AstNode constNode) {
8307 super.beforeComputeValue(constNode); 8453 super.beforeComputeValue(constNode);
8308 _nodeBeingEvaluated = constNode; 8454 _nodeBeingEvaluated = constNode;
8309 } 8455 }
8310 8456
8311 @override 8457 @override
8458 void beforeGetFieldEvaluationResult(FieldElementImpl field) {
8459 super.beforeGetFieldEvaluationResult(field);
8460 // If we are getting the constant value for a node in the graph, make sure
8461 // we properly recorded the dependency.
8462 VariableDeclaration node = findVariableDeclaration(field);
8463 if (node != null && referenceGraph.nodes.contains(node)) {
8464 expect(referenceGraph.containsPath(_nodeBeingEvaluated, node), isTrue);
8465 }
8466 }
8467
8468 @override
8312 void beforeGetConstantInitializers(ConstructorElement constructor) { 8469 void beforeGetConstantInitializers(ConstructorElement constructor) {
8313 super.beforeGetConstantInitializers(constructor); 8470 super.beforeGetConstantInitializers(constructor);
8314 // If we are getting the constant initializers for a node in the graph, 8471 // If we are getting the constant initializers for a node in the graph,
8315 // make sure we properly recorded the dependency. 8472 // make sure we properly recorded the dependency.
8316 ConstructorDeclaration node = findConstructorDeclaration(constructor); 8473 ConstructorDeclaration node = findConstructorDeclaration(constructor);
8317 if (node != null && referenceGraph.nodes.contains(node)) { 8474 if (node != null && referenceGraph.nodes.contains(node)) {
8318 expect(referenceGraph.containsPath(_nodeBeingEvaluated, node), isTrue); 8475 expect(referenceGraph.containsPath(_nodeBeingEvaluated, node), isTrue);
8319 } 8476 }
8320 } 8477 }
8321 8478
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
8621 if (_expectedExternalScriptName == null) { 8778 if (_expectedExternalScriptName == null) {
8622 expect(scriptSource, isNull, reason: "script $scriptIndex"); 8779 expect(scriptSource, isNull, reason: "script $scriptIndex");
8623 } else { 8780 } else {
8624 expect(scriptSource, isNotNull, reason: "script $scriptIndex"); 8781 expect(scriptSource, isNotNull, reason: "script $scriptIndex");
8625 String actualExternalScriptName = scriptSource.shortName; 8782 String actualExternalScriptName = scriptSource.shortName;
8626 expect(actualExternalScriptName, _expectedExternalScriptName, 8783 expect(actualExternalScriptName, _expectedExternalScriptName,
8627 reason: "script $scriptIndex"); 8784 reason: "script $scriptIndex");
8628 } 8785 }
8629 } 8786 }
8630 } 8787 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698