groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject [1/2] groovy git commit: GROOVY-7926 - Don't use a CastExpression if the return type is void.
Date Sun, 11 Sep 2016 22:22:18 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X a691f510a -> 96f1ea754


GROOVY-7926 - Don't use a CastExpression if the return type is void.


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

Branch: refs/heads/GROOVY_2_4_X
Commit: 50d4d1f13d0e75ea5067e145f8310e4f490988b8
Parents: a691f51
Author: Graeme Rocher <graeme.rocher@gmail.com>
Authored: Thu Sep 8 12:00:08 2016 +0200
Committer: paulk <paulk@asert.com.au>
Committed: Mon Sep 12 08:22:02 2016 +1000

----------------------------------------------------------------------
 .../groovy/transform/trait/TraitComposer.java   |  3 +-
 .../transform/traitx/Groovy7926Bug.groovy       | 50 ++++++++++++++++++++
 2 files changed, 52 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/50d4d1f1/src/main/org/codehaus/groovy/transform/trait/TraitComposer.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/transform/trait/TraitComposer.java b/src/main/org/codehaus/groovy/transform/trait/TraitComposer.java
index b65855c..38e2624 100644
--- a/src/main/org/codehaus/groovy/transform/trait/TraitComposer.java
+++ b/src/main/org/codehaus/groovy/transform/trait/TraitComposer.java
@@ -293,7 +293,8 @@ public abstract class TraitComposer {
 
         ClassNode[] exceptionNodes = correctToGenericsSpecRecurse(genericsSpec, copyExceptions(helperMethod.getExceptions()));
         ClassNode fixedReturnType = correctToGenericsSpecRecurse(genericsSpec, helperMethod.getReturnType());
-        Expression forwardExpression = genericsSpec.isEmpty()?mce:new CastExpression(fixedReturnType,mce);
+        boolean noCastRequired = genericsSpec.isEmpty() || fixedReturnType.getName().equals(ClassHelper.VOID_TYPE.getName());
+        Expression forwardExpression = noCastRequired ? mce : new CastExpression(fixedReturnType,mce);
         int access = helperMethod.getModifiers();
         // we could rely on the first parameter name ($static$self) but that information
is not
         // guaranteed to be always present

http://git-wip-us.apache.org/repos/asf/groovy/blob/50d4d1f1/src/test/org/codehaus/groovy/transform/traitx/Groovy7926Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/transform/traitx/Groovy7926Bug.groovy b/src/test/org/codehaus/groovy/transform/traitx/Groovy7926Bug.groovy
new file mode 100644
index 0000000..ee2d147
--- /dev/null
+++ b/src/test/org/codehaus/groovy/transform/traitx/Groovy7926Bug.groovy
@@ -0,0 +1,50 @@
+package org.codehaus.groovy.transform.traitx
+
+import org.codehaus.groovy.classgen.asm.AbstractBytecodeTestCase
+import org.codehaus.groovy.classgen.asm.InstructionSequence
+
+/**
+ * Created by graemerocher on 08/09/2016.
+ */
+class Groovy7926Bug extends AbstractBytecodeTestCase {
+
+    void testThatVoidTypesFromTraitsWithGenericsWork() {
+        assertScript('''
+trait MyTrait<D> {
+    void delete() {
+        // no-op
+        println "works"
+    }
+}
+class MyImpl implements MyTrait<MyImpl> {
+}
+new MyImpl().delete()
+return true
+''')
+    }
+
+    void testThatVoidTypesAreNotUsedForVariableNamesInByteCode() {
+        def byteCode = compile([method:"delete", classNamePattern:"MyImpl"],"""\
+trait MyTrait<D> {
+    void delete() {
+        // no-op
+        println "works"
+    }
+}
+class MyImpl implements MyTrait<MyImpl> {
+}
+        """)
+
+        def instructions = byteCode.instructions
+        byteCode.instructions = instructions[
+                instructions.indexOf("public delete()V")..-1
+        ]
+        instructions = byteCode.instructions
+        byteCode.instructions = instructions[
+                0..instructions.indexOf( instructions.find { it == '--BEGIN----END--' } )
+        ]
+        assert !byteCode.hasSequence([
+                "CHECKCAST void"
+        ])
+    }
+}


Mime
View raw message