Permalink
Browse files

GROOVY-5466: correct scoping issue for the case that an if-else does …

…not use BlockStatement
  • Loading branch information...
1 parent a3595e0 commit 90d670a40eeed1bc7d1f356084323e44651313fb @blackdrag blackdrag committed Jul 13, 2012
@@ -21,6 +21,7 @@
import org.codehaus.groovy.ast.stmt.BlockStatement;
import org.codehaus.groovy.ast.stmt.CatchStatement;
import org.codehaus.groovy.ast.stmt.ForStatement;
+import org.codehaus.groovy.ast.stmt.IfStatement;
import org.codehaus.groovy.ast.stmt.Statement;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.syntax.Types;
@@ -310,6 +311,16 @@ public void visitForLoop(ForStatement forLoop) {
popState();
}
+ public void visitIfElse(IfStatement ifElse) {
+ ifElse.getBooleanExpression().visit(this);
+ pushState();
+ ifElse.getIfBlock().visit(this);
+ popState();
+ pushState();
+ ifElse.getElseBlock().visit(this);
+ popState();
+ }
+
public void visitDeclarationExpression(DeclarationExpression expression) {
// visit right side first to avoid the usage of a
// variable before its declaration
@@ -45,4 +45,13 @@ class BlockScopeVisibilityTest extends CompilableTestSupport {
assert c(1)() == 1
}
-}
+ public void testForLoopStatement() {
+ // this example requires not to put the declaration
+ // into a block !
+ if (false)
+ int number = 1
+
+ shouldFail{ number }
+ }
+
+}

0 comments on commit 90d670a

Please sign in to comment.