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>(...)`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]