You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2026/06/29 20:25:48 UTC

(groovy) branch master updated: GROOVY-12117: Resolution of inherited static trait method from sub-trait body is transform-order dependent

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

paulk-asert 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 0d90a6e517 GROOVY-12117: Resolution of inherited static trait method from sub-trait body is transform-order dependent
0d90a6e517 is described below

commit 0d90a6e517567373e3259c6a6ad5fd820efae19c
Author: Paul King <pa...@asert.com.au>
AuthorDate: Tue Jun 30 05:50:59 2026 +1000

    GROOVY-12117: Resolution of inherited static trait method from sub-trait body is transform-order dependent
---
 .../transform/trait/TraitReceiverTransformer.java  | 13 ++++++++
 .../groovy/transform/traitx/Groovy12106.groovy     | 35 ++++++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git a/src/main/java/org/codehaus/groovy/transform/trait/TraitReceiverTransformer.java b/src/main/java/org/codehaus/groovy/transform/trait/TraitReceiverTransformer.java
index b338ff631f..17ded67ee0 100644
--- a/src/main/java/org/codehaus/groovy/transform/trait/TraitReceiverTransformer.java
+++ b/src/main/java/org/codehaus/groovy/transform/trait/TraitReceiverTransformer.java
@@ -414,6 +414,19 @@ class TraitReceiverTransformer extends ClassCodeExpressionTransformer {
                     return methodNode;
                 }
             }
+            // GROOVY-12117: when a co-compiled super trait has not been transformed
+            // yet, its helper is still an empty GROOVY-7909 stub, so the lowered
+            // static above is not found. The original static is still declared on
+            // the trait node at this point, so resolve it there. This keeps the
+            // rewrite independent of the order in which sibling traits are
+            // transformed (GEP-22 P1' dispatch consistency); the helper resolves
+            // identically once every trait is lowered, so this only matters for
+            // the not-yet-lowered super trait.
+            for (MethodNode methodNode : superTrait.getDeclaredMethods(methodName)) {
+                if (methodNode.isPublic() && methodNode.isStatic()) {
+                    return methodNode;
+                }
+            }
         }
 
         return null;
diff --git a/src/test/groovy/org/codehaus/groovy/transform/traitx/Groovy12106.groovy b/src/test/groovy/org/codehaus/groovy/transform/traitx/Groovy12106.groovy
index fd18ea3954..9fb163c099 100644
--- a/src/test/groovy/org/codehaus/groovy/transform/traitx/Groovy12106.groovy
+++ b/src/test/groovy/org/codehaus/groovy/transform/traitx/Groovy12106.groovy
@@ -170,4 +170,39 @@ final class Groovy12106 {
             assert new C().describe(new Field()) == 'seen'
         '''
     }
+
+    /**
+     * The Grails helper shape (above) but with the sub-trait declared <em>before</em>
+     * the super-trait in the same compilation unit (GROOVY-12117). When the sub-trait
+     * is transformed first, the super-trait's helper has not been generated yet, so
+     * {@code TraitReceiverTransformer.findConcreteMethod} could not see the inherited
+     * static via the helper and left the call as an unrewritten {@code Arguable#withDelegate},
+     * which then failed type checking — making resolution depend on declaration order.
+     * In a multi-file build (e.g. Grails GraphQL) the files sort sub-trait-first, which
+     * is why this shape escaped {@link #testGrailsHelperShapeWithDelegatesTo}. Resolution
+     * must be order independent (GEP-22 P1').
+     */
+    @Test
+    void testGrailsHelperShapeWithSubTraitDeclaredFirst() {
+        assertScript '''
+            import groovy.transform.CompileStatic
+            final class Field { String name = 'f' }
+            @CompileStatic
+            trait Arguable<T> extends ExecutesClosures {   // sub-trait declared FIRST
+                String describe(Field f) {
+                    def sb = new StringBuilder()
+                    withDelegate({ -> sb.append('seen') }, f)   // f:Field is a subtype of Object
+                    sb.toString()
+                }
+            }
+            @CompileStatic
+            trait ExecutesClosures {
+                static void withDelegate(@DelegatesTo(strategy=Closure.DELEGATE_ONLY, genericTypeIndex=0) Closure callable, Object delegate) {
+                    if (callable != null) { callable.delegate = delegate; callable.resolveStrategy = Closure.DELEGATE_ONLY; callable.call() }
+                }
+            }
+            class C implements Arguable<String> { }
+            assert new C().describe(new Field()) == 'seen'
+        '''
+    }
 }