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 11:04:10 UTC

[groovy] branch master updated (4acca28 -> 59e14e1)

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

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


    from 4acca28  GROOVY-9719: add test case
     new 7001afe  Tweak GROOVY-9126: Eliminate more unreachable bytecode
     new 946eaa4  Tweak GROOVY-9126 further: Eliminate more unreachable bytecode
     new 59e14e1  Remove unused code for debugging

The 3 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.


Summary of changes:
 .../groovy/classgen/AsmClassGenerator.java         | 19 +++++++
 src/test/groovy/bugs/Groovy9126.groovy             | 66 +++++++++++++++++++++-
 2 files changed, 84 insertions(+), 1 deletion(-)


[groovy] 01/03: 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 master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 7001afedf510b31484128f91d04c0c966bce9967
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 72024f0..9affbee 100644
--- a/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
+++ b/src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
@@ -514,6 +514,9 @@ public class AsmClassGenerator extends ClassGenerator {
         super.visitConstructorOrMethod(node, isConstructor);
 
         controller.getCompileStack().clear();
+
+        if (checkIfLastStatementIsReturn(code)) return;
+
         if (node.isVoidMethod()) {
             mv.visitInsn(RETURN);
         } else {
@@ -531,6 +534,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']
         )
     }
 }


[groovy] 02/03: Tweak GROOVY-9126 further: 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 master
in repository https://gitbox.apache.org/repos/asf/groovy.git

commit 946eaa496c8319dc8d067c39fffac9978e1729a0
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 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']
+        )
+    }
 }


[groovy] 03/03: Remove unused code for debugging

Posted by su...@apache.org.
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 59e14e1d6d4a21dbba2222963f9984dda9f15e18
Author: Daniel Sun <su...@apache.org>
AuthorDate: Wed Sep 30 18:53:18 2020 +0800

    Remove unused code for debugging
---
 src/test/groovy/bugs/Groovy9126.groovy | 2 --
 1 file changed, 2 deletions(-)

diff --git a/src/test/groovy/bugs/Groovy9126.groovy b/src/test/groovy/bugs/Groovy9126.groovy
index c08dc9b..fb72f9c 100644
--- a/src/test/groovy/bugs/Groovy9126.groovy
+++ b/src/test/groovy/bugs/Groovy9126.groovy
@@ -90,8 +90,6 @@ final class Groovy9126 extends AbstractBytecodeTestCase {
             }
         ''')
 
-        println bytecode
-
         assert bytecode.hasStrictSequence(
                 ['public nonVoidMethod()Ljava/lang/Object;',
                  'L0',