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

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

Issue 184663004: Follow factory redirections when checking case expressions. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove same-type switch-test from type-checker test. Created 6 years, 10 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 fd5c3f594366f19039b33e4ed91bfcb27c1e4696..048c1a89784fc9f149b951fd012feb729b3676f3 100644
--- a/sdk/lib/_internal/compiler/implementation/typechecker.dart
+++ b/sdk/lib/_internal/compiler/implementation/typechecker.dart
@@ -1701,71 +1701,25 @@ 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"));
- ClassElement cls = type.element;
- if (cls == compiler.doubleClass) return true;
- if (cls == compiler.intClass || cls == compiler.stringClass) return false;
- if (cls == compiler.typeClass) return true;
- Element equals = cls.lookupMember('==');
- return equals.getEnclosingClass() != compiler.objectClass;
- }
-
visitSwitchStatement(SwitchStatement node) {
// TODO(johnniwinther): Handle reachability based on reachability of
// switch cases.
+
DartType expressionType = analyze(node.expression);
- Map<CaseMatch, DartType> caseTypeMap = new Map<CaseMatch, DartType>();
+
+ // Check that all the case expressions are assignable to the expression.
for (SwitchCase switchCase in node.cases) {
for (Node labelOrCase in switchCase.labelsAndCases) {
CaseMatch caseMatch = labelOrCase.asCaseMatch();
if (caseMatch == null) continue;
DartType caseType = analyze(caseMatch.expression);
- caseTypeMap[caseMatch] = caseType;
checkAssignable(caseMatch, expressionType, caseType);
}
analyze(switchCase);
}
- // Check that all the case expressions have the same type.
- CaseMatch firstCase = null;
- DartType firstCaseType = null;
- bool hasReportedProblem = false;
- caseTypeMap.forEach((CaseMatch caseMatch, DartType caseType) {
- if (firstCaseType == null) {
- firstCase = caseMatch;
- firstCaseType = caseType;
- } else {
- if (caseType != firstCaseType) {
- if (!hasReportedProblem) {
- compiler.reportError(
- node,
- MessageKind.SWITCH_CASE_TYPES_NOT_EQUAL,
- {'type': firstCaseType});
- compiler.reportInfo(
- firstCase.expression,
- MessageKind.SWITCH_CASE_TYPES_NOT_EQUAL_CASE,
- {'type': firstCaseType});
- hasReportedProblem = true;
- }
- compiler.reportInfo(
- caseMatch.expression,
- MessageKind.SWITCH_CASE_TYPES_NOT_EQUAL_CASE,
- {'type': caseType});
- }
- }
- });
- // 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,
- {'type': firstCaseType});
- }
+
return StatementType.NOT_RETURNING;
}

Powered by Google App Engine
This is Rietveld 408576698