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

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

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


##########
src/main/java/org/codehaus/groovy/classgen/InnerClassCompletionVisitor.java:
##########
@@ -149,6 +151,17 @@ public void visitConstructor(final ConstructorNode node) {
         }
     }
 
+    /**
+     * Tests for one of the classes generated for a trait, i.e. the helper, the
+     * field helper or the static field helper. Classes declared within a trait
+     * are not included.
+     */
+    private static boolean isTraitHelper(final ClassNode node) {
+        ClassNode outerClass = node.getOuterClass();
+        return isTrait(outerClass) && (node.getModifiers() & ACC_SYNTHETIC) != 0
+                && node.getName().startsWith(outerClass.getName() + "$Trait$");
+    }

Review Comment:
   `isTraitHelper` currently identifies helper classes using 
`startsWith(outerClass.getName() + "$Trait$")`. This can incorrectly classify 
user-declared inner classes whose *simple* name begins with `Trait$...` (e.g. 
`static class Trait$Foo {}` inside a trait), causing this visitor to skip them 
and potentially reintroduce runtime issues. Prefer matching the exact helper 
class names/suffixes (Helper/FieldHelper/StaticFieldHelper) to avoid false 
positives.





> Double-nested non-static class in trait calling outer method compiles, but 
> fails at runtime
> -------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12226
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12226
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 5.0.8
>            Reporter: Octavia Togami
>            Priority: Major
>
> The following code works in 4.0.33, but fails in 5.0.8:
> {code:groovy}
> trait T {
>     static class Outer {
>         def outerMethod() { 'p' }
>         class Inner { def callOuter() { outerMethod() } }
>         def viaInner() { new Inner().callOuter() }
>     }
> }
> assert new T.Outer().viaInner() == 'p'
> {code}
> The failure is:
> {code:sh}
> java.lang.NoSuchMethodError: 'java.lang.Object 
> T$Outer.this$dist$invoke$1(java.lang.String, java.lang.Object)'
>     at T$Outer$Inner.methodMissing(v6_named.groovy)
>     at T$Outer$Inner.callOuter(v6_named.groovy:4)
> {code}
> This is because the compiler tries to generate a call to 
> {{T$Outer.this$dist$invoke$1}} in {{callOuter}}, but does not generate that 
> method in {{T$Outer}}, unlike Groovy 4. It should either produce a proper 
> compiler error, or generate the bridge method.
> This was reduced from real code in Gradle: 
> https://github.com/gradle/gradle/blob/2e04b0a02ba788075b2be3614f9fc0562ecfaf9d/platforms/native/language-native/src/testFixtures/groovy/org/gradle/language/LanguageTaskNames.groovy#L121



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

Reply via email to