OLD | NEW |
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 analyzer.test.generated.resolver_test; | 5 library analyzer.test.generated.resolver_test; |
6 | 6 |
7 import 'dart:collection'; | 7 import 'dart:collection'; |
8 | 8 |
9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/dart/ast/resolution_accessors.dart'; |
10 import 'package:analyzer/dart/ast/standard_ast_factory.dart'; | 11 import 'package:analyzer/dart/ast/standard_ast_factory.dart'; |
11 import 'package:analyzer/dart/ast/token.dart'; | 12 import 'package:analyzer/dart/ast/token.dart'; |
12 import 'package:analyzer/dart/ast/visitor.dart'; | 13 import 'package:analyzer/dart/ast/visitor.dart'; |
13 import 'package:analyzer/dart/element/element.dart'; | 14 import 'package:analyzer/dart/element/element.dart'; |
14 import 'package:analyzer/dart/element/type.dart'; | 15 import 'package:analyzer/dart/element/type.dart'; |
15 import 'package:analyzer/file_system/memory_file_system.dart'; | 16 import 'package:analyzer/file_system/memory_file_system.dart'; |
16 import 'package:analyzer/src/dart/element/builder.dart'; | 17 import 'package:analyzer/src/dart/element/builder.dart'; |
17 import 'package:analyzer/src/dart/element/element.dart'; | 18 import 'package:analyzer/src/dart/element/element.dart'; |
18 import 'package:analyzer/src/dart/element/type.dart'; | 19 import 'package:analyzer/src/dart/element/type.dart'; |
19 import 'package:analyzer/src/error/codes.dart'; | 20 import 'package:analyzer/src/error/codes.dart'; |
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 if (invalidlyPropagatedExpressionCount > 0) { | 473 if (invalidlyPropagatedExpressionCount > 0) { |
473 buffer.writeln("Incorrectly propagated "); | 474 buffer.writeln("Incorrectly propagated "); |
474 buffer.write(invalidlyPropagatedExpressionCount); | 475 buffer.write(invalidlyPropagatedExpressionCount); |
475 buffer.write(" of "); | 476 buffer.write(" of "); |
476 buffer.write(_propagatedExpressionCount); | 477 buffer.write(_propagatedExpressionCount); |
477 buffer.writeln(" expressions:"); | 478 buffer.writeln(" expressions:"); |
478 for (Expression expression in _invalidlyPropagatedExpressions) { | 479 for (Expression expression in _invalidlyPropagatedExpressions) { |
479 buffer.write(" "); | 480 buffer.write(" "); |
480 buffer.write(expression.toString()); | 481 buffer.write(expression.toString()); |
481 buffer.write(" ["); | 482 buffer.write(" ["); |
482 buffer.write(expression.staticType.displayName); | 483 buffer.write(staticTypeForExpression(expression).displayName); |
483 buffer.write(", "); | 484 buffer.write(", "); |
484 buffer.write(expression.propagatedType.displayName); | 485 buffer.write(propagatedTypeForExpression(expression).displayName); |
485 buffer.writeln("]"); | 486 buffer.writeln("]"); |
486 buffer.write(" "); | 487 buffer.write(" "); |
487 buffer.write(_getFileName(expression)); | 488 buffer.write(_getFileName(expression)); |
488 buffer.write(" : "); | 489 buffer.write(" : "); |
489 buffer.write(expression.offset); | 490 buffer.write(expression.offset); |
490 buffer.writeln(")"); | 491 buffer.writeln(")"); |
491 } | 492 } |
492 } | 493 } |
493 fail(buffer.toString()); | 494 fail(buffer.toString()); |
494 } | 495 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
531 @override | 532 @override |
532 Object visitLabel(Label node) => null; | 533 Object visitLabel(Label node) => null; |
533 | 534 |
534 @override | 535 @override |
535 Object visitLibraryIdentifier(LibraryIdentifier node) => null; | 536 Object visitLibraryIdentifier(LibraryIdentifier node) => null; |
536 | 537 |
537 @override | 538 @override |
538 Object visitPrefixedIdentifier(PrefixedIdentifier node) { | 539 Object visitPrefixedIdentifier(PrefixedIdentifier node) { |
539 // In cases where we have a prefixed identifier where the prefix is dynamic, | 540 // In cases where we have a prefixed identifier where the prefix is dynamic, |
540 // we don't want to assert that the node will have a type. | 541 // we don't want to assert that the node will have a type. |
541 if (node.staticType == null && node.prefix.staticType.isDynamic) { | 542 if (node.staticType == null && staticTypeForExpression(node.prefix).isDynami
c) { |
542 return null; | 543 return null; |
543 } | 544 } |
544 return super.visitPrefixedIdentifier(node); | 545 return super.visitPrefixedIdentifier(node); |
545 } | 546 } |
546 | 547 |
547 @override | 548 @override |
548 Object visitSimpleIdentifier(SimpleIdentifier node) { | 549 Object visitSimpleIdentifier(SimpleIdentifier node) { |
549 // In cases where identifiers are being used for something other than an | 550 // In cases where identifiers are being used for something other than an |
550 // expressions, then they can be ignored. | 551 // expressions, then they can be ignored. |
551 AstNode parent = node.parent; | 552 AstNode parent = node.parent; |
(...skipping 30 matching lines...) Expand all Loading... |
582 } | 583 } |
583 | 584 |
584 String _getFileName(AstNode node) { | 585 String _getFileName(AstNode node) { |
585 // TODO (jwren) there are two copies of this method, one here and one in | 586 // TODO (jwren) there are two copies of this method, one here and one in |
586 // ResolutionVerifier, they should be resolved into a single method | 587 // ResolutionVerifier, they should be resolved into a single method |
587 if (node != null) { | 588 if (node != null) { |
588 AstNode root = node.root; | 589 AstNode root = node.root; |
589 if (root is CompilationUnit) { | 590 if (root is CompilationUnit) { |
590 CompilationUnit rootCU = root; | 591 CompilationUnit rootCU = root; |
591 if (rootCU.element != null) { | 592 if (rootCU.element != null) { |
592 return rootCU.element.source.fullName; | 593 return elementForCompilationUnit(rootCU).source.fullName; |
593 } else { | 594 } else { |
594 return "<unknown file- CompilationUnit.getElement() returned null>"; | 595 return "<unknown file- CompilationUnit.getElement() returned null>"; |
595 } | 596 } |
596 } else { | 597 } else { |
597 return "<unknown file- CompilationUnit.getRoot() is not a CompilationUni
t>"; | 598 return "<unknown file- CompilationUnit.getRoot() is not a CompilationUni
t>"; |
598 } | 599 } |
599 } | 600 } |
600 return "<unknown file- ASTNode is null>"; | 601 return "<unknown file- ASTNode is null>"; |
601 } | 602 } |
602 } | 603 } |
(...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1078 return p; | 1079 return p; |
1079 } else { | 1080 } else { |
1080 return null; | 1081 return null; |
1081 } | 1082 } |
1082 }'''); | 1083 }'''); |
1083 LibraryElement library = resolve2(source); | 1084 LibraryElement library = resolve2(source); |
1084 assertNoErrors(source); | 1085 assertNoErrors(source); |
1085 verify([source]); | 1086 verify([source]); |
1086 CompilationUnit unit = resolveCompilationUnit(source, library); | 1087 CompilationUnit unit = resolveCompilationUnit(source, library); |
1087 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; | 1088 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; |
1088 InterfaceType typeA = classA.element.type; | 1089 InterfaceType typeA = elementForClassDeclaration(classA).type; |
1089 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; | 1090 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; |
1090 BlockFunctionBody body = | 1091 BlockFunctionBody body = |
1091 function.functionExpression.body as BlockFunctionBody; | 1092 function.functionExpression.body as BlockFunctionBody; |
1092 IfStatement ifStatement = body.block.statements[0] as IfStatement; | 1093 IfStatement ifStatement = body.block.statements[0] as IfStatement; |
1093 ReturnStatement statement = | 1094 ReturnStatement statement = |
1094 (ifStatement.thenStatement as Block).statements[0] as ReturnStatement; | 1095 (ifStatement.thenStatement as Block).statements[0] as ReturnStatement; |
1095 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; | 1096 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; |
1096 expect(variableName.propagatedType, same(typeA)); | 1097 expect(variableName.propagatedType, same(typeA)); |
1097 } | 1098 } |
1098 | 1099 |
1099 void test_assert() { | 1100 void test_assert() { |
1100 Source source = addSource(r''' | 1101 Source source = addSource(r''' |
1101 class A {} | 1102 class A {} |
1102 A f(var p) { | 1103 A f(var p) { |
1103 assert (p is A); | 1104 assert (p is A); |
1104 return p; | 1105 return p; |
1105 }'''); | 1106 }'''); |
1106 LibraryElement library = resolve2(source); | 1107 LibraryElement library = resolve2(source); |
1107 assertNoErrors(source); | 1108 assertNoErrors(source); |
1108 verify([source]); | 1109 verify([source]); |
1109 CompilationUnit unit = resolveCompilationUnit(source, library); | 1110 CompilationUnit unit = resolveCompilationUnit(source, library); |
1110 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; | 1111 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; |
1111 InterfaceType typeA = classA.element.type; | 1112 InterfaceType typeA = elementForClassDeclaration(classA).type; |
1112 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; | 1113 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; |
1113 BlockFunctionBody body = | 1114 BlockFunctionBody body = |
1114 function.functionExpression.body as BlockFunctionBody; | 1115 function.functionExpression.body as BlockFunctionBody; |
1115 ReturnStatement statement = body.block.statements[1] as ReturnStatement; | 1116 ReturnStatement statement = body.block.statements[1] as ReturnStatement; |
1116 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; | 1117 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; |
1117 expect(variableName.propagatedType, same(typeA)); | 1118 expect(variableName.propagatedType, same(typeA)); |
1118 } | 1119 } |
1119 | 1120 |
1120 void test_assignment() { | 1121 void test_assignment() { |
1121 Source source = addSource(r''' | 1122 Source source = addSource(r''' |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1196 main(CanvasElement canvas) { | 1197 main(CanvasElement canvas) { |
1197 var context = canvas.getContext('2d'); | 1198 var context = canvas.getContext('2d'); |
1198 }'''; | 1199 }'''; |
1199 Source source = addSource(code); | 1200 Source source = addSource(code); |
1200 LibraryElement library = resolve2(source); | 1201 LibraryElement library = resolve2(source); |
1201 assertNoErrors(source); | 1202 assertNoErrors(source); |
1202 verify([source]); | 1203 verify([source]); |
1203 CompilationUnit unit = resolveCompilationUnit(source, library); | 1204 CompilationUnit unit = resolveCompilationUnit(source, library); |
1204 SimpleIdentifier identifier = EngineTestCase.findNode( | 1205 SimpleIdentifier identifier = EngineTestCase.findNode( |
1205 unit, code, "context", (node) => node is SimpleIdentifier); | 1206 unit, code, "context", (node) => node is SimpleIdentifier); |
1206 expect(identifier.propagatedType.name, "CanvasRenderingContext2D"); | 1207 expect(propagatedTypeForExpression(identifier).name, "CanvasRenderingContext
2D"); |
1207 } | 1208 } |
1208 | 1209 |
1209 void test_forEach() { | 1210 void test_forEach() { |
1210 String code = r''' | 1211 String code = r''' |
1211 main() { | 1212 main() { |
1212 var list = <String> []; | 1213 var list = <String> []; |
1213 for (var e in list) { | 1214 for (var e in list) { |
1214 e; | 1215 e; |
1215 } | 1216 } |
1216 }'''; | 1217 }'''; |
(...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1649 Source source = addSource(r''' | 1650 Source source = addSource(r''' |
1650 class A {} | 1651 class A {} |
1651 A f(var p) { | 1652 A f(var p) { |
1652 return (p is A) ? p : null; | 1653 return (p is A) ? p : null; |
1653 }'''); | 1654 }'''); |
1654 LibraryElement library = resolve2(source); | 1655 LibraryElement library = resolve2(source); |
1655 assertNoErrors(source); | 1656 assertNoErrors(source); |
1656 verify([source]); | 1657 verify([source]); |
1657 CompilationUnit unit = resolveCompilationUnit(source, library); | 1658 CompilationUnit unit = resolveCompilationUnit(source, library); |
1658 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; | 1659 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; |
1659 InterfaceType typeA = classA.element.type; | 1660 InterfaceType typeA = elementForClassDeclaration(classA).type; |
1660 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; | 1661 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; |
1661 BlockFunctionBody body = | 1662 BlockFunctionBody body = |
1662 function.functionExpression.body as BlockFunctionBody; | 1663 function.functionExpression.body as BlockFunctionBody; |
1663 ReturnStatement statement = body.block.statements[0] as ReturnStatement; | 1664 ReturnStatement statement = body.block.statements[0] as ReturnStatement; |
1664 ConditionalExpression conditional = | 1665 ConditionalExpression conditional = |
1665 statement.expression as ConditionalExpression; | 1666 statement.expression as ConditionalExpression; |
1666 SimpleIdentifier variableName = | 1667 SimpleIdentifier variableName = |
1667 conditional.thenExpression as SimpleIdentifier; | 1668 conditional.thenExpression as SimpleIdentifier; |
1668 expect(variableName.propagatedType, same(typeA)); | 1669 expect(variableName.propagatedType, same(typeA)); |
1669 } | 1670 } |
1670 | 1671 |
1671 void test_is_if() { | 1672 void test_is_if() { |
1672 Source source = addSource(r''' | 1673 Source source = addSource(r''' |
1673 class A {} | 1674 class A {} |
1674 A f(var p) { | 1675 A f(var p) { |
1675 if (p is A) { | 1676 if (p is A) { |
1676 return p; | 1677 return p; |
1677 } else { | 1678 } else { |
1678 return null; | 1679 return null; |
1679 } | 1680 } |
1680 }'''); | 1681 }'''); |
1681 LibraryElement library = resolve2(source); | 1682 LibraryElement library = resolve2(source); |
1682 assertNoErrors(source); | 1683 assertNoErrors(source); |
1683 verify([source]); | 1684 verify([source]); |
1684 CompilationUnit unit = resolveCompilationUnit(source, library); | 1685 CompilationUnit unit = resolveCompilationUnit(source, library); |
1685 // prepare A | 1686 // prepare A |
1686 InterfaceType typeA; | 1687 InterfaceType typeA; |
1687 { | 1688 { |
1688 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; | 1689 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; |
1689 typeA = classA.element.type; | 1690 typeA = elementForClassDeclaration(classA).type; |
1690 } | 1691 } |
1691 // verify "f" | 1692 // verify "f" |
1692 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; | 1693 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; |
1693 BlockFunctionBody body = | 1694 BlockFunctionBody body = |
1694 function.functionExpression.body as BlockFunctionBody; | 1695 function.functionExpression.body as BlockFunctionBody; |
1695 IfStatement ifStatement = body.block.statements[0] as IfStatement; | 1696 IfStatement ifStatement = body.block.statements[0] as IfStatement; |
1696 // "p is A" | 1697 // "p is A" |
1697 { | 1698 { |
1698 IsExpression isExpression = ifStatement.condition; | 1699 IsExpression isExpression = ifStatement.condition; |
1699 SimpleIdentifier variableName = isExpression.expression; | 1700 SimpleIdentifier variableName = isExpression.expression; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1742 return p; | 1743 return p; |
1743 } else { | 1744 } else { |
1744 return null; | 1745 return null; |
1745 } | 1746 } |
1746 }'''); | 1747 }'''); |
1747 LibraryElement library = resolve2(source); | 1748 LibraryElement library = resolve2(source); |
1748 assertNoErrors(source); | 1749 assertNoErrors(source); |
1749 verify([source]); | 1750 verify([source]); |
1750 CompilationUnit unit = resolveCompilationUnit(source, library); | 1751 CompilationUnit unit = resolveCompilationUnit(source, library); |
1751 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; | 1752 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; |
1752 InterfaceType typeA = classA.element.type; | 1753 InterfaceType typeA = elementForClassDeclaration(classA).type; |
1753 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; | 1754 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; |
1754 BlockFunctionBody body = | 1755 BlockFunctionBody body = |
1755 function.functionExpression.body as BlockFunctionBody; | 1756 function.functionExpression.body as BlockFunctionBody; |
1756 IfStatement ifStatement = body.block.statements[0] as IfStatement; | 1757 IfStatement ifStatement = body.block.statements[0] as IfStatement; |
1757 ReturnStatement statement = | 1758 ReturnStatement statement = |
1758 (ifStatement.thenStatement as Block).statements[0] as ReturnStatement; | 1759 (ifStatement.thenStatement as Block).statements[0] as ReturnStatement; |
1759 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; | 1760 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; |
1760 expect(variableName.propagatedType, same(typeA)); | 1761 expect(variableName.propagatedType, same(typeA)); |
1761 } | 1762 } |
1762 | 1763 |
1763 void test_is_postConditional() { | 1764 void test_is_postConditional() { |
1764 Source source = addSource(r''' | 1765 Source source = addSource(r''' |
1765 class A {} | 1766 class A {} |
1766 A f(var p) { | 1767 A f(var p) { |
1767 A a = (p is A) ? p : throw null; | 1768 A a = (p is A) ? p : throw null; |
1768 return p; | 1769 return p; |
1769 }'''); | 1770 }'''); |
1770 LibraryElement library = resolve2(source); | 1771 LibraryElement library = resolve2(source); |
1771 assertNoErrors(source); | 1772 assertNoErrors(source); |
1772 verify([source]); | 1773 verify([source]); |
1773 CompilationUnit unit = resolveCompilationUnit(source, library); | 1774 CompilationUnit unit = resolveCompilationUnit(source, library); |
1774 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; | 1775 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; |
1775 InterfaceType typeA = classA.element.type; | 1776 InterfaceType typeA = elementForClassDeclaration(classA).type; |
1776 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; | 1777 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; |
1777 BlockFunctionBody body = | 1778 BlockFunctionBody body = |
1778 function.functionExpression.body as BlockFunctionBody; | 1779 function.functionExpression.body as BlockFunctionBody; |
1779 ReturnStatement statement = body.block.statements[1] as ReturnStatement; | 1780 ReturnStatement statement = body.block.statements[1] as ReturnStatement; |
1780 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; | 1781 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; |
1781 expect(variableName.propagatedType, same(typeA)); | 1782 expect(variableName.propagatedType, same(typeA)); |
1782 } | 1783 } |
1783 | 1784 |
1784 void test_is_postIf() { | 1785 void test_is_postIf() { |
1785 Source source = addSource(r''' | 1786 Source source = addSource(r''' |
1786 class A {} | 1787 class A {} |
1787 A f(var p) { | 1788 A f(var p) { |
1788 if (p is A) { | 1789 if (p is A) { |
1789 A a = p; | 1790 A a = p; |
1790 } else { | 1791 } else { |
1791 return null; | 1792 return null; |
1792 } | 1793 } |
1793 return p; | 1794 return p; |
1794 }'''); | 1795 }'''); |
1795 LibraryElement library = resolve2(source); | 1796 LibraryElement library = resolve2(source); |
1796 assertNoErrors(source); | 1797 assertNoErrors(source); |
1797 verify([source]); | 1798 verify([source]); |
1798 CompilationUnit unit = resolveCompilationUnit(source, library); | 1799 CompilationUnit unit = resolveCompilationUnit(source, library); |
1799 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; | 1800 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; |
1800 InterfaceType typeA = classA.element.type; | 1801 InterfaceType typeA = elementForClassDeclaration(classA).type; |
1801 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; | 1802 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; |
1802 BlockFunctionBody body = | 1803 BlockFunctionBody body = |
1803 function.functionExpression.body as BlockFunctionBody; | 1804 function.functionExpression.body as BlockFunctionBody; |
1804 ReturnStatement statement = body.block.statements[1] as ReturnStatement; | 1805 ReturnStatement statement = body.block.statements[1] as ReturnStatement; |
1805 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; | 1806 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; |
1806 expect(variableName.propagatedType, same(typeA)); | 1807 expect(variableName.propagatedType, same(typeA)); |
1807 } | 1808 } |
1808 | 1809 |
1809 void test_is_subclass() { | 1810 void test_is_subclass() { |
1810 Source source = addSource(r''' | 1811 Source source = addSource(r''' |
(...skipping 28 matching lines...) Expand all Loading... |
1839 while (p is A) { | 1840 while (p is A) { |
1840 return p; | 1841 return p; |
1841 } | 1842 } |
1842 return p; | 1843 return p; |
1843 }'''); | 1844 }'''); |
1844 LibraryElement library = resolve2(source); | 1845 LibraryElement library = resolve2(source); |
1845 assertNoErrors(source); | 1846 assertNoErrors(source); |
1846 verify([source]); | 1847 verify([source]); |
1847 CompilationUnit unit = resolveCompilationUnit(source, library); | 1848 CompilationUnit unit = resolveCompilationUnit(source, library); |
1848 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; | 1849 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; |
1849 InterfaceType typeA = classA.element.type; | 1850 InterfaceType typeA = elementForClassDeclaration(classA).type; |
1850 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; | 1851 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; |
1851 BlockFunctionBody body = | 1852 BlockFunctionBody body = |
1852 function.functionExpression.body as BlockFunctionBody; | 1853 function.functionExpression.body as BlockFunctionBody; |
1853 WhileStatement whileStatement = body.block.statements[0] as WhileStatement; | 1854 WhileStatement whileStatement = body.block.statements[0] as WhileStatement; |
1854 ReturnStatement statement = | 1855 ReturnStatement statement = |
1855 (whileStatement.body as Block).statements[0] as ReturnStatement; | 1856 (whileStatement.body as Block).statements[0] as ReturnStatement; |
1856 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; | 1857 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; |
1857 expect(variableName.propagatedType, same(typeA)); | 1858 expect(variableName.propagatedType, same(typeA)); |
1858 } | 1859 } |
1859 | 1860 |
1860 void test_isNot_conditional() { | 1861 void test_isNot_conditional() { |
1861 Source source = addSource(r''' | 1862 Source source = addSource(r''' |
1862 class A {} | 1863 class A {} |
1863 A f(var p) { | 1864 A f(var p) { |
1864 return (p is! A) ? null : p; | 1865 return (p is! A) ? null : p; |
1865 }'''); | 1866 }'''); |
1866 LibraryElement library = resolve2(source); | 1867 LibraryElement library = resolve2(source); |
1867 assertNoErrors(source); | 1868 assertNoErrors(source); |
1868 verify([source]); | 1869 verify([source]); |
1869 CompilationUnit unit = resolveCompilationUnit(source, library); | 1870 CompilationUnit unit = resolveCompilationUnit(source, library); |
1870 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; | 1871 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; |
1871 InterfaceType typeA = classA.element.type; | 1872 InterfaceType typeA = elementForClassDeclaration(classA).type; |
1872 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; | 1873 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; |
1873 BlockFunctionBody body = | 1874 BlockFunctionBody body = |
1874 function.functionExpression.body as BlockFunctionBody; | 1875 function.functionExpression.body as BlockFunctionBody; |
1875 ReturnStatement statement = body.block.statements[0] as ReturnStatement; | 1876 ReturnStatement statement = body.block.statements[0] as ReturnStatement; |
1876 ConditionalExpression conditional = | 1877 ConditionalExpression conditional = |
1877 statement.expression as ConditionalExpression; | 1878 statement.expression as ConditionalExpression; |
1878 SimpleIdentifier variableName = | 1879 SimpleIdentifier variableName = |
1879 conditional.elseExpression as SimpleIdentifier; | 1880 conditional.elseExpression as SimpleIdentifier; |
1880 expect(variableName.propagatedType, same(typeA)); | 1881 expect(variableName.propagatedType, same(typeA)); |
1881 } | 1882 } |
1882 | 1883 |
1883 void test_isNot_if() { | 1884 void test_isNot_if() { |
1884 Source source = addSource(r''' | 1885 Source source = addSource(r''' |
1885 class A {} | 1886 class A {} |
1886 A f(var p) { | 1887 A f(var p) { |
1887 if (p is! A) { | 1888 if (p is! A) { |
1888 return null; | 1889 return null; |
1889 } else { | 1890 } else { |
1890 return p; | 1891 return p; |
1891 } | 1892 } |
1892 }'''); | 1893 }'''); |
1893 LibraryElement library = resolve2(source); | 1894 LibraryElement library = resolve2(source); |
1894 assertNoErrors(source); | 1895 assertNoErrors(source); |
1895 verify([source]); | 1896 verify([source]); |
1896 CompilationUnit unit = resolveCompilationUnit(source, library); | 1897 CompilationUnit unit = resolveCompilationUnit(source, library); |
1897 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; | 1898 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; |
1898 InterfaceType typeA = classA.element.type; | 1899 InterfaceType typeA = elementForClassDeclaration(classA).type; |
1899 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; | 1900 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; |
1900 BlockFunctionBody body = | 1901 BlockFunctionBody body = |
1901 function.functionExpression.body as BlockFunctionBody; | 1902 function.functionExpression.body as BlockFunctionBody; |
1902 IfStatement ifStatement = body.block.statements[0] as IfStatement; | 1903 IfStatement ifStatement = body.block.statements[0] as IfStatement; |
1903 ReturnStatement statement = | 1904 ReturnStatement statement = |
1904 (ifStatement.elseStatement as Block).statements[0] as ReturnStatement; | 1905 (ifStatement.elseStatement as Block).statements[0] as ReturnStatement; |
1905 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; | 1906 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; |
1906 expect(variableName.propagatedType, same(typeA)); | 1907 expect(variableName.propagatedType, same(typeA)); |
1907 } | 1908 } |
1908 | 1909 |
1909 void test_isNot_if_logicalOr() { | 1910 void test_isNot_if_logicalOr() { |
1910 Source source = addSource(r''' | 1911 Source source = addSource(r''' |
1911 class A {} | 1912 class A {} |
1912 A f(var p) { | 1913 A f(var p) { |
1913 if (p is! A || null == p) { | 1914 if (p is! A || null == p) { |
1914 return null; | 1915 return null; |
1915 } else { | 1916 } else { |
1916 return p; | 1917 return p; |
1917 } | 1918 } |
1918 }'''); | 1919 }'''); |
1919 LibraryElement library = resolve2(source); | 1920 LibraryElement library = resolve2(source); |
1920 assertNoErrors(source); | 1921 assertNoErrors(source); |
1921 CompilationUnit unit = resolveCompilationUnit(source, library); | 1922 CompilationUnit unit = resolveCompilationUnit(source, library); |
1922 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; | 1923 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; |
1923 InterfaceType typeA = classA.element.type; | 1924 InterfaceType typeA = elementForClassDeclaration(classA).type; |
1924 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; | 1925 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; |
1925 BlockFunctionBody body = | 1926 BlockFunctionBody body = |
1926 function.functionExpression.body as BlockFunctionBody; | 1927 function.functionExpression.body as BlockFunctionBody; |
1927 IfStatement ifStatement = body.block.statements[0] as IfStatement; | 1928 IfStatement ifStatement = body.block.statements[0] as IfStatement; |
1928 ReturnStatement statement = | 1929 ReturnStatement statement = |
1929 (ifStatement.elseStatement as Block).statements[0] as ReturnStatement; | 1930 (ifStatement.elseStatement as Block).statements[0] as ReturnStatement; |
1930 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; | 1931 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; |
1931 expect(variableName.propagatedType, same(typeA)); | 1932 expect(variableName.propagatedType, same(typeA)); |
1932 } | 1933 } |
1933 | 1934 |
1934 void test_isNot_postConditional() { | 1935 void test_isNot_postConditional() { |
1935 Source source = addSource(r''' | 1936 Source source = addSource(r''' |
1936 class A {} | 1937 class A {} |
1937 A f(var p) { | 1938 A f(var p) { |
1938 A a = (p is! A) ? throw null : p; | 1939 A a = (p is! A) ? throw null : p; |
1939 return p; | 1940 return p; |
1940 }'''); | 1941 }'''); |
1941 LibraryElement library = resolve2(source); | 1942 LibraryElement library = resolve2(source); |
1942 assertNoErrors(source); | 1943 assertNoErrors(source); |
1943 verify([source]); | 1944 verify([source]); |
1944 CompilationUnit unit = resolveCompilationUnit(source, library); | 1945 CompilationUnit unit = resolveCompilationUnit(source, library); |
1945 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; | 1946 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; |
1946 InterfaceType typeA = classA.element.type; | 1947 InterfaceType typeA = elementForClassDeclaration(classA).type; |
1947 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; | 1948 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; |
1948 BlockFunctionBody body = | 1949 BlockFunctionBody body = |
1949 function.functionExpression.body as BlockFunctionBody; | 1950 function.functionExpression.body as BlockFunctionBody; |
1950 ReturnStatement statement = body.block.statements[1] as ReturnStatement; | 1951 ReturnStatement statement = body.block.statements[1] as ReturnStatement; |
1951 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; | 1952 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; |
1952 expect(variableName.propagatedType, same(typeA)); | 1953 expect(variableName.propagatedType, same(typeA)); |
1953 } | 1954 } |
1954 | 1955 |
1955 void test_isNot_postIf() { | 1956 void test_isNot_postIf() { |
1956 Source source = addSource(r''' | 1957 Source source = addSource(r''' |
1957 class A {} | 1958 class A {} |
1958 A f(var p) { | 1959 A f(var p) { |
1959 if (p is! A) { | 1960 if (p is! A) { |
1960 return null; | 1961 return null; |
1961 } | 1962 } |
1962 return p; | 1963 return p; |
1963 }'''); | 1964 }'''); |
1964 LibraryElement library = resolve2(source); | 1965 LibraryElement library = resolve2(source); |
1965 assertNoErrors(source); | 1966 assertNoErrors(source); |
1966 verify([source]); | 1967 verify([source]); |
1967 CompilationUnit unit = resolveCompilationUnit(source, library); | 1968 CompilationUnit unit = resolveCompilationUnit(source, library); |
1968 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; | 1969 ClassDeclaration classA = unit.declarations[0] as ClassDeclaration; |
1969 InterfaceType typeA = classA.element.type; | 1970 InterfaceType typeA = elementForClassDeclaration(classA).type; |
1970 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; | 1971 FunctionDeclaration function = unit.declarations[1] as FunctionDeclaration; |
1971 BlockFunctionBody body = | 1972 BlockFunctionBody body = |
1972 function.functionExpression.body as BlockFunctionBody; | 1973 function.functionExpression.body as BlockFunctionBody; |
1973 ReturnStatement statement = body.block.statements[1] as ReturnStatement; | 1974 ReturnStatement statement = body.block.statements[1] as ReturnStatement; |
1974 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; | 1975 SimpleIdentifier variableName = statement.expression as SimpleIdentifier; |
1975 expect(variableName.propagatedType, same(typeA)); | 1976 expect(variableName.propagatedType, same(typeA)); |
1976 } | 1977 } |
1977 | 1978 |
1978 void test_issue20904BuggyTypePromotionAtIfJoin_5() { | 1979 void test_issue20904BuggyTypePromotionAtIfJoin_5() { |
1979 // https://code.google.com/p/dart/issues/detail?id=20904 | 1980 // https://code.google.com/p/dart/issues/detail?id=20904 |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2409 }'''); | 2410 }'''); |
2410 LibraryElement library = resolve2(source); | 2411 LibraryElement library = resolve2(source); |
2411 assertNoErrors(source); | 2412 assertNoErrors(source); |
2412 verify([source]); | 2413 verify([source]); |
2413 CompilationUnit unit = resolveCompilationUnit(source, library); | 2414 CompilationUnit unit = resolveCompilationUnit(source, library); |
2414 FunctionDeclaration main = unit.declarations[0] as FunctionDeclaration; | 2415 FunctionDeclaration main = unit.declarations[0] as FunctionDeclaration; |
2415 BlockFunctionBody body = main.functionExpression.body as BlockFunctionBody; | 2416 BlockFunctionBody body = main.functionExpression.body as BlockFunctionBody; |
2416 ReturnStatement statement = body.block.statements[11] as ReturnStatement; | 2417 ReturnStatement statement = body.block.statements[11] as ReturnStatement; |
2417 NodeList<Expression> elements = | 2418 NodeList<Expression> elements = |
2418 (statement.expression as ListLiteral).elements; | 2419 (statement.expression as ListLiteral).elements; |
2419 expect(elements[0].propagatedType.name, "AnchorElement"); | 2420 expect(propagatedTypeForExpression(elements[0]).name, "AnchorElement"); |
2420 expect(elements[1].propagatedType.name, "AnchorElement"); | 2421 expect(propagatedTypeForExpression(elements[1]).name, "AnchorElement"); |
2421 expect(elements[2].propagatedType.name, "BodyElement"); | 2422 expect(propagatedTypeForExpression(elements[2]).name, "BodyElement"); |
2422 expect(elements[3].propagatedType.name, "ButtonElement"); | 2423 expect(propagatedTypeForExpression(elements[3]).name, "ButtonElement"); |
2423 expect(elements[4].propagatedType.name, "DivElement"); | 2424 expect(propagatedTypeForExpression(elements[4]).name, "DivElement"); |
2424 expect(elements[5].propagatedType.name, "InputElement"); | 2425 expect(propagatedTypeForExpression(elements[5]).name, "InputElement"); |
2425 expect(elements[6].propagatedType.name, "SelectElement"); | 2426 expect(propagatedTypeForExpression(elements[6]).name, "SelectElement"); |
2426 expect(elements[7].propagatedType.name, "DivElement"); | 2427 expect(propagatedTypeForExpression(elements[7]).name, "DivElement"); |
2427 expect(elements[8].propagatedType.name, "Element"); | 2428 expect(propagatedTypeForExpression(elements[8]).name, "Element"); |
2428 expect(elements[9].propagatedType.name, "Element"); | 2429 expect(propagatedTypeForExpression(elements[9]).name, "Element"); |
2429 expect(elements[10].propagatedType.name, "Element"); | 2430 expect(propagatedTypeForExpression(elements[10]).name, "Element"); |
2430 } | 2431 } |
2431 } | 2432 } |
2432 | 2433 |
2433 @reflectiveTest | 2434 @reflectiveTest |
2434 class TypeProviderImplTest extends EngineTestCase { | 2435 class TypeProviderImplTest extends EngineTestCase { |
2435 void test_creation() { | 2436 void test_creation() { |
2436 // | 2437 // |
2437 // Create a mock library element with the types expected to be in dart:core. | 2438 // Create a mock library element with the types expected to be in dart:core. |
2438 // We cannot use either ElementFactory or TestTypeProvider (which uses | 2439 // We cannot use either ElementFactory or TestTypeProvider (which uses |
2439 // ElementFactory) because we side-effect the elements in ways that would | 2440 // ElementFactory) because we side-effect the elements in ways that would |
(...skipping 1194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3634 */ | 3635 */ |
3635 class _StaleElement extends ElementImpl { | 3636 class _StaleElement extends ElementImpl { |
3636 _StaleElement() : super("_StaleElement", -1); | 3637 _StaleElement() : super("_StaleElement", -1); |
3637 | 3638 |
3638 @override | 3639 @override |
3639 get kind => throw "_StaleElement's kind shouldn't be accessed"; | 3640 get kind => throw "_StaleElement's kind shouldn't be accessed"; |
3640 | 3641 |
3641 @override | 3642 @override |
3642 accept(_) => throw "_StaleElement shouldn't be visited"; | 3643 accept(_) => throw "_StaleElement shouldn't be visited"; |
3643 } | 3644 } |
OLD | NEW |