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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: pkg/compiler/lib/src/typechecker.dart
diff --git a/pkg/compiler/lib/src/typechecker.dart b/pkg/compiler/lib/src/typechecker.dart
index b1fb8cd434fc7613472b666e0482a5b58f8e88e3..9300b10a2fb08523b10d496f233002992ce6a12d 100644
--- a/pkg/compiler/lib/src/typechecker.dart
+++ b/pkg/compiler/lib/src/typechecker.dart
@@ -1740,7 +1740,11 @@ class TypeCheckerVisitor extends Visitor<DartType> {
DartType expressionType = analyze(node.expression);
// Check that all the case expressions are assignable to the expression.
+ bool hasDefaultCase = false;
for (SwitchCase switchCase in node.cases) {
+ if (switchCase.isDefaultCase) {
+ hasDefaultCase = true;
+ }
for (Node labelOrCase in switchCase.labelsAndCases) {
CaseMatch caseMatch = labelOrCase.asCaseMatch();
if (caseMatch == null) continue;
@@ -1752,6 +1756,42 @@ class TypeCheckerVisitor extends Visitor<DartType> {
analyze(switchCase);
}
+ if (!hasDefaultCase && expressionType.isEnumType) {
+ Map<ConstantValue, FieldElement> enumValues =
+ <ConstantValue, FieldElement>{};
+ List<FieldElement> unreferencedFields = <FieldElement>[];
+ EnumClassElement enumClass = expressionType.element;
+ enumClass.enumValues.forEach((FieldElement field) {
+ ConstantExpression constantExpression =
+ compiler.constants.getConstantForVariable(field);
+ if (constantExpression == null) {
+ // The field might not have been resolved.
+ unreferencedFields.add(field);
+ } else {
+ enumValues[constantExpression.value] = field;
+ }
+ });
+
+ for (SwitchCase switchCase in node.cases) {
+ for (Node labelOrCase in switchCase.labelsAndCases) {
+ CaseMatch caseMatch = labelOrCase.asCaseMatch();
+ if (caseMatch != null) {
+ ConstantExpression caseConstant =
+ compiler.resolver.constantCompiler.compileNode(
+ caseMatch.expression, elements);
+ 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
+ }
+ }
+ }
+ unreferencedFields.addAll(enumValues.values);
+ if (!unreferencedFields.isEmpty) {
+ compiler.reportWarning(node, MessageKind.MISSING_ENUM_CASES,
+ {'enumType': expressionType,
+ 'enumValues': unreferencedFields.map((e) => e.name).join(', ')});
+ }
+ }
+
+
return const StatementType();
}

Powered by Google App Engine
This is Rietveld 408576698