You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2020/09/30 10:32:14 UTC

[groovy] branch danielsun/tweak-GROOVY9126 updated: Tweak GROOVY-9126 further: Eliminate more unreachable bytecode

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

sunlan pushed a commit to branch danielsun/tweak-GROOVY9126
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/danielsun/tweak-GROOVY9126 by this push:
     new c76f67e  Tweak GROOVY-9126 further: Eliminate more unreachable bytecode
c76f67e is described below

commit c76f67eff59e8d001da9b62b08fa4c95658b5574
Author: Daniel Sun <su...@apache.org>
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 9df389d..c7cadfe 100644
--- a/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
+++ b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
@@ -462,7 +462,7 @@ public class AsmClassGenerator extends ClassGenerator {
 
         controller.getCompileStack().clear();
 
-        if (checkIfLastStatementIsReturn(code)) return;
+        if (checkIfLastStatementIsReturnOrThrow(code)) return;
 
         if (node.isVoidMethod()) {
             mv.visitInsn(RETURN);
@@ -481,18 +481,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']
+        )
+    }
 }