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

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: 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..4073f291f67914cc9238110f80ce6fea5623ca98 100644
--- a/pkg/compiler/lib/src/typechecker.dart
+++ b/pkg/compiler/lib/src/typechecker.dart
@@ -1736,11 +1736,16 @@ class TypeCheckerVisitor extends Visitor<DartType> {
visitSwitchStatement(SwitchStatement node) {
// TODO(johnniwinther): Handle reachability based on reachability of
// switch cases.
+ // TODO(johnniwinther): Provide hint of duplicate case constants.
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 +1757,44 @@ class TypeCheckerVisitor extends Visitor<DartType> {
analyze(switchCase);
}
+ if (!hasDefaultCase && expressionType.isEnumType) {
+ compiler.enqueuer.resolution.addDeferredAction(
+ elements.analyzedElement, () {
+ 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);
+ }
+ }
+ }
+ 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();
}
« 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