groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pa...@apache.org
Subject groovy git commit: GROOVY-8325: @CompileStatic calls wrong newInstance method
Date Mon, 16 Oct 2017 05:42:07 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_4_X e2660dbe6 -> d965023c2


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

Branch: refs/heads/GROOVY_2_4_X
Commit: d965023c2668991e744c213c5125c5adcc677cd9
Parents: e2660db
Author: paulk <paulk@asert.com.au>
Authored: Mon Oct 16 15:39:26 2017 +1000
Committer: paulk <paulk@asert.com.au>
Committed: Mon Oct 16 15:41:56 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/d965023c/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 b6ce6d5..0790f1f 100644
--- a/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -346,32 +346,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/d965023c/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/d965023c/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]")
     }
 
 }


Mime
View raw message