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 09:48:57 UTC

[groovy] branch danielsun/tweak-GROOVY9126 created (now 3b15555)

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

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


      at 3b15555  Tweak GROOVY-9126: Eliminate more unreachable bytecode

This branch includes the following new commits:

     new 3b15555  Tweak GROOVY-9126: Eliminate more unreachable bytecode

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[groovy] 01/01: Tweak GROOVY-9126: Eliminate more unreachable bytecode

Posted by su...@apache.org.
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

commit 3b15555adf74b88e3e69ba02926dd27581458721
Author: Daniel Sun <su...@apache.org>
AuthorDate: Wed Sep 30 17:48:43 2020 +0800

    Tweak GROOVY-9126: Eliminate more unreachable bytecode
---
 .../groovy/classgen/AsmClassGenerator.java         | 18 ++++++++++++
 src/test/groovy/bugs/Groovy9126.groovy             | 33 +++++++++++++++++++++-
 2 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
index 2010e02..9df389d 100644
--- a/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
+++ b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
@@ -461,6 +461,9 @@ public class AsmClassGenerator extends ClassGenerator {
         super.visitConstructorOrMethod(node, isConstructor);
 
         controller.getCompileStack().clear();
+
+        if (checkIfLastStatementIsReturn(code)) return;
+
         if (node.isVoidMethod()) {
             mv.visitInsn(RETURN);
         } else {
@@ -478,6 +481,21 @@ public class AsmClassGenerator extends ClassGenerator {
         }
     }
 
+    private boolean checkIfLastStatementIsReturn(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) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
     private void visitAnnotationDefaultExpression(final AnnotationVisitor av, final ClassNode type, final Expression exp) {
         if (exp instanceof ClosureExpression) {
             ClassNode closureClass = controller.getClosureWriter().getOrAddClosureClass((ClosureExpression) exp, ACC_PUBLIC);
diff --git a/src/test/groovy/bugs/Groovy9126.groovy b/src/test/groovy/bugs/Groovy9126.groovy
index 3df9633..69ec5ff 100644
--- a/src/test/groovy/bugs/Groovy9126.groovy
+++ b/src/test/groovy/bugs/Groovy9126.groovy
@@ -45,7 +45,38 @@ final class Groovy9126 extends AbstractBytecodeTestCase {
                  'IMUL',
                  'IRETURN',
                  'L1',
-                 'FRAME FULL [] [java/lang/Throwable]']
+                 'LOCALVARIABLE this Lscript; L0 L1 0']
+        )
+    }
+
+    @Test
+    void testUnreachableBytecode2() {
+        def bytecode = compile([method:'nonVoidMethod'],'''
+            @groovy.transform.CompileStatic
+            def nonVoidMethod() {
+                println 123
+                567
+            }
+        ''')
+
+        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',
+                 'SIPUSH 567',
+                 'INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;',
+                 'ARETURN',
+                 'L2',
+                 'LOCALVARIABLE this Lscript; L0 L2 0']
         )
     }
 }