groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject groovy git commit: GROOVY-8090: Incorrectly processing method-level type generics information(closes #789)
Date Sun, 26 Aug 2018 09:36:23 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X 40b632943 -> 4e40942dc


GROOVY-8090: Incorrectly processing method-level type generics information(closes #789)

(cherry picked from commit 0395aff7e9ac00d97f2bc1ff7ee10d6034cf495b)


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

Branch: refs/heads/GROOVY_2_5_X
Commit: 4e40942dcda1ad758cd23d70cf64807c3d1a5871
Parents: 40b6329
Author: Daniel Sun <sunlan@apache.org>
Authored: Sun Aug 26 16:32:35 2018 +0800
Committer: Daniel Sun <sunlan@apache.org>
Committed: Sun Aug 26 17:36:10 2018 +0800

----------------------------------------------------------------------
 .../stc/StaticTypeCheckingSupport.java          |  9 +++
 src/test/groovy/bugs/Groovy8090Bug.groovy       | 69 ++++++++++++++++++++
 2 files changed, 78 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/4e40942d/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
index 8135ca7..69415e7 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -1555,6 +1555,15 @@ public abstract class StaticTypeCheckingSupport {
     private static boolean inferenceCheck(Set<GenericsTypeName> fixedGenericsPlaceHolders,
Map<GenericsTypeName, GenericsType> resolvedMethodGenerics, ClassNode type, ClassNode
wrappedArgument, boolean lastArg) {
         Map<GenericsTypeName, GenericsType> connections = new HashMap<GenericsTypeName,
GenericsType>();
         if (isPrimitiveType(wrappedArgument)) wrappedArgument = getWrapper(wrappedArgument);
+
+        if (lastArg &&
+                type.isArray() && type.getComponentType().isGenericsPlaceHolder()
&&
+                !wrappedArgument.isArray() && wrappedArgument.isGenericsPlaceHolder())
{
+            // GROOVY-8090 handle generics varargs, e.g. `U x = ...; Arrays.asList(x)`
+            // we should connect the type of vararg(e.g. T is the type of T...) to the argument
type
+
+            type = type.getComponentType();
+        }
         // the context we compare with in the end is the one of the callsite
         // so far we specified the context of the method declaration only
         // thus for each argument, we try to find the connected generics first

http://git-wip-us.apache.org/repos/asf/groovy/blob/4e40942d/src/test/groovy/bugs/Groovy8090Bug.groovy
----------------------------------------------------------------------
diff --git a/src/test/groovy/bugs/Groovy8090Bug.groovy b/src/test/groovy/bugs/Groovy8090Bug.groovy
new file mode 100644
index 0000000..de37a94
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy8090Bug.groovy
@@ -0,0 +1,69 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package groovy.bugs
+
+import gls.CompilableTestSupport
+
+class Groovy8090Bug extends CompilableTestSupport {
+    void testGroovy8090() {
+        assertScript '''
+        import static java.util.Arrays.asList
+        
+        @groovy.transform.CompileStatic
+        class Main {
+            final <T> Iterable<T> foo(T instance) { asList(instance) }
+            final <U> Iterable<U> bar(U instance) { asList(instance) }
+            final Iterable<String> baz(String instance) { asList(instance) }
+        }
+        
+        new Main().with {
+            assert foo('A') + bar('B') + baz('C') == ['A', 'B', 'C']
+        }
+        '''
+    }
+
+    void test2() {
+        assertScript '''
+        @groovy.transform.CompileStatic
+        class Main {
+            final <U> Iterable<U> bar(U instance) { Arrays.asList(instance) }
+        }
+        
+        assert new Main().bar('B') == ['B']
+        '''
+    }
+
+    void test3() {
+        assertScript '''
+        @groovy.transform.CompileStatic
+        class Arraysx {
+            static <T> List<T> asList(T a) {
+                return [a]
+            }
+        }
+        
+        @groovy.transform.CompileStatic
+        class Main {
+            final <U> Iterable<U> bar(U instance) { Arraysx.asList(instance)
}
+        }
+        
+        assert new Main().bar('B') == ['B']
+        '''
+    }
+}


Mime
View raw message