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

Unified Diff: frogsh

Issue 8343037: Throw FallThroughError when we fall through a case in a switch statement. Fixes (Closed) Base URL: http://dart.googlecode.com/svn/experimental/frog/
Patch Set: '' Created 9 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
« no previous file with comments | « no previous file | gen.dart » ('j') | gen.dart » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: frogsh
===================================================================
--- frogsh (revision 858)
+++ frogsh (working copy)
@@ -283,6 +283,11 @@
TypeError.prototype.toString = function() {
return ("Failed type check: type " + this.srcType + " is not assignable to type ") + ("" + this.dstType + " of " + this.dstName + " in " + this.url + " at line ") + ("" + this.line + ", column " + this.column + ".");
}
+// ********** Code for FallThroughError **************
+function FallThroughError() {}
+FallThroughError.prototype.toString = function() {
+ return ("Switch case fall-through in " + this.url + " at line " + this.line + ".");
+}
// ********** Code for Object **************
Object.prototype.get$dynamic = function() {
return this;
@@ -7272,6 +7277,7 @@
else {
this.writer.writeln(('return ' + this.visitValue(node.value).code + ';'));
}
+ return true;
}
MethodGenerator.prototype.visitThrowStatement = function(node) {
if (node.value != null) {
@@ -7288,6 +7294,7 @@
this.writer.writeln(('throw ' + rethrow.code + ';'));
}
}
+ return true;
}
MethodGenerator.prototype.visitAssertStatement = function(node) {
var test = this.visitValue(node.test);
@@ -7309,6 +7316,7 @@
else {
this.writer.writeln(('break ' + node.label.name + ';'));
}
+ return true;
}
MethodGenerator.prototype.visitContinueStatement = function(node) {
if (node.label == null) {
@@ -7317,15 +7325,18 @@
else {
this.writer.writeln(('continue ' + node.label.name + ';'));
}
+ return true;
}
MethodGenerator.prototype.visitIfStatement = function(node) {
var test = this.visitBool(node.test);
+ var return1, return2;
this.writer.write(('if (' + test.code + ') '));
- node.trueBranch.visit(this);
+ return1 = node.trueBranch.visit(this);
if (node.falseBranch != null) {
this.writer.write('else ');
- node.falseBranch.visit(this);
+ return2 = node.falseBranch.visit(this);
}
+ return (return1 && return2);
}
MethodGenerator.prototype.visitWhileStatement = function(node) {
var test = this.visitBool(node.test);
@@ -7482,6 +7493,7 @@
MethodGenerator.prototype.visitSwitchStatement = function(node) {
var test = this.visitValue(node.test);
this.writer.enterBlock(('switch (' + test.code + ') {'));
+ var allCasesReturn = true;
var $list = node.cases;
for (var $i = 0;$i < $list.length; $i++) {
var case_ = $list.$index($i);
@@ -7493,6 +7505,9 @@
i < case_.cases.length; i++) {
var expr = case_.cases.$index(i);
if (expr == null) {
+ if (i < case_.cases.length - 1) {
+ world.error('default clause must be the last case', case_.get$span());
+ }
this.writer.writeln('default:');
}
else {
@@ -7501,26 +7516,35 @@
}
}
this.writer.enterBlock('');
+ var caseReturns;
var $list0 = case_.statements;
for (var $i0 = case_.statements.iterator(); $i0.hasNext(); ) {
var stmt = $i0.next();
- stmt.visit(this);
+ caseReturns = stmt.visit(this);
}
+ if ($ne(case_, node.cases.$index(node.cases.length - 1)) && !caseReturns) {
+ var span = case_.statements.$index(case_.statements.length - 1).get$span();
+ this.writer.writeln(('\$throw(new FallThroughError("' + span.file.filename + '",') + (' ' + span.file.getLine(span.start) + '))'));
+ allCasesReturn = false;
+ }
this.writer.exitBlock('');
this._popBlock();
}
this.writer.exitBlock('}');
+ return allCasesReturn;
}
MethodGenerator.prototype.visitBlockStatement = function(node) {
this._pushBlock(false);
this.writer.enterBlock('{');
+ var returns;
var $list = node.body;
for (var $i = 0;$i < $list.length; $i++) {
var stmt = $list.$index($i);
- stmt.visit(this);
+ returns = stmt.visit(this);
}
this.writer.exitBlock('}');
this._popBlock();
+ return returns;
}
MethodGenerator.prototype.visitLabeledStatement = function(node) {
this.writer.writeln(('' + node.name.name + ':'));
« no previous file with comments | « no previous file | gen.dart » ('j') | gen.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698