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

Side by Side Diff: pkg/compiler/lib/src/typechecker.dart

Issue 710343002: Check enums in switch cases. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Updated cf. comments + process deferred actions in test 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/compiler/lib/src/resolution/members.dart ('k') | pkg/compiler/lib/src/warnings.dart » ('j') | 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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 part of dart2js; 5 part of dart2js;
6 6
7 class TypeCheckerTask extends CompilerTask { 7 class TypeCheckerTask extends CompilerTask {
8 TypeCheckerTask(Compiler compiler) : super(compiler); 8 TypeCheckerTask(Compiler compiler) : super(compiler);
9 String get name => "Type checker"; 9 String get name => "Type checker";
10 10
(...skipping 1718 matching lines...) Expand 10 before | Expand all | Expand 10 after
1729 // unresolved methods. For instance [: foo(a: 42); :] where 'foo' is neither 1729 // unresolved methods. For instance [: foo(a: 42); :] where 'foo' is neither
1730 // found in the enclosing scope nor through lookup on 'this' or 1730 // found in the enclosing scope nor through lookup on 'this' or
1731 // [: x.foo(b: 42); :] where 'foo' cannot be not found through lookup on 1731 // [: x.foo(b: 42); :] where 'foo' cannot be not found through lookup on
1732 // the static type of 'x'. 1732 // the static type of 'x'.
1733 return analyze(node.expression); 1733 return analyze(node.expression);
1734 } 1734 }
1735 1735
1736 visitSwitchStatement(SwitchStatement node) { 1736 visitSwitchStatement(SwitchStatement node) {
1737 // TODO(johnniwinther): Handle reachability based on reachability of 1737 // TODO(johnniwinther): Handle reachability based on reachability of
1738 // switch cases. 1738 // switch cases.
1739 // TODO(johnniwinther): Provide hint of duplicate case constants.
1739 1740
1740 DartType expressionType = analyze(node.expression); 1741 DartType expressionType = analyze(node.expression);
1741 1742
1742 // Check that all the case expressions are assignable to the expression. 1743 // Check that all the case expressions are assignable to the expression.
1744 bool hasDefaultCase = false;
1743 for (SwitchCase switchCase in node.cases) { 1745 for (SwitchCase switchCase in node.cases) {
1746 if (switchCase.isDefaultCase) {
1747 hasDefaultCase = true;
1748 }
1744 for (Node labelOrCase in switchCase.labelsAndCases) { 1749 for (Node labelOrCase in switchCase.labelsAndCases) {
1745 CaseMatch caseMatch = labelOrCase.asCaseMatch(); 1750 CaseMatch caseMatch = labelOrCase.asCaseMatch();
1746 if (caseMatch == null) continue; 1751 if (caseMatch == null) continue;
1747 1752
1748 DartType caseType = analyze(caseMatch.expression); 1753 DartType caseType = analyze(caseMatch.expression);
1749 checkAssignable(caseMatch, expressionType, caseType); 1754 checkAssignable(caseMatch, expressionType, caseType);
1750 } 1755 }
1751 1756
1752 analyze(switchCase); 1757 analyze(switchCase);
1753 } 1758 }
1754 1759
1760 if (!hasDefaultCase && expressionType.isEnumType) {
1761 compiler.enqueuer.resolution.addDeferredAction(
1762 elements.analyzedElement, () {
1763 Map<ConstantValue, FieldElement> enumValues =
1764 <ConstantValue, FieldElement>{};
1765 List<FieldElement> unreferencedFields = <FieldElement>[];
1766 EnumClassElement enumClass = expressionType.element;
1767 enumClass.enumValues.forEach((FieldElement field) {
1768 ConstantExpression constantExpression =
1769 compiler.constants.getConstantForVariable(field);
1770 if (constantExpression == null) {
1771 // The field might not have been resolved.
1772 unreferencedFields.add(field);
1773 } else {
1774 enumValues[constantExpression.value] = field;
1775 }
1776 });
1777
1778 for (SwitchCase switchCase in node.cases) {
1779 for (Node labelOrCase in switchCase.labelsAndCases) {
1780 CaseMatch caseMatch = labelOrCase.asCaseMatch();
1781 if (caseMatch != null) {
1782 ConstantExpression caseConstant =
1783 compiler.resolver.constantCompiler.compileNode(
1784 caseMatch.expression, elements);
1785 enumValues.remove(caseConstant.value);
1786 }
1787 }
1788 }
1789 unreferencedFields.addAll(enumValues.values);
1790 if (!unreferencedFields.isEmpty) {
1791 compiler.reportWarning(node, MessageKind.MISSING_ENUM_CASES,
1792 {'enumType': expressionType,
1793 'enumValues': unreferencedFields.map((e) => e.name).join(', ')});
1794 }
1795 });
1796 }
1797
1755 return const StatementType(); 1798 return const StatementType();
1756 } 1799 }
1757 1800
1758 visitSwitchCase(SwitchCase node) { 1801 visitSwitchCase(SwitchCase node) {
1759 return analyze(node.statements); 1802 return analyze(node.statements);
1760 } 1803 }
1761 1804
1762 visitTryStatement(TryStatement node) { 1805 visitTryStatement(TryStatement node) {
1763 // TODO(johnniwinther): Use reachability information of try-block, 1806 // TODO(johnniwinther): Use reachability information of try-block,
1764 // catch-blocks and finally-block to compute the whether the try statement 1807 // catch-blocks and finally-block to compute the whether the try statement
(...skipping 12 matching lines...) Expand all
1777 1820
1778 visitTypedef(Typedef node) { 1821 visitTypedef(Typedef node) {
1779 // Do not typecheck [Typedef] nodes. 1822 // Do not typecheck [Typedef] nodes.
1780 } 1823 }
1781 1824
1782 visitNode(Node node) { 1825 visitNode(Node node) {
1783 compiler.internalError(node, 1826 compiler.internalError(node,
1784 'Unexpected node ${node.getObjectDescription()} in the type checker.'); 1827 'Unexpected node ${node.getObjectDescription()} in the type checker.');
1785 } 1828 }
1786 } 1829 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/resolution/members.dart ('k') | pkg/compiler/lib/src/warnings.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698