You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/11/11 22:25:19 UTC

[groovy] branch master updated: GROOVY-7204, GROOVY-8059: step back

This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new e47055241a GROOVY-7204, GROOVY-8059: step back
e47055241a is described below

commit e47055241a5aebbd1826fe8ac88dc5f6d645128f
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Fri Nov 11 16:14:02 2022 -0600

    GROOVY-7204, GROOVY-8059: step back
---
 .../transform/stc/StaticTypeCheckingSupport.java   | 49 +++----------
 .../{Groovy7204Bug.groovy => Groovy7204.groovy}    | 84 +++++++++++++++-------
 src/test/groovy/bugs/Groovy8059Bug.groovy          | 49 -------------
 src/test/groovy/bugs/Groovy8090.groovy             | 74 +++++++++++++++++++
 src/test/groovy/bugs/Groovy8090Bug.groovy          | 69 ------------------
 .../groovy/transform/stc/GenericsSTCTest.groovy    | 10 +--
 .../transform/stc/STCnAryExpressionTest.groovy     |  4 +-
 .../InheritConstructorsTransformTest.groovy        |  2 +-
 .../traitx/TraitASTTransformationTest.groovy       |  2 +-
 9 files changed, 152 insertions(+), 191 deletions(-)

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 9d4af3f7fc..57eef99ea9 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -60,7 +60,6 @@ import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
 import java.util.LinkedList;
@@ -1053,29 +1052,22 @@ public abstract class StaticTypeCheckingSupport {
     }
 
     private static List<MethodNode> chooseBestMethods(final ClassNode receiver, Collection<MethodNode> methods, final ClassNode[] argumentTypes) {
-        int bestDist = Integer.MAX_VALUE;
         List<MethodNode> bestMethods = new ArrayList<>();
 
         // phase 1: argument-parameter distance classifier
+        int bestDist = Integer.MAX_VALUE;
         for (MethodNode method : methods) {
-            ClassNode declaringClass = method.getDeclaringClass();
-            ClassNode actualReceiver = receiver != null ? receiver : declaringClass;
-
-            Map<GenericsType, GenericsType> spec;
-            if (method.isStatic()) {
-                spec = Collections.emptyMap(); // none visible
-            } else {
-                spec = GenericsUtils.makeDeclaringAndActualGenericsTypeMapOfExactType(declaringClass, actualReceiver);
-                GenericsType[] methodGenerics = method.getGenericsTypes();
-                if (methodGenerics != null) { // GROOVY-10322: remove hidden type parameters
-                    for (int i = 0, n = methodGenerics.length; i < n && !spec.isEmpty(); i += 1) {
-                        for (Iterator<GenericsType> it = spec.keySet().iterator(); it.hasNext(); ) {
-                            if (it.next().getName().equals(methodGenerics[i].getName())) it.remove();
-                        }
-                    }
+            Parameter[] parameters = method.getParameters();
+            int nParameters = parameters.length;
+            if (nParameters > 0) {
+                parameters = parameters.clone();
+                for (int i = 0; i < nParameters; i += 1) {
+                    Parameter p = parameters[i];
+                    ClassNode t = p.getOriginType();
+                    if (t.isGenericsPlaceHolder() || isUsingGenericsOrIsArrayUsingGenerics(t))
+                        parameters[i] = new Parameter(t.getPlainNodeReference(), p.getName());
                 }
             }
-            Parameter[] parameters = makeRawTypes(method.getParameters(), spec);
 
             int dist = measureParametersAndArgumentsDistance(parameters, argumentTypes);
             if (dist >= 0 && dist <= bestDist) {
@@ -1178,27 +1170,6 @@ public abstract class StaticTypeCheckingSupport {
         return getDistance(actualReceiverForDistance, declaringClassForDistance);
     }
 
-    private static Parameter[] makeRawTypes(final Parameter[] parameters, final Map<GenericsType, GenericsType> genericsPlaceholderAndTypeMap) {
-        return Arrays.stream(parameters).map(param -> {
-            String name = param.getType().getUnresolvedName();
-            Optional<GenericsType> value = genericsPlaceholderAndTypeMap.entrySet().stream()
-                .filter(e -> e.getKey().getName().equals(name)).findFirst().map(Map.Entry::getValue);
-            ClassNode type = value.map(gt -> !gt.isPlaceholder() ? gt.getType() : makeRawType(gt.getType())).orElseGet(() -> makeRawType(param.getType()));
-
-            return new Parameter(type, param.getName());
-        }).toArray(Parameter[]::new);
-    }
-
-    private static ClassNode makeRawType(final ClassNode receiver) {
-        if (receiver.isArray()) {
-            return makeRawType(receiver.getComponentType()).makeArray();
-        }
-        ClassNode raw = receiver.getPlainNodeReference();
-        raw.setUsingGenerics(false);
-        raw.setGenericsTypes(null);
-        return raw;
-    }
-
     private static List<MethodNode> removeCovariantsAndInterfaceEquivalents(final Collection<MethodNode> collection, final boolean disjoint) {
         List<MethodNode> list = new ArrayList<>(new LinkedHashSet<>(collection)), toBeRemoved = new ArrayList<>();
         for (int i = 0, n = list.size(); i < n - 1; i += 1) {
diff --git a/src/test/groovy/bugs/Groovy7204Bug.groovy b/src/test/groovy/bugs/Groovy7204.groovy
similarity index 89%
rename from src/test/groovy/bugs/Groovy7204Bug.groovy
rename to src/test/groovy/bugs/Groovy7204.groovy
index 749dbc5ddb..e86c5654b8 100644
--- a/src/test/groovy/bugs/Groovy7204Bug.groovy
+++ b/src/test/groovy/bugs/Groovy7204.groovy
@@ -23,13 +23,13 @@ import org.junit.Test
 
 import static groovy.test.GroovyAssert.assertScript
 
-final class Groovy7204Bug {
+final class Groovy7204 {
 
     private final GroovyShell shell = GroovyShell.withConfig {
         imports { star 'groovy.transform' }
     }
 
-    @Test
+    @NotYetImplemented @Test
     void testTypeChecked1() {
         assertScript shell, '''
             @TypeChecked
@@ -69,7 +69,7 @@ final class Groovy7204Bug {
         '''
     }
 
-    @Test
+    @NotYetImplemented @Test
     void testTypeChecked2() {
         assertScript shell, '''
             @TypeChecked
@@ -109,7 +109,7 @@ final class Groovy7204Bug {
         '''
     }
 
-    @Test
+    @NotYetImplemented @Test
     void testTypeChecked3() {
         assertScript shell, '''
             @TypeChecked
@@ -153,7 +153,7 @@ final class Groovy7204Bug {
         '''
     }
 
-    @Test
+    @NotYetImplemented @Test
     void testTypeChecked4() {
         assertScript shell, '''
             @TypeChecked
@@ -197,7 +197,7 @@ final class Groovy7204Bug {
         '''
     }
 
-    @Test
+    @NotYetImplemented @Test
     void testTypeChecked5() {
         assertScript shell, '''
             @TypeChecked
@@ -241,9 +241,27 @@ final class Groovy7204Bug {
         '''
     }
 
+    @NotYetImplemented @Test
+    void testTypeChecked6() {
+        assertScript shell, '''
+            class Repository<T, S extends Serializable> {
+                void delete(T arg) { assert true }
+                void delete(S arg) { assert false: 'wrong method invoked' }
+            }
+
+            @TypeChecked
+            def test() {
+                Repository<String, Long> r = new Repository<String, Long>()
+                r.delete('foo')
+            }
+
+            test()
+        '''
+    }
+
     //
 
-    @Test
+    @NotYetImplemented @Test
     void testCompileStatic1() {
         assertScript shell, '''
             @CompileStatic
@@ -283,7 +301,7 @@ final class Groovy7204Bug {
         '''
     }
 
-    @Test
+    @NotYetImplemented @Test
     void testCompileStatic2() {
         assertScript shell, '''
             @CompileStatic
@@ -323,7 +341,7 @@ final class Groovy7204Bug {
         '''
     }
 
-    @Test
+    @NotYetImplemented @Test
     void testCompileStatic3() {
         assertScript shell, '''
             @CompileStatic
@@ -367,7 +385,7 @@ final class Groovy7204Bug {
         '''
     }
 
-    @Test
+    @NotYetImplemented @Test
     void testCompileStatic4() {
         assertScript shell, '''
             @CompileStatic
@@ -411,7 +429,7 @@ final class Groovy7204Bug {
         '''
     }
 
-    @Test
+    @NotYetImplemented @Test
     void testCompileStatic5() {
         assertScript shell, '''
             @CompileStatic
@@ -458,7 +476,6 @@ final class Groovy7204Bug {
     @NotYetImplemented @Test
     void testCompileStatic6() {
         assertScript shell, '''
-            @CompileStatic
             class Repository<T, S extends Serializable> {
                 void delete(T arg) { assert true }
                 void delete(S arg) { assert false: 'wrong method invoked' }
@@ -474,47 +491,64 @@ final class Groovy7204Bug {
         '''
     }
 
-    @Test
+    @NotYetImplemented @Test // GROOVY-8059
     void testCompileStatic7() {
         assertScript shell, '''
+            abstract class A<K extends Serializable, V> {
+                void delete(K key) {}
+                void delete(V val) {}
+            }
+            class C extends A<String, Integer> {
+            }
+
             @CompileStatic
+            class Test {
+                Test() {
+                    def obj = new C()
+                    obj.delete(Integer.valueOf(1))
+                }
+            }
+
+            new Test()
+        '''
+    }
+
+    @Test
+    void testCompileStatic8() {
+        assertScript shell, '''
             class Trie<T> {
             }
 
             @CompileStatic
             class Base<T> {
-                protected List<Trie<T>> list
-
+                protected List<Trie<T>> list = []
                 Base() {
-                    list = new ArrayList<Trie<T>>()
-                    list.add(new Trie<String>()) // should fail?
+                    list.add(new Trie<String>()) // should fail!!
                 }
             }
 
             @CompileStatic
-            class Derived extends Base<String> {
+            class Test extends Base<String> {
                 Trie<String> getFirstElement() {
                     list.get(0)
                 }
             }
 
-            assert new Derived().getFirstElement() instanceof Trie
+            assert new Test().firstElement instanceof Trie
         '''
     }
 
     @Test
-    void testCompileStatic8() {
+    void testCompileStatic9() {
         assertScript shell, '''
-            @CompileStatic
             class Trie<T> {
             }
 
-            @CompileStatic
             class Base<T> extends ArrayList<Trie<T>> {
             }
 
-            @groovy.transform.CompileStatic
-            class Derived extends Base<String> {
+            @CompileStatic
+            class Test extends Base<String> {
                 Derived() {
                     add(new Trie<String>())
                 }
@@ -523,7 +557,7 @@ final class Groovy7204Bug {
                 }
             }
 
-            assert new Derived().firstElement instanceof Trie
+            assert new Test().firstElement instanceof Trie
         '''
     }
 }
diff --git a/src/test/groovy/bugs/Groovy8059Bug.groovy b/src/test/groovy/bugs/Groovy8059Bug.groovy
deleted file mode 100644
index 05f7f3755e..0000000000
--- a/src/test/groovy/bugs/Groovy8059Bug.groovy
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- *  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 groovy.test.GroovyTestCase
-
-class Groovy8059Bug extends GroovyTestCase {
-    void test1() {
-        assertScript '''
-        @groovy.transform.CompileStatic
-        class Base<K extends Serializable, V> {
-            void delete(K key) {}
-            void delete(V value) {}
-        }
-
-        @groovy.transform.CompileStatic
-        class Foo extends Base<String, Integer> {}
-
-        @groovy.transform.CompileStatic
-        public class Class1 {
-            Class1() {
-                Foo foo = new Foo()
-                foo.delete(Integer.valueOf(1))
-            }
-        }
-        new Class1()
-        '''
-    }
-}
diff --git a/src/test/groovy/bugs/Groovy8090.groovy b/src/test/groovy/bugs/Groovy8090.groovy
new file mode 100644
index 0000000000..ea32acbf72
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy8090.groovy
@@ -0,0 +1,74 @@
+/*
+ *  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 org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+
+final class Groovy8090 {
+
+    @Test
+    void test1() {
+        assertScript '''
+            import static java.util.Arrays.asList
+
+            @groovy.transform.CompileStatic
+            class C {
+                def <T> Iterable<T> foo(T instance) { asList(instance) }
+                def <U> Iterable<U> bar(U instance) { asList(instance) }
+                Iterable<String> baz(String instance) { asList(instance) }
+            }
+
+            new C().with {
+                assert foo('A') + bar('B') + baz('C') == ['A', 'B', 'C']
+            }
+        '''
+    }
+
+    @Test
+    void test2() {
+        assertScript '''
+            @groovy.transform.CompileStatic
+            class C {
+                def <U> Iterable<U> bar(U instance) { Arrays.asList(instance) }
+            }
+
+            assert new C().bar('B') == ['B']
+        '''
+    }
+
+    @Test
+    void test3() {
+        assertScript '''
+            class A {
+                static <T> List<T> asList(T a) {
+                    return [a]
+                }
+            }
+
+            @groovy.transform.CompileStatic
+            class C {
+                def <U> Iterable<U> bar(U instance) { A.asList(instance) }
+            }
+
+            assert new C().bar('B') == ['B']
+        '''
+    }
+}
diff --git a/src/test/groovy/bugs/Groovy8090Bug.groovy b/src/test/groovy/bugs/Groovy8090Bug.groovy
deleted file mode 100644
index 5b5652fc50..0000000000
--- a/src/test/groovy/bugs/Groovy8090Bug.groovy
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- *  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']
-        '''
-    }
-}
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index 36fdd1d997..8f5e047000 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -4033,13 +4033,13 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
     // GROOVY-6233
     void testConstructorArgumentsAgainstGenerics() {
         shouldFailWithMessages '''
-            class Foo<T>{  Foo(T a, T b){} }
-            def bar() {
-                Foo<Map> f = new Foo<Map>("a",1)
+            class C<T> {
+                C(T a, T b) {
+                }
             }
-            bar()
+            def c_of_map = new C<Map>("a", 1)
         ''',
-        'Cannot find matching method Foo#<init>(java.lang.String, int)'
+        'Cannot call C#<init>(java.util.Map, java.util.Map) with arguments [java.lang.String, int]'
     }
 
     // GROOVY-5742
diff --git a/src/test/groovy/transform/stc/STCnAryExpressionTest.groovy b/src/test/groovy/transform/stc/STCnAryExpressionTest.groovy
index e79079f2e3..f67192b670 100644
--- a/src/test/groovy/transform/stc/STCnAryExpressionTest.groovy
+++ b/src/test/groovy/transform/stc/STCnAryExpressionTest.groovy
@@ -257,13 +257,13 @@ class STCnAryExpressionTest extends StaticTypeCheckingTestCase {
         shouldFailWithMessages '''
            'abc' < 1
         ''',
-        'Cannot find matching method java.lang.String#compareTo(int)'
+        'Cannot call java.lang.String#compareTo(java.lang.String) with arguments [int]'
     }
 
     void testCompareToCallCheckWithIncompatibleTypesAlsoFailsIfComparableImplemented() {
         shouldFailWithMessages '''
            'abc'.compareTo(1)
         ''',
-        'Cannot find matching method java.lang.String#compareTo(int)'
+        'Cannot call java.lang.String#compareTo(java.lang.String) with arguments [int]'
     }
 }
diff --git a/src/test/org/codehaus/groovy/transform/InheritConstructorsTransformTest.groovy b/src/test/org/codehaus/groovy/transform/InheritConstructorsTransformTest.groovy
index 05e31a0f54..b8e2dd33f5 100644
--- a/src/test/org/codehaus/groovy/transform/InheritConstructorsTransformTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/InheritConstructorsTransformTest.groovy
@@ -260,7 +260,7 @@ final class InheritConstructorsTransformTest {
             assert op.toString() == '3|DOWN'
         '''
         assert err.message.contains('Cannot call OrderPublisher#<init>(java.util.Deque<java.lang.Integer>) with arguments [java.util.LinkedList<java.lang.String>]')
-        assert err.message.contains('Cannot find matching method OrderPublisher#<init>(java.util.Date)')
+        assert err.message.contains('Cannot call OrderPublisher#<init>(java.math.RoundingMode) with arguments [java.util.Date]')
         assert err.message.contains('Cannot call OrderPublisher#<init>(java.util.Set<java.math.RoundingMode>) with arguments [java.util.HashSet<java.util.Date>]')
     }
 
diff --git a/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy b/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
index 1af26fb1c3..d5a375ff64 100644
--- a/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
@@ -2312,7 +2312,7 @@ final class TraitASTTransformationTest {
             test()
         '''
 
-        assert err =~ 'Cannot find matching method Dummy#set\\(java.lang.String\\)'
+        assert err =~ /Cannot call Dummy#set\(App\) with arguments \[java.lang.String\]/
     }
 
     @Test