This is an automated email from the ASF dual-hosted git repository. sunlan pushed a commit to branch GROOVY-4721 in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 87390561659f50e5a117bebb697731e8666f97b3 Author: Daniel Sun <[email protected]> AuthorDate: Wed Apr 2 01:06:23 2025 +0900 GROOVY-4721: variable declared in try block is in scope in finally block --- .../groovy/classgen/asm/StatementWriter.java | 16 ++- src/test/groovy/bugs/Groovy4721.groovy | 117 +++++++++++++++++++++ .../org/apache/groovy/groovysh/GroovyshTest.groovy | 23 +++- 3 files changed, 151 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java b/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java index 710e8cf747..3927b59c73 100644 --- a/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java +++ b/src/main/java/org/codehaus/groovy/classgen/asm/StatementWriter.java @@ -20,6 +20,7 @@ package org.codehaus.groovy.classgen.asm; import org.codehaus.groovy.ast.ClassHelper; import org.codehaus.groovy.ast.ClassNode; +import org.codehaus.groovy.ast.VariableScope; import org.codehaus.groovy.ast.expr.BinaryExpression; import org.codehaus.groovy.ast.expr.BooleanExpression; import org.codehaus.groovy.ast.expr.ClosureListExpression; @@ -36,6 +37,7 @@ import org.codehaus.groovy.ast.stmt.CaseStatement; import org.codehaus.groovy.ast.stmt.CatchStatement; import org.codehaus.groovy.ast.stmt.ContinueStatement; import org.codehaus.groovy.ast.stmt.DoWhileStatement; +import org.codehaus.groovy.ast.stmt.EmptyStatement; import org.codehaus.groovy.ast.stmt.ExpressionStatement; import org.codehaus.groovy.ast.stmt.ForStatement; import org.codehaus.groovy.ast.stmt.IfStatement; @@ -401,12 +403,20 @@ public class StatementWriter { private BlockRecorder makeBlockRecorder(final Statement finallyStatement) { BlockRecorder recorder = new BlockRecorder(); + final CompileStack compileStack = controller.getCompileStack(); + recorder.excludedStatement = () -> { - controller.getCompileStack().pushBlockRecorderVisit(recorder); + if (finallyStatement == null || finallyStatement instanceof EmptyStatement) return; + + final VariableScope originalScope = compileStack.getScope(); + compileStack.pop(); + compileStack.pushBlockRecorderVisit(recorder); finallyStatement.visit(controller.getAcg()); - controller.getCompileStack().popBlockRecorderVisit(recorder); + compileStack.popBlockRecorderVisit(recorder); + compileStack.pushVariableScope(originalScope); }; - controller.getCompileStack().pushBlockRecorder(recorder); + + compileStack.pushBlockRecorder(recorder); return recorder; } diff --git a/src/test/groovy/bugs/Groovy4721.groovy b/src/test/groovy/bugs/Groovy4721.groovy new file mode 100644 index 0000000000..8e720f6f83 --- /dev/null +++ b/src/test/groovy/bugs/Groovy4721.groovy @@ -0,0 +1,117 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package bugs + +import org.junit.jupiter.api.Test + +import static groovy.test.GroovyAssert.assertScript +import static groovy.test.GroovyAssert.shouldFail + +final class Groovy4721 { + @Test + void testAccessingVariableInFinallyBlock_1() { + def err = shouldFail '''\ + class MyClass { + def myMethod() { + try { + def x = 'foo' + } + finally { + println 'x: ' + x + } + } + } + println new MyClass().myMethod() + ''' + assert err =~ /No such property: x for class: MyClass/ + } + + @Test + void testAccessingVariableInFinallyBlock_2() { + def err = shouldFail '''\ + class MyClass { + def myMethod() { + try { + def x = 'foo' + return x + } + finally { + println 'x: ' + x + } + } + } + println new MyClass().myMethod() + ''' + assert err =~ /No such property: x for class: MyClass/ + } + + @Test + void testAccessingVariableInFinallyBlock_3() { + def err = shouldFail '''\ + class MyClass { + def myMethod() { + try { + def x = 'foo' + return + } + finally { + println 'x: ' + x + } + } + } + println new MyClass().myMethod() + ''' + assert err =~ /No such property: x for class: MyClass/ + } + + @Test + void testAccessingVariableInFinallyBlock_4() { + assertScript '''\ + class MyClass { + def myMethod() { + try { + def x = 'foo' + } + finally { + assert true + } + } + } + assert 'foo' == new MyClass().myMethod() + ''' + } + + @Test + void testAccessingVariableInFinallyBlock_5() { + assertScript '''\ + class MyClass { + def myMethod() { + try { + def x = 'foo' + return x + } + finally { + assert true + } + } + } + assert 'foo' == new MyClass().myMethod() + ''' + } +} diff --git a/subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/GroovyshTest.groovy b/subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/GroovyshTest.groovy index 504f3b123e..bdec18b4cf 100644 --- a/subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/GroovyshTest.groovy +++ b/subprojects/groovy-groovysh/src/test/groovy/org/apache/groovy/groovysh/GroovyshTest.groovy @@ -19,13 +19,15 @@ package org.apache.groovy.groovysh import groovy.test.GroovyTestCase -import org.apache.groovy.groovysh.completion.antlr4.ReflectionCompleter +import groovy.test.NotYetImplemented import org.apache.groovy.groovysh.completion.ReflectionCompletionCandidate import org.apache.groovy.groovysh.completion.TokenUtilTest +import org.apache.groovy.groovysh.completion.antlr4.ReflectionCompleter import org.codehaus.groovy.GroovyException import org.codehaus.groovy.control.MultipleCompilationErrorsException import org.codehaus.groovy.tools.shell.IO import org.fusesource.jansi.AnsiOutputStream +import org.junit.jupiter.api.Disabled class GroovyshTest extends GroovyTestCase { @@ -392,10 +394,19 @@ class GroovyshInterpreterModeTest extends GroovyshTest { } } + // groovy:000> int x = 3 + // ===> 3 + // groovy:000> x + // Unknown property: x + // groovy:000> x = 3 + // ===> 3 + // groovy:000> x + // ===> 3 + // groovy:000> void testBoundVar() { Groovysh groovysh = createGroovysh() - groovysh.execute('int x = 3') + groovysh.execute('x = 3') assert mockOut.toString().length() > 0 assert ' 3\n' == mockOut.toString().normalize()[-3..-1] groovysh.execute('x') @@ -403,6 +414,14 @@ class GroovyshInterpreterModeTest extends GroovyshTest { assert ' 3\n' == mockOut.toString().normalize()[-3..-1] } + // collecting variables relies on GROOVY-4721, e.g. accessing variable declared in try block is in scope in finally block + // but after GROOVY-4721 is fixed, this test will fail, but it is aligned with the behavior of the groovySh: + // groovy:000> int x, y, z + // ===> 0 + // groovy:000> y + // Unknown property: y + // groovy:000> + @NotYetImplemented void testBoundVarmultiple() { Groovysh groovysh = createGroovysh() groovysh.execute('int x, y, z')
