Chromium Code Reviews| 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; |
| } |