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 2023/04/30 16:29:11 UTC

[groovy] branch GROOVY_3_0_X updated: GROOVY-11012: STC: propagate interface generics

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

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


The following commit(s) were added to refs/heads/GROOVY_3_0_X by this push:
     new 2374d697fd GROOVY-11012: STC: propagate interface generics
2374d697fd is described below

commit 2374d697fdb309d7ed0b43ae3be53802a45ca4bd
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sun Apr 30 10:44:22 2023 -0500

    GROOVY-11012: STC: propagate interface generics
---
 .../codehaus/groovy/transform/trait/Traits.java    |   4 +-
 .../traitx/TraitASTTransformationTest.groovy       | 280 ++++++++++-----------
 2 files changed, 132 insertions(+), 152 deletions(-)

diff --git a/src/main/java/org/codehaus/groovy/transform/trait/Traits.java b/src/main/java/org/codehaus/groovy/transform/trait/Traits.java
index ecdd903262..8e0d88e151 100644
--- a/src/main/java/org/codehaus/groovy/transform/trait/Traits.java
+++ b/src/main/java/org/codehaus/groovy/transform/trait/Traits.java
@@ -288,7 +288,9 @@ public abstract class Traits {
         if (cNode.isInterface()) interfaces.add(cNode);
         ClassNode[] directInterfaces = cNode.getInterfaces();
         for (int i = directInterfaces.length - 1; i >= 0; i -= 1) {
-            ClassNode iNode = GenericsUtils.parameterizeType(cNode, directInterfaces[i]);
+            ClassNode iNode = directInterfaces[i];
+            if (cNode.isRedirectNode()) // GROOVY-11012
+                iNode = GenericsUtils.parameterizeType(cNode, iNode);
             if (interfaces.add(iNode)) collectAllInterfacesReverseOrder(iNode, interfaces);
         }
         return interfaces;
diff --git a/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy b/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
index 41f8282b98..613f68cb0b 100644
--- a/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
+++ b/src/test/org/codehaus/groovy/transform/traitx/TraitASTTransformationTest.groovy
@@ -503,177 +503,155 @@ final class TraitASTTransformationTest {
 
     @Test
     void testTraitWithGenerics1() {
-        assertScript '''
-            trait Provider<T> {
-                T get() {
-                    null
-                }
-            }
-            class StringProvider implements Provider<String> {}
-            def c = new StringProvider()
-            assert c.get() == null
-        '''
-
-        assertScript '''
-            @groovy.transform.CompileStatic
-            trait Provider<T> {
-                T get() {
-                    null
-                }
-            }
-            @groovy.transform.CompileStatic
-            class StringProvider implements Provider<String> {}
-            def c = new StringProvider()
-            assert c.get() == null
-        '''
+        for (mode in ['','@groovy.transform.TypeChecked','@groovy.transform.CompileStatic']) {
+            assertScript """
+                $mode
+                trait Provider<T> {
+                    T get() { null }
+                }
+                $mode
+                class StringProvider implements Provider<String> {
+                }
+                $mode
+                void test() {
+                    def c = new StringProvider()
+                    assert c.get() == null
+                }
+                test()
+            """
+        }
     }
 
     @Test
     void testTraitWithGenerics2() {
-        assertScript '''
-            trait Provider<T> {
-                T get(T ref) {
-                    ref
-                }
-            }
-            class StringProvider implements Provider<String> {}
-            def c = new StringProvider()
-            assert c.get('foo') == 'foo'
-        '''
-
-        assertScript '''
-            @groovy.transform.CompileStatic
-            trait Provider<T> {
-                T get(T ref) {
-                    ref
-                }
-            }
-            @groovy.transform.CompileStatic
-            class StringProvider implements Provider<String> {}
-            def c = new StringProvider()
-            assert c.get('foo') == 'foo'
-        '''
+        for (mode in ['','@groovy.transform.TypeChecked','@groovy.transform.CompileStatic']) {
+            assertScript """
+                $mode
+                trait Provider<T> {
+                    T get(T t) { t }
+                }
+                $mode
+                class StringProvider implements Provider<String> {
+                }
+                $mode
+                void test() {
+                    def p = new StringProvider()
+                    assert p.get('foo') == 'foo'
+                }
+                test()
+            """
+        }
     }
 
     @Test // GROOVY-9760
     void testTraitWithGenerics3() {
-        assertScript '''
-            trait Provider<T> {
-                T get(T ref) {
-                    ref
+        for (mode in ['','@groovy.transform.TypeChecked','@groovy.transform.CompileStatic']) {
+            assertScript """
+                $mode
+                trait Provider<T> {
+                    T get(T t) { t }
                 }
-            }
-            class UnspecifiedProvider implements Provider {
-            }
-            assert new UnspecifiedProvider().get('foo') == 'foo'
-        '''
+                $mode
+                class UnspecifiedProvider implements Provider {
+                }
+                $mode
+                void test() {
+                    assert new UnspecifiedProvider().get('foo') == 'foo'
+                }
+                test()
+            """
+        }
+    }
 
-        assertScript '''
-            @groovy.transform.CompileStatic
-            trait Provider<T> {
-                T get(T ref) {
-                    ref
+    @Test // GROOVY-11012
+    void testTraitWithGenerics4() {
+        for (mode in ['','@groovy.transform.TypeChecked','@groovy.transform.CompileStatic']) {
+            assertScript """
+                $mode
+                trait Provider<T> {
+                    T get(T t) { t }
                 }
-            }
-            @groovy.transform.CompileStatic
-            class UnspecifiedProvider implements Provider {
-            }
-            assert new UnspecifiedProvider().get('foo') == 'foo'
-        '''
+                $mode
+                class Supplier<U> implements Provider<U> {
+                }
+                $mode
+                void test() {
+                    def s = new Supplier<Number>()
+                    Number n = s.get(42)
+                    assert n == 42
+                }
+                test()
+            """
+        }
     }
 
     @Test
-    void testTraitWithGenericProperty() {
-        assertScript '''
-            trait PropertyProvider<T> {
-                T foo
-            }
-            class StringProperty implements PropertyProvider<String> {}
-            def c = new StringProperty()
-            c.foo = 'foo'
-            assert c.foo == 'foo'
-        '''
-
-        assertScript '''
-            @groovy.transform.CompileStatic
-            trait PropertyProvider<T> {
-                T foo
-            }
-            @groovy.transform.CompileStatic
-            class StringProperty implements PropertyProvider<String> {}
-            def c = new StringProperty()
-            c.foo = 'foo'
-            assert c.foo == 'foo'
-        '''
+    void testTraitWithGenericProperty1() {
+        for (mode in ['','@groovy.transform.TypeChecked','@groovy.transform.CompileStatic']) {
+            assertScript """
+                $mode
+                trait PropertyProvider<T> {
+                    T foo
+                }
+                $mode
+                class StringProperty implements PropertyProvider<String> {
+                }
+                $mode
+                void test() {
+                    def c = new StringProperty()
+                    c.foo = 'foo'
+                    assert c.foo == 'foo'
+                }
+                test()
+            """
+        }
     }
 
     @Test
-    void testTraitWithComplexGenericProperty() {
-        assertScript '''
-            trait PropertyProvider<T> {
-                List<T> foo
-            }
-            class StringProperty implements PropertyProvider<String> {}
-            def c = new StringProperty()
-            c.foo = ['foo']
-            assert c.foo == ['foo']
-        '''
-
-        assertScript '''
-            @groovy.transform.CompileStatic
-            trait PropertyProvider<T> {
-                List<T> foo
-            }
-            @groovy.transform.CompileStatic
-            class StringProperty implements PropertyProvider<String> {}
-            def c = new StringProperty()
-            c.foo = ['foo']
-            assert c.foo == ['foo']
-        '''
-
-        assertScript '''
-            trait PropertyProvider<T> {
-                List<T> foo
-            }
-            class StringProperty implements PropertyProvider<String> {}
-
-            @groovy.transform.CompileStatic
-            void test() {
-                def c = new StringProperty()
-                c.foo = ['foo']
-                assert c.foo == ['foo']
-            }
-            test()
-        '''
+    void testTraitWithGenericProperty2() {
+        for (mode in ['','@groovy.transform.TypeChecked','@groovy.transform.CompileStatic']) {
+            assertScript """
+                $mode
+                trait PropertyProvider<T> {
+                    List<T> foo
+                }
+                $mode
+                class StringProperty implements PropertyProvider<String> {
+                }
+                $mode
+                void test() {
+                    def c = new StringProperty()
+                    c.foo = ['foo']
+                    assert c.foo == ['foo']
+                }
+                test()
+            """
+        }
     }
 
     @Test
-    void testTraitWithGenericField() {
-        assertScript '''
-            trait PropertyProvider<T> {
-                private T foo
-                void set(T t) { foo = t}
-                T get() { foo }
-            }
-            class StringProperty implements PropertyProvider<String> {}
-            def c = new StringProperty()
-            c.set('foo')
-            assert c.get() == 'foo'
-        '''
-
-        assertScript '''
-            @groovy.transform.CompileStatic
-            trait PropertyProvider<T> {
-                private T foo
-                void set(T t) { foo = t}
-                T get() { foo }
-            }
-            @groovy.transform.CompileStatic
-            class StringProperty implements PropertyProvider<String> {}
-            def c = new StringProperty()
-            c.set('foo')
-            assert c.get() == 'foo'
-        '''
+    void testTraitWithGenericProperty3() {
+        for (mode in ['','@groovy.transform.TypeChecked','@groovy.transform.CompileStatic']) {
+            assertScript """
+                $mode
+                trait PropertyProvider<T> {
+                    private T foo
+                    T getFoo() { foo }
+                    void setFoo(T t) { foo = t }
+                }
+                $mode
+                class StringProperty implements PropertyProvider<String> {
+                }
+                $mode
+                void test() {
+                    def c = new StringProperty(foo:'bar')
+                    assert c.getFoo() == 'bar'
+                    c.setFoo('foo')
+                    assert c.getFoo() == 'foo'
+                }
+                test()
+            """
+        }
     }
 
     @Test