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:21:28 GMT
Repository: groovy
Updated Branches:
  refs/heads/master a1626fb4b -> e219dd8dc


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/41d9e982
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/41d9e982
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/41d9e982

Branch: refs/heads/master
Commit: 41d9e9829f395096ddc0cc128180200eee06cf64
Parents: a1626fb
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:04:58 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/41d9e982/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 d49837b..2e36887 100644
--- a/src/main/org/codehaus/groovy/transform/trait/TraitComposer.java
+++ b/src/main/org/codehaus/groovy/transform/trait/TraitComposer.java
@@ -294,7 +294,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/41d9e982/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