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/10/24 21:48:04 UTC

[groovy] branch GROOVY_2_5_X updated: GROOVY-10109, GROOVY-10120: STC: access via public bridge method

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

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


The following commit(s) were added to refs/heads/GROOVY_2_5_X by this push:
     new 40fb9a03e1 GROOVY-10109, GROOVY-10120: STC: access via public bridge method
40fb9a03e1 is described below

commit 40fb9a03e1e31ab085f1a90f4a19508beab68804
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Mon Oct 24 15:14:08 2022 -0500

    GROOVY-10109, GROOVY-10120: STC: access via public bridge method
---
 .../transform/stc/StaticTypeCheckingSupport.java   | 13 ++++------
 src/test/groovy/transform/stc/BugsSTCTest.groovy   | 27 +++++++++++++++++++++
 .../groovy/transform/stc/GenericsSTCTest.groovy    | 28 +++++++++++++++++-----
 3 files changed, 54 insertions(+), 14 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 b61d436931..00f4d0e44e 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java
@@ -1223,13 +1223,11 @@ public abstract class StaticTypeCheckingSupport {
         return raw;
     }
 
-    private static Collection<MethodNode> removeCovariantsAndInterfaceEquivalents(final Collection<MethodNode> collection) {
-        List<MethodNode> toBeRemoved = new ArrayList<>();
-        List<MethodNode> list = new ArrayList<>(new LinkedHashSet<>(collection));
+    private static List<MethodNode> removeCovariantsAndInterfaceEquivalents(final Collection<MethodNode> collection) {
+        List<MethodNode> list = new ArrayList<>(new LinkedHashSet<>(collection)), toBeRemoved = new ArrayList<>();
         for (int i = 0, n = list.size(); i < n - 1; i += 1) {
             MethodNode one = list.get(i);
-            if (toBeRemoved.contains(one)) continue;
-            for (int j = i + 1; j < list.size(); j++) {
+            for (int j = i + 1; j < n && !toBeRemoved.contains(one); j += 1) {
                 MethodNode two = list.get(j);
                 if (toBeRemoved.contains(two)) continue;
                 if (one.getParameters().length == two.getParameters().length) {
@@ -1256,11 +1254,10 @@ public abstract class StaticTypeCheckingSupport {
                     } else if (!oneDC.equals(twoDC)) {
                         if (ParameterUtils.parametersEqual(one.getParameters(), two.getParameters())) {
                             // GROOVY-6882, GROOVY-6970: drop overridden or interface equivalent method
-                            // GROOVY-8638, GROOVY-10120: unless bridge method (synthetic variant) overrides
                             if (twoDC.isInterface() ? oneDC.implementsInterface(twoDC) : oneDC.isDerivedFrom(twoDC)) {
-                                toBeRemoved.add(isSynthetic(one, two) ? one : two);
+                                toBeRemoved.add(two);
                             } else if (oneDC.isInterface() ? twoDC.isInterface() : twoDC.isDerivedFrom(oneDC)) {
-                                toBeRemoved.add(isSynthetic(two, one) ? two : one);
+                                toBeRemoved.add(one);
                             }
                         }
                     }
diff --git a/src/test/groovy/transform/stc/BugsSTCTest.groovy b/src/test/groovy/transform/stc/BugsSTCTest.groovy
index 9ec2413d9b..d44e696aa9 100644
--- a/src/test/groovy/transform/stc/BugsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/BugsSTCTest.groovy
@@ -1038,6 +1038,33 @@ Printer
         '''
     }
 
+    // GROOVY-10109
+    void testInvokePublicMethodFromInaccessibleBase() {
+        assertScript '''
+            new StringBuilder().with {
+                assert length() == 0 // access error
+            }
+        '''
+    }
+
+    void testInvokeSuperMethodFromCovariantOverride() {
+        assertScript '''
+            abstract class A { int i = 0
+                def m(CharSequence cs) {
+                    i += 1
+                }
+            }
+            class C extends A {
+                def m(String s) {
+                    super.m(s)
+                    i += 1
+                }
+            }
+            def n = new C().m("")
+            assert n == 2
+        '''
+    }
+
     // GROOVY-10424
     void testPrivateInnerClassOptimizedBooleanExpr1() {
         assertScript '''
diff --git a/src/test/groovy/transform/stc/GenericsSTCTest.groovy b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
index c9fe57dd82..699083e2a2 100644
--- a/src/test/groovy/transform/stc/GenericsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/GenericsSTCTest.groovy
@@ -323,16 +323,12 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
         '''
     }
 
-    // GROOVY-8638, GROOVY-10120
     void testReturnTypeInferenceWithMethodGenerics18() {
         if (!GroovyAssert.isAtLeastJdk('1.8')) return
-
-        String guava = '''
+        assertScript '''
             @Grab('com.google.guava:guava:31.1-jre')
             import com.google.common.collect.*
-        '''
 
-        assertScript guava + '''
             ListMultimap<String, Integer> mmap = ArrayListMultimap.create()
 
             Map<String, Collection<Integer>> map = mmap.asMap()
@@ -342,8 +338,28 @@ class GenericsSTCTest extends StaticTypeCheckingTestCase {
                 Collection<Integer> values = entry.value
             }
         '''
+    }
+
+    // GROOVY-8638
+    void testReturnTypeInferenceWithMethodGenerics18a() {
+        if (!GroovyAssert.isAtLeastJdk('1.8')) return
+        shouldFailWithMessages '''
+            @Grab('com.google.guava:guava:31.1-jre')
+            import com.google.common.collect.*
+
+            def mmap = (ListMultimap<String, Integer>) ArrayListMultimap.create()
+            Map<String, Set<Integer>> map = mmap.asMap()
+        ''',
+        'Cannot assign java.util.Map <String, java.util.Collection> to: java.util.Map <String, Set>'
+    }
+
+    @NotYetImplemented // GROOVY-10120
+    void testReturnTypeInferenceWithMethodGenerics18b() {
+        if (!GroovyAssert.isAtLeastJdk('1.8')) return
+        shouldFailWithMessages '''
+            @Grab('com.google.guava:guava:31.1-jre')
+            import com.google.common.collect.*
 
-        shouldFailWithMessages guava + '''
             ListMultimap<String, Integer> mmap = ArrayListMultimap.create()
             Map<String, Set<Integer>> map = mmap.asMap() // ArrayListMultimap#asMap() is bridge and lacks generics
         ''',