G
YouTube TikTok Games
Skip to content

GROOVY-12605: resolve a static accessor method of a trait as a property in a static scope of the implementing class - #3132

Open
codeconsole wants to merge 1 commit into
apache:masterfrom
codeconsole:trait-static-accessor-static-scope
Open

codeconsole wants to merge 1 commit into
apache:masterfrom
codeconsole:trait-static-accessor-static-scope

Conversation

@codeconsole

Copy link
Copy Markdown
Contributor

A static accessor method of a trait could not be referenced as a property from a static method of the implementing class:

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.

…ty in a static scope of the implementing class
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.1021%. Comparing base (cd944a5) to head (3ff2f13).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #3132        +/-   ##
==================================================
+ Coverage     74.1016%   74.1021%   +0.0005%     
- Complexity      43596      43608        +12     
==================================================
  Files            1692       1692                
  Lines          149245     149263        +18     
  Branches        28284      28291         +7     
==================================================
+ Hits           110593     110607        +14     
- Misses          28475      28477         +2     
- Partials        10177      10179         +2     
Files with missing lines Coverage Δ
...codehaus/groovy/classgen/VariableScopeVisitor.java 94.3428% <100.0000%> (+0.0860%) ⬆️

... and 15 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@testlens-app

testlens-app Bot commented Oct 31, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 3ff2f13
▶️ Tests: 101859 executed
⚪️ Checks: 26/26 completed


Learn more about TestLens at testlens.app/docs.

@daniellansun daniellansun left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM for GROOVY-12605.

VariableScopeVisitor.findClassMember already synthesised pseudo-properties for superclass accessors, but for traits it only considered fields/properties and did not walk a still-unmoved extends clause. Matching non-private static trait accessors (same-CU method or precompiled public forwarder) and following unresolved super-traits fixes vr / label / setter assignment from a static method of the implementing class in every mode — including precompiled traits and private-static negative cases.

@CompileModesTest matrix is appropriate. CI green.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants