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

Unified Diff: sdk/lib/_internal/compiler/implementation/typechecker.dart

Issue 26978004: Emit a compile-time error when using doubles as switch case expressions. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 months 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: sdk/lib/_internal/compiler/implementation/typechecker.dart
diff --git a/sdk/lib/_internal/compiler/implementation/typechecker.dart b/sdk/lib/_internal/compiler/implementation/typechecker.dart
index 2cc56efd9b0ea3a2ece6b4efb2e69d2d67192d89..bbff390294e8a4bd7bccd285f0f8db81eb842579 100644
--- a/sdk/lib/_internal/compiler/implementation/typechecker.dart
+++ b/sdk/lib/_internal/compiler/implementation/typechecker.dart
@@ -1193,6 +1193,17 @@ class TypeCheckerVisitor extends Visitor<DartType> {
return analyze(node.expression);
}
+ bool invalidSwitchExpressionType(Node diagnosticNode, DartType type) {
+ if (type.kind == TypeKind.FUNCTION) return true;
+ assert(invariant(diagnosticNode, type.kind == TypeKind.INTERFACE,
+ message: "Expected interface type"));
Johnni Winther 2013/10/15 11:47:44 Why can't we see other types here? What about thes
+ ClassElement cls = type.element;
+ if (cls == compiler.doubleClass) return true;
+ if (cls == compiler.intClass || cls == compiler.stringClass) return false;
+ Element equals = cls.lookupMember(const SourceString('=='));
+ return equals.getEnclosingClass() != compiler.objectClass;
+ }
+
visitSwitchStatement(SwitchStatement node) {
// TODO(johnniwinther): Handle reachability based on reachability of
// switch cases.
@@ -1211,7 +1222,7 @@ class TypeCheckerVisitor extends Visitor<DartType> {
analyze(switchCase);
}
-
+ // Check that all the case expressions have the same type.
CaseMatch firstCase = null;
DartType firstCaseType = null;
bool hasReportedProblem = false;
@@ -1239,7 +1250,13 @@ class TypeCheckerVisitor extends Visitor<DartType> {
}
}
});
-
+ // Check that the type is either [int], [String], or a class that does not
+ // implement `operator ==`.
+ if (firstCaseType != null &&
+ invalidSwitchExpressionType(firstCase, firstCaseType)) {
+ compiler.reportError(firstCase.expression,
+ MessageKind.SWITCH_CASE_VALUE_OVERRIDES_EQUALS);
+ }
return StatementType.NOT_RETURNING;
}
« no previous file with comments | « sdk/lib/_internal/compiler/implementation/ssa/builder.dart ('k') | sdk/lib/_internal/compiler/implementation/warnings.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698