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 5b1c282ba0e2e0faa3577dfb6d507ee16504ea6b
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       | 11 ++-
 src/test/groovy/bugs/Groovy4721.groovy             | 81 ++++++++++++++++++++++
 2 files changed, 89 insertions(+), 3 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..2ddfdac513 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;
@@ -401,12 +402,16 @@ public class StatementWriter {
 
     private BlockRecorder makeBlockRecorder(final Statement finallyStatement) {
         BlockRecorder recorder = new BlockRecorder();
+        final CompileStack compileStack = controller.getCompileStack();
         recorder.excludedStatement = () -> {
-            controller.getCompileStack().pushBlockRecorderVisit(recorder);
+            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..cf31056909
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy4721.groovy
@@ -0,0 +1,81 @@
+/*
+ *  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.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/
+    }
+}

Reply via email to