groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject groovy git commit: GROOVY-8085: Exception in "finally" not caught by outer "try"
Date Mon, 27 Feb 2017 14:31:27 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X 41f088116 -> b54906248


GROOVY-8085: Exception in "finally" not caught by outer "try"


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/b5490624
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/b5490624
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/b5490624

Branch: refs/heads/GROOVY_2_4_X
Commit: b549062485cbd82dfde5537a16ddee126cc7fcfc
Parents: 41f0881
Author: sunlan <sunlan@apache.org>
Authored: Mon Feb 27 08:52:08 2017 +0800
Committer: paulk <paulk@asert.com.au>
Committed: Tue Feb 28 00:31:06 2017 +1000

----------------------------------------------------------------------
 .../groovy/classgen/asm/CompileStack.java       | 17 ++--
 src/test/groovy/bugs/Groovy8085Bug.groovy       | 91 ++++++++++++++++++++
 2 files changed, 100 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/b5490624/src/main/org/codehaus/groovy/classgen/asm/CompileStack.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/classgen/asm/CompileStack.java b/src/main/org/codehaus/groovy/classgen/asm/CompileStack.java
index 99555ed..03421c9 100644
--- a/src/main/org/codehaus/groovy/classgen/asm/CompileStack.java
+++ b/src/main/org/codehaus/groovy/classgen/asm/CompileStack.java
@@ -257,7 +257,7 @@ public class CompileStack implements Opcodes {
      * @param store defines if the toplevel argument of the stack should be stored
      * @return the index used for this temporary variable
      */
-    public int defineTemporaryVariable(org.codehaus.groovy.ast.Variable var, boolean store)
{
+    public int defineTemporaryVariable(Variable var, boolean store) {
         return defineTemporaryVariable(var.getName(), var.getType(),store);
     }
 
@@ -772,25 +772,26 @@ public class CompileStack implements Opcodes {
 
         MethodVisitor mv = controller.getMethodVisitor();
 
-        Label end = new Label();
-        mv.visitInsn(NOP);
-        mv.visitLabel(end);
-        Label newStart = new Label();
-
         for (BlockRecorder fb : blocks) {
             if (visitedBlocks.contains(fb)) continue;
 
+            Label end = new Label();
+            mv.visitInsn(NOP);
+            mv.visitLabel(end);
+
             fb.closeRange(end);
 
             // we exclude the finally block from the exception table
             // here to avoid double visiting of finally statements
             fb.excludedStatement.run();
 
+            Label newStart = new Label();
             fb.startRange(newStart);
+
+            mv.visitInsn(NOP);
+            mv.visitLabel(newStart);
         }
 
-        mv.visitInsn(NOP);
-        mv.visitLabel(newStart);
     }
 
     public void applyBlockRecorder() {

http://git-wip-us.apache.org/repos/asf/groovy/blob/b5490624/src/test/groovy/bugs/Groovy8085Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy8085Bug.groovy b/src/test/groovy/bugs/Groovy8085Bug.groovy
new file mode 100644
index 0000000..f1a9310
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy8085Bug.groovy
@@ -0,0 +1,91 @@
+/*
+ *  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 groovy.bugs
+
+class Groovy8085Bug extends GroovyTestCase {
+    void testTryCatchFinally() {
+        assertScript '''
+            try {
+                try {
+                    true
+                } finally {
+                    99 / 0
+                }
+            } catch (Exception e) {
+                System.out.println("catch!!!");
+            }
+        '''
+    }
+
+    void testTryCatchFinally2() {
+        assertScript '''
+            def visitSequence = []
+            try {
+                try {
+                    true
+                } finally {
+                    visitSequence << 'innerFinally'
+                    99 / 0
+                }
+            } catch (Exception e) {
+                visitSequence << 'outerCatch'
+                System.out.println("catch!!!");
+            } finally {
+                visitSequence << 'outerFinally'
+            }
+            
+            assert ['innerFinally', 'outerCatch', 'outerFinally'] == visitSequence
+        '''
+    }
+
+    void testTryCatchFinallyWithExplicitReturn() {
+        assertScript '''
+            try {
+                try {
+                    return true
+                } finally {
+                    99 / 0
+                }
+            } catch (Exception e) {
+                System.out.println("catch!!!");
+            }
+        '''
+    }
+
+    void testTryCatchFinallyWithExplicitReturn2() {
+        assertScript '''
+            def visitSequence = []
+            try {
+                try {
+                    return true
+                } finally {
+                    visitSequence << 'innerFinally'
+                    99 / 0
+                }
+            } catch (Exception e) {
+                visitSequence << 'outerCatch'
+                System.out.println("catch!!!");
+            } finally {
+                visitSequence << 'outerFinally'
+            }
+            
+            assert ['innerFinally', 'outerCatch', 'outerFinally'] == visitSequence
+        '''
+    }
+}


Mime
View raw message