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

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 910)
+++ frogsh (working copy)
@@ -302,6 +302,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;
@@ -2559,6 +2564,7 @@
return next;
}
+ $throw(new FallThroughError("./leg/scanner/scanner.dart", 389))
jimhug 2011/10/28 20:02:44 Please send Peter Ahe and email pointing this chan
default:
@@ -3466,6 +3472,7 @@
return next;
}
+ $throw(new FallThroughError("./leg/scanner/scanner.dart", 389))
default:
@@ -4268,6 +4275,7 @@
return next;
}
+ $throw(new FallThroughError("./leg/scanner/scanner.dart", 389))
default:
@@ -7250,6 +7258,7 @@
else {
if (body != null) body.visit(this);
}
+ return false;
}
MethodGenerator.prototype._pushBlock = function(reentrant) {
this._scope = new BlockScope(this, this._scope, reentrant);
@@ -7289,6 +7298,7 @@
MethodGenerator.prototype.visitDietStatement = function(node) {
var parser = new lang_Parser(node.span.file, false, node.span.start);
this.visitStatementsInBlock(parser.block());
+ return false;
}
MethodGenerator.prototype.visitVariableDefinition = function(node) {
var isFinal = false;
@@ -7322,12 +7332,14 @@
}
}
this.writer.writeln(';');
+ return false;
}
MethodGenerator.prototype.visitFunctionDefinition = function(node) {
var name = world.toJsIdentifier(node.name.name);
var meth = this._makeLambdaMethod(name, node);
var funcValue = this._scope.create(name, meth.get$functionType(), this.method.get$definition());
meth.generator.writeDefinition(this.writer, null);
+ return false;
}
MethodGenerator.prototype.visitReturnStatement = function(node) {
if (node.value == null) {
@@ -7339,6 +7351,7 @@
}
this.writer.writeln(('return ' + this.visitValue(node.value).code + ';'));
}
+ return true;
}
MethodGenerator.prototype.visitThrowStatement = function(node) {
if (node.value != null) {
@@ -7355,6 +7368,7 @@
this.writer.writeln(('throw ' + rethrow.code + ';'));
}
}
+ return true;
}
MethodGenerator.prototype.visitAssertStatement = function(node) {
var test = this.visitValue(node.test);
@@ -7368,6 +7382,7 @@
var column = span.file.getColumn(line, span.start);
this.writer.writeln(('\$assert(' + test.code + ', "' + MethodGenerator._escapeString(span.get$text()) + '",') + (' "' + span.file.filename + '", ' + (line + 1) + ', ' + (column + 1) + ');'));
}
+ return false;
}
MethodGenerator.prototype.visitBreakStatement = function(node) {
if (node.label == null) {
@@ -7376,6 +7391,7 @@
else {
this.writer.writeln(('break ' + node.label.name + ';'));
}
+ return true;
}
MethodGenerator.prototype.visitContinueStatement = function(node) {
if (node.label == null) {
@@ -7384,15 +7400,20 @@
else {
this.writer.writeln(('continue ' + node.label.name + ';'));
}
+ return true;
}
MethodGenerator.prototype.visitIfStatement = function(node) {
var test = this.visitBool(node.test);
+ var exit1;
this.writer.write(('if (' + test.code + ') '));
- node.trueBranch.visit(this);
+ exit1 = node.trueBranch.visit(this);
if (node.falseBranch != null) {
this.writer.write('else ');
- node.falseBranch.visit(this);
+ if (node.falseBranch.visit(this) && exit1) {
+ return true;
+ }
}
+ return false;
}
MethodGenerator.prototype.visitWhileStatement = function(node) {
var test = this.visitBool(node.test);
@@ -7400,6 +7421,7 @@
this._pushBlock(true);
node.body.visit(this);
this._popBlock();
+ return false;
}
MethodGenerator.prototype.visitDoStatement = function(node) {
this.writer.write('do ');
@@ -7408,6 +7430,7 @@
this._popBlock();
var test = this.visitBool(node.test);
this.writer.writeln(('while (' + test.code + ')'));
+ return false;
}
MethodGenerator.prototype.visitForStatement = function(node) {
this._pushBlock(false);
@@ -7435,6 +7458,7 @@
node.body.visit(this);
this._popBlock();
this._popBlock();
+ return false;
}
MethodGenerator.prototype.visitForInStatement = function(node) {
var itemType = this.method.resolveType(node.item.type);
@@ -7467,6 +7491,7 @@
this.visitStatementsInBlock(node.body);
this.writer.exitBlock('}');
this._popBlock();
+ return false;
}
MethodGenerator.prototype.visitTryStatement = function(node) {
this.writer.enterBlock('try {');
@@ -7545,6 +7570,7 @@
this._popBlock();
}
this.writer.exitBlock('}');
+ return false;
}
MethodGenerator.prototype.visitSwitchStatement = function(node) {
var test = this.visitValue(node.test);
@@ -7571,33 +7597,45 @@
}
}
this.writer.enterBlock('');
- var $list0 = case_.statements;
- for (var $i0 = case_.statements.iterator(); $i0.hasNext(); ) {
- var stmt = $i0.next();
- stmt.visit(this);
+ var caseExits = false;
+ for (var i = 0;
+ i < case_.statements.length; i++) {
+ var stmt = case_.statements.$index(i);
+ caseExits = stmt.visit(this);
+ if ($ne(stmt, case_.statements.$index(case_.statements.length - 1)) && caseExits) {
+ world.warning('unreachable code', case_.statements.$index(i + 1).get$span());
+ }
}
- if ($ne(case_, node.cases.$index(node.cases.length - 1))) {
+ if ($ne(case_, node.cases.$index(node.cases.length - 1)) && !caseExits) {
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) + '))'));
}
this.writer.exitBlock('');
this._popBlock();
}
this.writer.exitBlock('}');
+ return false;
}
MethodGenerator.prototype.visitBlockStatement = function(node) {
this._pushBlock(false);
this.writer.enterBlock('{');
- var $list = node.body;
- for (var $i = 0;$i < $list.length; $i++) {
- var stmt = $list.$index($i);
- stmt.visit(this);
+ var exits = false;
+ for (var i = 0;
+ i < node.body.length; i++) {
+ var stmt = node.body.$index(i);
+ exits = stmt.visit(this);
+ if (exits && $ne(stmt, node.body.$index(node.body.length - 1))) {
+ world.warning('unreachable code', node.body.$index(i + 1).get$span());
+ }
}
this.writer.exitBlock('}');
this._popBlock();
+ return exits;
}
MethodGenerator.prototype.visitLabeledStatement = function(node) {
this.writer.writeln(('' + node.name.name + ':'));
node.body.visit(this);
+ return false;
}
MethodGenerator.prototype.visitExpressionStatement = function(node) {
if ((node.body instanceof VarExpression) || (node.body instanceof ThisExpression)) {
@@ -7605,9 +7643,11 @@
}
var value = this.visitVoid(node.body);
this.writer.writeln(('' + value.code + ';'));
+ return false;
}
MethodGenerator.prototype.visitEmptyStatement = function(node) {
this.writer.writeln(';');
+ return false;
}
MethodGenerator.prototype._checkNonStatic = function(node) {
if (this.method.get$isStatic()) {
@@ -12089,8 +12129,8 @@
var start = this._peekToken.start;
var label = null;
if (this._peekIdentifier()) {
+ label = this.identifier();
this._eat(8/*TokenKind.COLON*/);
- label = this.identifier();
}
var cases = [];
while (true) {
« no previous file with comments | « no previous file | gen.dart » ('j') | gen.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698