GROOVY-5466: correct scoping issue for the case that an if-else does not use BlockStatement
diff --git a/src/main/org/codehaus/groovy/classgen/VariableScopeVisitor.java b/src/main/org/codehaus/groovy/classgen/VariableScopeVisitor.java
index 8a2153a..3203314 100644
--- a/src/main/org/codehaus/groovy/classgen/VariableScopeVisitor.java
+++ b/src/main/org/codehaus/groovy/classgen/VariableScopeVisitor.java
@@ -22,6 +22,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;
@@ -320,6 +321,16 @@
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
diff --git a/src/test/gls/scope/BlockScopeVisibilityTest.groovy b/src/test/gls/scope/BlockScopeVisibilityTest.groovy
index 84771fa..6211999 100644
--- a/src/test/gls/scope/BlockScopeVisibilityTest.groovy
+++ b/src/test/gls/scope/BlockScopeVisibilityTest.groovy
@@ -45,4 +45,13 @@
assert c(1)() == 1
}
-}
\ No newline at end of file
+ public void testForLoopStatement() {
+ // this example requires not to put the declaration
+ // into a block !
+ if (false)
+ int number = 1
+
+ shouldFail{ number }
+ }
+
+}