mirror of
https://github.com/dart-lang/sdk
synced 2024-09-20 06:31:30 +00:00
Emit a compile-time error when using type literals in case-expressions.
BUG=dartbug.com/15295 R=johnniwinther@google.com, ngeoffray@google.com Review URL: https://codereview.chromium.org//88433002 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@30707 260f80e4-7a28-3924-810f-c04153c831b5
This commit is contained in:
parent
12bf7bb6c8
commit
f0443b361e
|
@ -1508,6 +1508,7 @@ class TypeCheckerVisitor extends Visitor<DartType> {
|
|||
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;
|
||||
}
|
||||
|
@ -1562,7 +1563,8 @@ class TypeCheckerVisitor extends Visitor<DartType> {
|
|||
if (firstCaseType != null &&
|
||||
invalidSwitchExpressionType(firstCase, firstCaseType)) {
|
||||
compiler.reportError(firstCase.expression,
|
||||
MessageKind.SWITCH_CASE_VALUE_OVERRIDES_EQUALS);
|
||||
MessageKind.SWITCH_CASE_VALUE_OVERRIDES_EQUALS,
|
||||
{'type': firstCaseType});
|
||||
}
|
||||
return StatementType.NOT_RETURNING;
|
||||
}
|
||||
|
|
|
@ -567,7 +567,7 @@ main() => new C<String>();
|
|||
|
||||
static const MessageKind SWITCH_CASE_VALUE_OVERRIDES_EQUALS =
|
||||
const MessageKind(
|
||||
"Error: 'case' expression type overrides 'operator=='.");
|
||||
"Error: 'case' expression type '#{type}' overrides 'operator =='.");
|
||||
|
||||
static const MessageKind INVALID_ARGUMENT_AFTER_NAMED = const MessageKind(
|
||||
"Error: Unnamed argument after named argument.");
|
||||
|
|
16
tests/compiler/dart2js_extra/type_constant_switch_test.dart
Normal file
16
tests/compiler/dart2js_extra/type_constant_switch_test.dart
Normal file
|
@ -0,0 +1,16 @@
|
|||
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
|
||||
// for details. All rights reserved. Use of this source code is governed by a
|
||||
// BSD-style license that can be found in the LICENSE file.
|
||||
|
||||
// Test that switching on type literals leads to a compile time error in
|
||||
// dart2js since its implementation of [Type] implements 'operator =='.
|
||||
|
||||
class C {}
|
||||
|
||||
var v;
|
||||
|
||||
main() {
|
||||
switch (v) {
|
||||
case C: break; /// 01: compile-time error
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue