[
https://issues.apache.org/jira/browse/GROOVY-12605?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18135605#comment-18135605
]
ASF GitHub Bot commented on GROOVY-12605:
-----------------------------------------
codeconsole opened a new pull request, #3132:
URL: https://github.com/apache/groovy/pull/3132
A static accessor method of a trait could not be referenced as a property
from a static method of the implementing class:
```groovy
trait T { static boolean isVr() { true } }
class C implements T { static m() { vr } }
assert C.m()
```
This failed to compile in every mode, dynamic included, with "Apparent
variable 'vr' was found in a static scope but doesn't refer to a local
variable, static field or class", in 4.0.33, 5.0.8 and 6.0.0 too. The same
method declared in a superclass works, and so does the same reference from an
instance method of `C`. Static getters, static setters used as assignment
targets, and statics inherited through a super trait (`trait U extends T`)
failed the same way.
`VariableScopeVisitor.findClassMember` creates a pseudo-property for
accessor methods of the class and its superclasses. For traits it looked only
at fields and properties, and it did not follow a super trait that is still in
the `extends` clause before `TraitASTTransformation` moves it to the
interfaces. It now also matches a trait's non-private static accessor methods:
the method itself for a trait in the same compilation unit, the public static
forwarder for a precompiled trait. It also queues a trait's super trait.
Private static trait methods and static methods of plain interfaces are still
not resolved, since neither becomes a member of the implementing class. The
pseudo-property construction moved into a shared helper.
## Tests
`TraitASTTransformationTest.testTraitStaticAccessorFromStaticMethod`,
`testSuperTraitStaticAccessorFromStaticMethod` and
`testPrecompiledTraitStaticAccessorFromStaticMethod` fail without the change in
dynamic, `@TypeChecked` and `@CompileStatic` mode.
`testTraitPrivateStaticAccessorFromStaticMethod` checks that a private static
trait accessor stays rejected. `groovy.transform.stc.*`,
`org.codehaus.groovy.classgen.*` (including `asm.sc.*`),
`org.codehaus.groovy.transform.traitx.*`, `gls.*`, `bugs.*`,
`groovy.ThisAndSuperTest` and `groovy.StaticImportTest` pass. The Checkstyle
and CodeNarc gates pass.
> A static accessor method of a trait cannot be referenced as a property from a
> static method of the implementing class
> ---------------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12605
> URL: https://issues.apache.org/jira/browse/GROOVY-12605
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 4.0.33, 5.0.8, 6.0.0
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> {code:groovy}
> trait T { static boolean isVr() { true } }
> class C implements T { static m() { vr } }
> assert C.m()
> {code}
> Actual: compilation fails in dynamic, type-checked and statically compiled
> code alike:
> {noformat}
> Apparent variable 'vr' was found in a static scope but doesn't refer to a
> local variable, static field or class.
> {noformat}
> The same happens for a static getter, for a static setter used as an
> assignment target, and for a static property or accessor inherited through a
> super trait (trait U extends T, class C implements U).
> Expected: the bare name resolves to the static accessor woven into C, as it
> does when the method is declared in a superclass and as it already does from
> instance methods of C.
> Cause: VariableScopeVisitor.findClassMember creates a pseudo-property for
> accessor methods of the class and its superclasses, but for traits it looks
> only at fields and properties, and it does not follow a super trait that is
> still in the extends clause before the trait transformation. StaticVerifier
> then rejects the unresolved name.
> Seen in 4.0.33, 5.0.8 and 6.0.0.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)