groovy git commit: GROOVY-7420: Prefer a method signature without boxing or unboxing (closes #204)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

groovy git commit: GROOVY-7420: Prefer a method signature without boxing or unboxing (closes #204)

pascalschumacher
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X 7e7f8053e -> 33fce5f73


GROOVY-7420: Prefer a method signature without boxing or unboxing (closes #204)

The method resolution mechanism is closer to what is done when following the
Java Language Specification (JLS 15.2.2), and removes the ambiguity when
calling a method having both primitive- and object-parameter overloads with
an object argument.


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

Branch: refs/heads/GROOVY_2_4_X
Commit: 33fce5f734e919adf5a44b77bbccca2893044e6c
Parents: 7e7f805
Author: Frank Pavageau <[hidden email]>
Authored: Wed Dec 16 10:05:20 2015 +0100
Committer: pascalschumacher <[hidden email]>
Committed: Wed Dec 16 22:53:58 2015 +0100

----------------------------------------------------------------------
 .../groovy/transform/stc/StaticTypeCheckingSupport.java         | 4 +++-
 .../codehaus/groovy/classgen/asm/sc/bugs/Groovy7420Bug.groovy   | 5 +++--
 2 files changed, 6 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/33fce5f7/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
index 4760cb3..eb6bac9 100644
--- a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -816,7 +816,9 @@ public abstract class StaticTypeCheckingSupport {
                 && unwrapReceiver!=unwrapCompare) {
             dist = getPrimitiveDistance(unwrapReceiver, unwrapCompare);
         }
-        if (isPrimitiveType(receiver) && !isPrimitiveType(compare)) {
+        // Add a penalty against boxing or unboxing, to get a resolution similar to JLS 15.12.2
+        // (http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2).
+        if (isPrimitiveType(receiver) ^ isPrimitiveType(compare)) {
             dist = (dist+1)<<1;
         }
         if (unwrapCompare.equals(unwrapReceiver)) return dist;

http://git-wip-us.apache.org/repos/asf/groovy/blob/33fce5f7/src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy7420Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy7420Bug.groovy b/src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy7420Bug.groovy
index 82642e1..5205ee0 100644
--- a/src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy7420Bug.groovy
+++ b/src/test/org/codehaus/groovy/classgen/asm/sc/bugs/Groovy7420Bug.groovy
@@ -18,12 +18,10 @@
  */
 package org.codehaus.groovy.classgen.asm.sc.bugs
 
-import groovy.transform.NotYetImplemented
 import groovy.transform.stc.StaticTypeCheckingTestCase
 import org.codehaus.groovy.classgen.asm.sc.StaticCompilationTestSupport
 
 class Groovy7420Bug extends StaticTypeCheckingTestCase implements StaticCompilationTestSupport {
-    @NotYetImplemented
     void testOverloadedMethodWithPrimitiveOrObjectParameter() {
         assertScript '''
             class A {
@@ -38,6 +36,9 @@ class Groovy7420Bug extends StaticTypeCheckingTestCase implements StaticCompilat
 
             Long l = 42L
             assert A.m(l) == "object"
+            assert A.m(l.longValue()) == "primitive"
+            int i = 42
+            assert A.m(i) == "primitive" // Primitive widening
         '''
     }
 }