[ 
https://issues.apache.org/jira/browse/GROOVY-12112?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18092088#comment-18092088
 ] 

ASF GitHub Bot commented on GROOVY-12112:
-----------------------------------------

Copilot commented on code in PR #2639:
URL: https://github.com/apache/groovy/pull/2639#discussion_r3487254045


##########
src/main/java/org/codehaus/groovy/transform/stc/TraitTypeCheckingExtension.java:
##########
@@ -84,6 +86,31 @@ public List<MethodNode> handleMissingMethod(final ClassNode 
receiver, final Stri
             ClassNode returnType = 
mce.getNodeMetaData(TraitASTTransformation.DO_DYNAMIC);
             if (returnType != null) return 
Collections.singletonList(makeDynamic(call, returnType));
 
+            // GROOVY-12112: a qualified `Trait.m(...)` call to a @Virtual 
trait static cannot
+            // resolve — @Virtual statics are intentionally not promoted onto 
the trait interface
+            // (they exist to be overridden per implementing class, so a bare 
Trait.m(...) has no
+            // implementing class to dispatch through). Replace the generic 
"cannot find method"
+            // with a clear, actionable error pointing at the supported forms.
+            if (isClassClassNodeWrappingConcreteType(receiver)) {
+                ClassNode traitType = receiver.getGenericsTypes()[0].getType();
+                if (Traits.isTrait(traitType)) {
+                    for (ClassNode trait : Traits.findTraits(traitType)) {
+                        ClassNode helper = Traits.findHelper(trait);
+                        if (helper == null) continue;
+                        for (MethodNode m : helper.getDeclaredMethods(name)) {
+                            if (m.isStatic() && 
!m.getAnnotations(VIRTUAL_TYPE).isEmpty()) {
+                                String tn = traitType.getNameWithoutPackage();
+                                addStaticTypeError("Cannot call '" + tn + "." 
+ name + "(...)': '" + name
+                                        + "' is a @Virtual trait static 
method, which is overridable per implementing class and is not callable through 
the trait itself. "
+                                        + "Invoke it on an implementing class 
(e.g. 'SomeImpl." + name + "(...)'), or use '" + tn + ".super." + name
+                                        + "(...)' from within trait code for 
the trait's own definition", mce);

Review Comment:
   The new error message suggests using `Trait.super.<method>(...)` based on 
the *receiver* trait type (`traitType`). If the `@Virtual` static is inherited 
(e.g. receiver is `Q` but the `@Virtual` method is declared on super-trait 
`P`), the correct explicit default form is `P.super.<method>(...)` (the 
declaring trait), not `Q.super.<method>(...)`.





> Reject qualified Trait.m() to a @Virtual trait static with a clear compile 
> error
> --------------------------------------------------------------------------------
>
>                 Key: GROOVY-12112
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12112
>             Project: Groovy
>          Issue Type: Improvement
>            Reporter: Paul King
>            Assignee: Paul King
>            Priority: Major
>




--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to