You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by su...@apache.org on 2023/12/20 14:53:49 UTC

(groovy) branch GROOVY_4_0_X updated: GROOVY-10998: Not reporting cyclic dependency in function's type parameters

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

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


The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push:
     new 8693786bcf GROOVY-10998: Not reporting cyclic dependency in function's type parameters
8693786bcf is described below

commit 8693786bcf6f219d3518cdae1a106c738780f02c
Author: Daniel Sun <su...@apache.org>
AuthorDate: Wed Dec 20 22:23:50 2023 +0800

    GROOVY-10998: Not reporting cyclic dependency in function's type parameters
    
    (cherry picked from commit 40acb86b04ec77b9a0319c98fb035ede8e8032da)
---
 .../codehaus/groovy/control/ResolveVisitor.java    | 27 ++++++----
 src/test/groovy/bugs/Groovy10998.groovy            | 59 ++++++++++++++++++++++
 2 files changed, 77 insertions(+), 9 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
index 2cbeeb3731..271c925847 100644
--- a/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
+++ b/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java
@@ -285,6 +285,8 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
             }
         }
 
+        checkGenericsCyclicInheritance(node.getGenericsTypes());
+
         MethodNode oldCurrentMethod = currentMethod;
         currentMethod = node;
         super.visitConstructorOrMethod(node, isConstructor);
@@ -1277,15 +1279,7 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
             for (ClassNode in : node.getInterfaces()) {
                 checkCyclicInheritance(node, in);
             }
-            if (node.getGenericsTypes() != null) {
-                for (GenericsType gt : node.getGenericsTypes()) {
-                    if (gt != null && gt.getUpperBounds() != null) {
-                        for (ClassNode variant : gt.getUpperBounds()) {
-                            if (variant.isGenericsPlaceHolder()) checkCyclicInheritance(variant, gt.getType());
-                        }
-                    }
-                }
-            }
+            checkGenericsCyclicInheritance(node.getGenericsTypes());
           case 2:
             // VariableScopeVisitor visits anon. inner class body inline, so resolve now
             for (Iterator<InnerClassNode> it = node.getInnerClasses(); it.hasNext(); ) {
@@ -1313,6 +1307,21 @@ public class ResolveVisitor extends ClassCodeExpressionTransformer {
         currentClass = oldNode;
     }
 
+    private void checkGenericsCyclicInheritance(GenericsType[] genericsTypes) {
+        if (genericsTypes == null) return;
+
+        for (GenericsType gt : genericsTypes) {
+            if (gt == null) continue;
+            ClassNode[] upperBounds = gt.getUpperBounds();
+            if (upperBounds == null) continue;
+            for (ClassNode variant : upperBounds) {
+                if (variant.isGenericsPlaceHolder()) {
+                    checkCyclicInheritance(variant, gt.getType());
+                }
+            }
+        }
+    }
+
     private void addFatalError(final String text, final ASTNode node) {
         source.addFatalError(text, node);
     }
diff --git a/src/test/groovy/bugs/Groovy10998.groovy b/src/test/groovy/bugs/Groovy10998.groovy
new file mode 100644
index 0000000000..0334441628
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy10998.groovy
@@ -0,0 +1,59 @@
+/*
+ *  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
+import static groovy.test.GroovyAssert.shouldFail
+
+final class Groovy10998 {
+    @Test
+    void testTypeParamCycle1() {
+        def err = shouldFail '''
+            class Test {
+              static <T extends T> void test() {}
+            }
+        '''
+        assert err =~ /Cycle detected: the type T cannot extend.implement itself or one of its own member types/
+    }
+
+    @Test
+    void testTypeParamCycle2() {
+        def err = shouldFail '''
+            class Test {
+              static <T extends U, U extends T> void test() {}
+            }
+        '''
+        assert err =~ /Cycle detected: the type T cannot extend.implement itself or one of its own member types/
+    }
+
+    @Test
+    void testTypeParamNoCycle() {
+        assertScript '''
+            class Test {
+              static <T, U extends T> String test(T t, U u) {
+                "$t,$u"
+              }
+            }
+            assert "1,2" == Test.<Number, Integer>test(1, 2)
+        '''
+    }
+}