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

Unified Diff: gen.dart

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 | « frogsh ('k') | lib/corelib.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: gen.dart
===================================================================
--- gen.dart (revision 858)
+++ gen.dart (working copy)
@@ -1072,15 +1072,20 @@
meth.generator.writeDefinition(writer, null);
}
- void visitReturnStatement(ReturnStatement node) {
+ /**
+ * Returns true indicating that code will return after executing this
jimhug 2011/10/28 14:33:34 Returns true indicating that normal control-flow i
+ * statement.
+ */
+ bool visitReturnStatement(ReturnStatement node) {
if (node.value == null) {
writer.writeln('return;');
} else {
writer.writeln('return ${visitValue(node.value).code};');
}
+ return true;
}
- void visitThrowStatement(ThrowStatement node) {
+ bool visitThrowStatement(ThrowStatement node) {
// Dart allows throwing anything, just like JS
if (node.value != null) {
var value = visitValue(node.value);
@@ -1096,6 +1101,7 @@
writer.writeln('throw ${rethrow.code};');
}
}
+ return true;
}
void visitAssertStatement(AssertStatement node) {
jimhug 2011/10/28 14:33:34 For type safety, you need to update all of the oth
@@ -1115,31 +1121,35 @@
}
}
- void visitBreakStatement(BreakStatement node) {
+ bool visitBreakStatement(BreakStatement node) {
// TODO(jimhug): Lots of flow error checking here and below.
if (node.label == null) {
writer.writeln('break;');
} else {
writer.writeln('break ${node.label.name};');
}
+ return true;
}
- void visitContinueStatement(ContinueStatement node) {
+ bool visitContinueStatement(ContinueStatement node) {
if (node.label == null) {
writer.writeln('continue;');
} else {
writer.writeln('continue ${node.label.name};');
}
+ return true;
}
- void visitIfStatement(IfStatement node) {
+ bool visitIfStatement(IfStatement node) {
var test = visitBool(node.test);
+ var return1, return2;
jimhug 2011/10/28 14:33:34 I'd suggest the name "exit" here instead of return
Emily Fortuna 2011/10/28 17:23:23 I tested this out before I wrote this, and if(null
writer.write('if (${test.code}) ');
- node.trueBranch.visit(this);
+ return1 = node.trueBranch.visit(this);
if (node.falseBranch != null) {
writer.write('else ');
- node.falseBranch.visit(this);
+ return2 = node.falseBranch.visit(this);
jimhug 2011/10/28 14:33:34 if (node.falseBranch.visit(this) && exit1) return
}
+ return (return1 && return2);
jimhug 2011/10/28 14:33:34 return false;
}
void visitWhileStatement(WhileStatement node) {
@@ -1328,10 +1338,11 @@
writer.exitBlock('}');
}
- void visitSwitchStatement(SwitchStatement node) {
+ bool visitSwitchStatement(SwitchStatement node) {
// TODO(jimhug): Lots of negative tests to check for.
var test = visitValue(node.test);
writer.enterBlock('switch (${test.code}) {');
+ bool allCasesReturn = true;
jimhug 2011/10/28 14:33:34 I don't think that you can use this effectively he
for (var case_ in node.cases) {
if (case_.label != null) {
world.error('unimplemented: labeled case statement', case_.span);
@@ -1351,26 +1362,36 @@
}
}
writer.enterBlock('');
+ bool caseReturns;
jimhug 2011/10/28 14:33:34 This needs to be initialized to false.
for (var stmt in case_.statements) {
- stmt.visit(this);
+ caseReturns = stmt.visit(this);
jimhug 2011/10/28 14:33:34 This should have the same checking for previous st
}
- if (case_ != node.cases[node.cases.length - 1]) {
+ if (case_ != node.cases[node.cases.length - 1] && !caseReturns) {
var span = case_.statements[case_.statements.length - 1].span;
+ //TODO(efortuna): This error message isn't 100% correct because
+ // constructors are not being built correctly. Check back when I've
+ // fixed constructors.
+ writer.writeln('\$throw(new FallThroughError("${span.file.filename}",' +
+ ' ${span.file.getLine(span.start)}))');
+ allCasesReturn = false;
jimhug 2011/10/28 14:33:34 See above on allCasesReturn - but this shouldn't b
}
writer.exitBlock('');
_popBlock();
}
writer.exitBlock('}');
+ return allCasesReturn;
jimhug 2011/10/28 14:33:34 return false;
}
- void visitBlockStatement(BlockStatement node) {
+ bool visitBlockStatement(BlockStatement node) {
_pushBlock();
writer.enterBlock('{');
+ var returns;
jimhug 2011/10/28 14:33:34 Needs to be initialized.
for (var stmt in node.body) {
- stmt.visit(this);
+ returns = stmt.visit(this);
Emily Fortuna 2011/10/27 22:11:24 I should add a warning here if we have a return st
jimhug 2011/10/28 14:33:34 I think that your warning should work here just fi
}
writer.exitBlock('}');
_popBlock();
+ return returns;
}
void visitLabeledStatement(LabeledStatement node) {
« no previous file with comments | « frogsh ('k') | lib/corelib.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698