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

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: 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
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 1722 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 1739
1740 DartType expressionType = analyze(node.expression); 1740 DartType expressionType = analyze(node.expression);
1741 1741
1742 // Check that all the case expressions are assignable to the expression. 1742 // Check that all the case expressions are assignable to the expression.
1743 bool hasDefaultCase = false;
1743 for (SwitchCase switchCase in node.cases) { 1744 for (SwitchCase switchCase in node.cases) {
1745 if (switchCase.isDefaultCase) {
1746 hasDefaultCase = true;
1747 }
1744 for (Node labelOrCase in switchCase.labelsAndCases) { 1748 for (Node labelOrCase in switchCase.labelsAndCases) {
1745 CaseMatch caseMatch = labelOrCase.asCaseMatch(); 1749 CaseMatch caseMatch = labelOrCase.asCaseMatch();
1746 if (caseMatch == null) continue; 1750 if (caseMatch == null) continue;
1747 1751
1748 DartType caseType = analyze(caseMatch.expression); 1752 DartType caseType = analyze(caseMatch.expression);
1749 checkAssignable(caseMatch, expressionType, caseType); 1753 checkAssignable(caseMatch, expressionType, caseType);
1750 } 1754 }
1751 1755
1752 analyze(switchCase); 1756 analyze(switchCase);
1753 } 1757 }
1754 1758
1759 if (!hasDefaultCase && expressionType.isEnumType) {
1760 Map<ConstantValue, FieldElement> enumValues =
1761 <ConstantValue, FieldElement>{};
1762 List<FieldElement> unreferencedFields = <FieldElement>[];
1763 EnumClassElement enumClass = expressionType.element;
1764 enumClass.enumValues.forEach((FieldElement field) {
1765 ConstantExpression constantExpression =
1766 compiler.constants.getConstantForVariable(field);
1767 if (constantExpression == null) {
1768 // The field might not have been resolved.
1769 unreferencedFields.add(field);
1770 } else {
1771 enumValues[constantExpression.value] = field;
1772 }
1773 });
1774
1775 for (SwitchCase switchCase in node.cases) {
1776 for (Node labelOrCase in switchCase.labelsAndCases) {
1777 CaseMatch caseMatch = labelOrCase.asCaseMatch();
1778 if (caseMatch != null) {
1779 ConstantExpression caseConstant =
1780 compiler.resolver.constantCompiler.compileNode(
1781 caseMatch.expression, elements);
1782 enumValues.remove(caseConstant.value);
floitsch 2014/11/12 17:52:35 Do you want to add a check here to make sure that
Johnni Winther 2014/11/13 08:48:40 Not required by spec but should have a hint regard
1783 }
1784 }
1785 }
1786 unreferencedFields.addAll(enumValues.values);
1787 if (!unreferencedFields.isEmpty) {
1788 compiler.reportWarning(node, MessageKind.MISSING_ENUM_CASES,
1789 {'enumType': expressionType,
1790 'enumValues': unreferencedFields.map((e) => e.name).join(', ')});
1791 }
1792 }
1793
1794
1755 return const StatementType(); 1795 return const StatementType();
1756 } 1796 }
1757 1797
1758 visitSwitchCase(SwitchCase node) { 1798 visitSwitchCase(SwitchCase node) {
1759 return analyze(node.statements); 1799 return analyze(node.statements);
1760 } 1800 }
1761 1801
1762 visitTryStatement(TryStatement node) { 1802 visitTryStatement(TryStatement node) {
1763 // TODO(johnniwinther): Use reachability information of try-block, 1803 // TODO(johnniwinther): Use reachability information of try-block,
1764 // catch-blocks and finally-block to compute the whether the try statement 1804 // catch-blocks and finally-block to compute the whether the try statement
(...skipping 12 matching lines...) Expand all
1777 1817
1778 visitTypedef(Typedef node) { 1818 visitTypedef(Typedef node) {
1779 // Do not typecheck [Typedef] nodes. 1819 // Do not typecheck [Typedef] nodes.
1780 } 1820 }
1781 1821
1782 visitNode(Node node) { 1822 visitNode(Node node) {
1783 compiler.internalError(node, 1823 compiler.internalError(node,
1784 'Unexpected node ${node.getObjectDescription()} in the type checker.'); 1824 'Unexpected node ${node.getObjectDescription()} in the type checker.');
1785 } 1825 }
1786 } 1826 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698