You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2017/10/16 05:39:56 UTC

groovy git commit: GROOVY-8325: @CompileStatic calls wrong newInstance method

Repository: groovy
Updated Branches:
  refs/heads/master 7e1da637a -> 9061c4a15


GROOVY-8325: @CompileStatic calls wrong newInstance method


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

Branch: refs/heads/master
Commit: 9061c4a1572561a6246ac3cc7cf76831d97220b1
Parents: 7e1da63
Author: paulk <pa...@asert.com.au>
Authored: Mon Oct 16 15:39:26 2017 +1000
Committer: paulk <pa...@asert.com.au>
Committed: Mon Oct 16 15:39:50 2017 +1000

----------------------------------------------------------------------
 .../stc/StaticTypeCheckingSupport.java          | 28 ++-------------
 .../groovy/transform/stc/MiscSTCTest.groovy     | 38 ++++++++++++++++++++
 .../stc/StaticTypeCheckingTestCase.groovy       |  7 ++--
 3 files changed, 44 insertions(+), 29 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/9061c4a1/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 eb208fe..30584a9 100644
--- a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -324,32 +324,8 @@ public abstract class StaticTypeCheckingSupport {
         }
         if (isPrimitiveType(toBeAssignedTo)) toBeAssignedTo = getWrapper(toBeAssignedTo);
         if (isPrimitiveType(type)) type = getWrapper(type);
-        if (Double_TYPE==toBeAssignedTo) {
-            return type.isDerivedFrom(Number_TYPE);
-        }
-        if (Float_TYPE==toBeAssignedTo) {
-            return type.isDerivedFrom(Number_TYPE) && Double_TYPE!=type.redirect();
-        }
-        if (Long_TYPE==toBeAssignedTo) {
-            return type.isDerivedFrom(Number_TYPE)
-                    && Double_TYPE!=type.redirect()
-                    && Float_TYPE!=type.redirect();
-        }
-        if (Integer_TYPE==toBeAssignedTo) {
-            return type.isDerivedFrom(Number_TYPE)
-                    && Double_TYPE!=type.redirect()
-                    && Float_TYPE!=type.redirect()
-                    && Long_TYPE!=type.redirect();
-        }
-        if (Short_TYPE==toBeAssignedTo) {
-            return type.isDerivedFrom(Number_TYPE)
-                    && Double_TYPE!=type.redirect()
-                    && Float_TYPE!=type.redirect()
-                    && Long_TYPE!=type.redirect()
-                    && Integer_TYPE!=type.redirect();
-        }
-        if (Byte_TYPE==toBeAssignedTo) {
-            return type.redirect() == Byte_TYPE;
+        if (NUMBER_TYPES.containsKey(type.redirect()) && NUMBER_TYPES.containsKey(toBeAssignedTo.redirect())) {
+            return NUMBER_TYPES.get(type.redirect()) <= NUMBER_TYPES.get(toBeAssignedTo.redirect());
         }
         if (type.isArray() && toBeAssignedTo.isArray()) {
             return isAssignableTo(type.getComponentType(),toBeAssignedTo.getComponentType());

http://git-wip-us.apache.org/repos/asf/groovy/blob/9061c4a1/src/test/groovy/transform/stc/MiscSTCTest.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/transform/stc/MiscSTCTest.groovy b/src/test/groovy/transform/stc/MiscSTCTest.groovy
index 94d8c0e..fd132e3 100644
--- a/src/test/groovy/transform/stc/MiscSTCTest.groovy
+++ b/src/test/groovy/transform/stc/MiscSTCTest.groovy
@@ -351,4 +351,42 @@ class MiscSTCTest extends StaticTypeCheckingTestCase {
             assert ifThenElseLocalVar2(new FooBase()) == null
         '''
     }
+
+    // GROOVY-8325
+    void testNumericCoercion() {
+        assertScript '''
+            class Foo {
+                Long val
+                static Foo newInstance(Long val) {
+                    return new Foo(val: val)
+                }
+            }
+            class FooFactory {
+                static Foo create() {
+                    Foo.newInstance(123)
+                }
+            }
+            assert FooFactory.create().val == 123
+        '''
+    }
+
+    void testNumericCoercionWithCustomNumber() {
+        shouldFailWithMessages '''
+            class CustomNumber extends Number {
+                @Delegate Long delegate = 42L
+            }
+            class Foo {
+                Integer val
+                static Foo newInstance2(Integer val) {
+                    return new Foo(val: val)
+                }
+            }
+            class FooFactory {
+                static Foo create() {
+                    Foo.newInstance2(new CustomNumber())
+                }
+            }
+        ''', 'Cannot find matching method Foo#newInstance2(CustomNumber)'
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/9061c4a1/src/test/groovy/transform/stc/StaticTypeCheckingTestCase.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/transform/stc/StaticTypeCheckingTestCase.groovy b/src/test/groovy/transform/stc/StaticTypeCheckingTestCase.groovy
index e42600a..c7c2262 100644
--- a/src/test/groovy/transform/stc/StaticTypeCheckingTestCase.groovy
+++ b/src/test/groovy/transform/stc/StaticTypeCheckingTestCase.groovy
@@ -27,8 +27,6 @@ import org.codehaus.groovy.control.customizers.ImportCustomizer
 
 /**
  * Support class for static type checking test cases.
- *
- * @author Cedric Champeau
  */
 abstract class StaticTypeCheckingTestCase extends GroovyTestCase {
     protected CompilerConfiguration config
@@ -86,8 +84,11 @@ abstract class StaticTypeCheckingTestCase extends GroovyTestCase {
             if (success && mce.errorCollector.errorCount!=messages.length) {
                 throw new AssertionError("Expected error messages were found, but compiler threw additional errors : " + mce.toString())
             }
+            if (!success) {
+                throw new AssertionError("Not all expected error messages were found, compiler threw these errors : " + mce.toString())
+            }
         }
-        if (!success) throw new AssertionError("Test should have failed with messages [$messages]")
+        if (!success) throw new AssertionError("Test passed but should have failed with messages [$messages]")
     }
 
 }