This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 946eaa496c8319dc8d067c39fffac9978e1729a0
Author: Daniel Sun <[email protected]>
AuthorDate: Wed Sep 30 18:31:58 2020 +0800

    Tweak GROOVY-9126 further: Eliminate more unreachable bytecode
---
 .../groovy/classgen/AsmClassGenerator.java         |  7 +++--
 src/test/groovy/bugs/Groovy9126.groovy             | 35 ++++++++++++++++++++++
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java 
b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
index 9affbee..4513f40 100644
--- a/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
+++ b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
@@ -515,7 +515,7 @@ public class AsmClassGenerator extends ClassGenerator {
 
         controller.getCompileStack().clear();
 
-        if (checkIfLastStatementIsReturn(code)) return;
+        if (checkIfLastStatementIsReturnOrThrow(code)) return;
 
         if (node.isVoidMethod()) {
             mv.visitInsn(RETURN);
@@ -534,18 +534,19 @@ public class AsmClassGenerator extends ClassGenerator {
         }
     }
 
-    private boolean checkIfLastStatementIsReturn(Statement code) {
+    private boolean checkIfLastStatementIsReturnOrThrow(Statement code) {
         if (code instanceof BlockStatement) {
             BlockStatement blockStatement = (BlockStatement) code;
             List<Statement> statementList = blockStatement.getStatements();
             int statementCnt = statementList.size();
             if (statementCnt > 0) {
                 Statement lastStatement = statementList.get(statementCnt - 1);
-                if (lastStatement instanceof ReturnStatement) {
+                if (lastStatement instanceof ReturnStatement || lastStatement 
instanceof ThrowStatement) {
                     return true;
                 }
             }
         }
+
         return false;
     }
 
diff --git a/src/test/groovy/bugs/Groovy9126.groovy 
b/src/test/groovy/bugs/Groovy9126.groovy
index 69ec5ff..c08dc9b 100644
--- a/src/test/groovy/bugs/Groovy9126.groovy
+++ b/src/test/groovy/bugs/Groovy9126.groovy
@@ -79,4 +79,39 @@ final class Groovy9126 extends AbstractBytecodeTestCase {
                  'LOCALVARIABLE this Lscript; L0 L2 0']
         )
     }
+
+    @Test
+    void testUnreachableBytecode3() {
+        def bytecode = compile([method:'nonVoidMethod'],'''
+            @groovy.transform.CompileStatic
+            def nonVoidMethod() {
+                println 123
+                throw new RuntimeException()
+            }
+        ''')
+
+        println bytecode
+
+        assert bytecode.hasStrictSequence(
+                ['public nonVoidMethod()Ljava/lang/Object;',
+                 'L0',
+                 'LINENUMBER 4 L0',
+                 'ALOAD 0',
+                 'CHECKCAST script',
+                 'BIPUSH 123',
+                 'INVOKESTATIC java/lang/Integer.valueOf 
(I)Ljava/lang/Integer;',
+                 'INVOKEVIRTUAL script.println (Ljava/lang/Object;)V',
+                 'ACONST_NULL',
+                 'POP',
+                 'L1',
+                 'LINENUMBER 5 L1',
+                 'NEW java/lang/RuntimeException',
+                 'DUP',
+                 'INVOKESPECIAL java/lang/RuntimeException.<init> ()V',
+                 'CHECKCAST java/lang/Throwable',
+                 'ATHROW',
+                 'L2',
+                 'LOCALVARIABLE this Lscript; L0 L2 0']
+        )
+    }
 }

Reply via email to