Skip to content

GROOVY-11907: trait field reference transform restructure#2451

Open
eric-milles wants to merge 1 commit intomasterfrom
GROOVY-11907
Open

GROOVY-11907: trait field reference transform restructure#2451
eric-milles wants to merge 1 commit intomasterfrom
GROOVY-11907

Conversation

@eric-milles
Copy link
Copy Markdown
Member

@eric-milles eric-milles commented Apr 8, 2026

Restructure the trait receiver transformer to funnel all the field reference transformation through a single method. I replaced the instanceof checking with a simpler set of choices.

  1. When the self parameter is "$static$self" use directly
  2. When the field is static use "$self.getClass()"
  3. Otherwise, cast "$self" to the field helper type
    private Expression transformFieldReference(final Expression exp, final FieldNode fn, final Expression value) {
        Expression receiver;
        if (ClassHelper.isClassType(weaved.getOriginType())) {
            receiver = varX(weaved); // $static$self
        } else if (fn.isStatic()) {
            var call = callX(varX(weaved), "getClass"); // $self.getClass()
            call.setImplicitThis(false);
            call.setMethodTarget(ClassHelper.OBJECT_TYPE.getGetterMethod("getClass", false));
            receiver = call;
        } else {
            receiver = castX(fieldHelper, varX(weaved)); // (<trait class>$Trait$FieldHelper) $self
        }

        MethodCallExpression mce;
        if (value == null) {
            mce = callX(receiver, Traits.helperGetterName(fn));
        } else {
            mce = callX(receiver, Traits.helperSetterName(fn), transform(value));
        }
        mce.setImplicitThis(false);
        mce.setSourcePosition(exp);

        // GROOVY-7255: static fields are available via the implementing class, which is not checkable
        if (fn.isStatic()) mce.putNodeMetaData(TraitASTTransformation.DO_DYNAMIC, fn.getOriginType());

        return mce;
    }

see #2443

This was the grails error before the change:

java.lang.AssertionError
        at groovyjarjarasm.asm.Frame.putAbstractType(Frame.java:1482)
        at groovyjarjarasm.asm.MethodWriter.putAbstractTypes(MethodWriter.java:1961)
        at groovyjarjarasm.asm.MethodWriter.putFrame(MethodWriter.java:1920)
        at groovyjarjarasm.asm.MethodWriter.visitFrameEnd(MethodWriter.java:1852)
        at groovyjarjarasm.asm.Frame.accept(Frame.java:1405)
        at groovyjarjarasm.asm.MethodWriter.computeAllFrames(MethodWriter.java:1630)
        at groovyjarjarasm.asm.MethodWriter.visitMaxs(MethodWriter.java:1548)
        at org.codehaus.groovy.classgen.AsmClassGenerator.visitConstructorOrMethod(AsmClassGenerator.java:626)
        at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:110)
        at org.codehaus.groovy.classgen.AsmClassGenerator.visitMethod(AsmClassGenerator.java:771)
        at org.codehaus.groovy.ast.ClassNode.visitMethods(ClassNode.java:1335)
        at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1328)
        at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:52)
        at org.codehaus.groovy.classgen.AsmClassGenerator.visitClass(AsmClassGenerator.java:379)
        at org.codehaus.groovy.control.CompilationUnit$3.call(CompilationUnit.java:780)
        at org.codehaus.groovy.control.CompilationUnit$IPrimaryClassNodeOperation.doPhaseOperation(CompilationUnit.java:922)
        at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:668)
        at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:642)
        at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:623)
        at org.gradle.api.internal.tasks.compile.ApiGroovyCompiler.execute(ApiGroovyCompiler.java:285)

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 8, 2026

Codecov Report

❌ Patch coverage is 92.59259% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.4934%. Comparing base (bc8bc7a) to head (19f3899).

Files with missing lines Patch % Lines
...oovy/transform/trait/TraitReceiverTransformer.java 92.5926% 0 Missing and 2 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@                Coverage Diff                 @@
##               master      #2451        +/-   ##
==================================================
- Coverage     66.4994%   66.4934%   -0.0060%     
+ Complexity      30215      30211         -4     
==================================================
  Files            1408       1408                
  Lines          117744     117732        -12     
  Branches        20907      20906         -1     
==================================================
- Hits            78299      78284        -15     
- Misses          33020      33022         +2     
- Partials         6425       6426         +1     
Files with missing lines Coverage Δ
...oovy/transform/trait/TraitReceiverTransformer.java 88.6228% <92.5926%> (-0.7627%) ⬇️

... and 3 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.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants