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

Unified Diff: lib/src/compiler/code_generator.dart

Issue 2026133002: Throw TypeError instead of CastError for type coercions. (Closed) Base URL: https://github.com/dart-lang/dev_compiler@master
Patch Set: Created 4 years, 7 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: lib/src/compiler/code_generator.dart
diff --git a/lib/src/compiler/code_generator.dart b/lib/src/compiler/code_generator.dart
index 6077c26ec543a7704068904a85b2aa28062f9254..4b3623dc7037d2049b234c323d3cacd3d9403fb2 100644
--- a/lib/src/compiler/code_generator.dart
+++ b/lib/src/compiler/code_generator.dart
@@ -549,7 +549,17 @@ class CodeGenerator extends GeneralizingAstVisitor
nameType: options.nameTypeTests || options.hoistTypeTests,
hoistType: options.hoistTypeTests);
if (_inAngularTemplate) return jsFrom;
- return js.call('dart.as(#, #)', [jsFrom, type]);
+ if (isReifiedCoercion(node)) {
+ return js.call('dart.check(#, #)', [jsFrom, type]);
+ } else {
+ return js.call('dart.as(#, #)', [jsFrom, type]);
+ }
+ }
+
+ bool isReifiedCoercion(AstNode node) {
+ // TODO(sra): Find a better way to recognize reified coercion, since we
+ // can't set the isSynthetic attribute.
+ return (node is AsExpression) && (node.asOperator.offset == 0);
}
@override
@@ -4402,12 +4412,10 @@ class CodeGenerator extends GeneralizingAstVisitor
if (op == '&&') return shortCircuit('# && #');
if (op == '||') return shortCircuit('# || #');
}
- // Offset 0 from start of file is syntactically impossible for normal code.
- // TODO(sra): Find a better way to recognize reified coercion, since we
- // can't set the isSynthetic attribute.
- if (node is AsExpression && node.asOperator.offset == 0) {
- assert(node.staticType == types.boolType);
- return js.call('dart.test(#)', _visit(node.expression));
+ if (isReifiedCoercion(node)) {
+ AsExpression asNode = node;
+ assert(asNode.staticType == types.boolType);
+ return js.call('dart.test(#)', _visit(asNode.expression));
}
JS.Expression result = _visit(node);
if (isNullable(node)) result = js.call('dart.test(#)', result);

Powered by Google App Engine
This is Rietveld 408576698