[
https://issues.apache.org/jira/browse/GROOVY-12612?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18137437#comment-18137437
]
ASF GitHub Bot commented on GROOVY-12612:
-----------------------------------------
codeconsole commented on PR #3139:
URL: https://github.com/apache/groovy/pull/3139#issuecomment-6610864141
Merge note: #3129 (GROOVY-12602) and #3139 (GROOVY-12612) fix the two halves
of an implicit-this call from a `@CompileStatic` lambda in a trait instance
method:
- `Supplier<String> s = () -> foo()` needs #3129. With #3139 alone it still
throws `ClassCastException: class java.lang.Class cannot be cast to class T`.
- `Supplier<String> s = () -> this.foo()` needs #3139. With #3129 alone it
throws `ClassCastException: class C cannot be cast to class T$Trait$Helper`.
With both applied, every shape I probed passes under dynamic, `@TypeChecked`
and `@CompileStatic`. The shapes were a lambda with arguments, an overridden
trait method, a nested lambda, a closure inside a lambda, `this::foo`,
properties, private fields, static and private methods. 4.0.33, 5.0.8 and 6.0.0
fail the same way, and both JIRA issues now list them. Both PRs append tests at
the end of `TraitWithClosureOrLambda.groovy`, so the second to merge has a
trivial conflict there.
> Explicit this.m() inside a closure or lambda in a trait method is called on
> the trait helper class instead of the implementing instance
> ---------------------------------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12612
> URL: https://issues.apache.org/jira/browse/GROOVY-12612
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 4.0.33, 5.0.8, 6.0.0
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> {code:groovy}
> @groovy.transform.CompileStatic
> trait T {
> String getName() { 'T' }
> String m() { Closure<String> c = { -> this.getName() }; c() }
> }
> class C implements T {
> String getName() { 'C' }
> }
> assert new C().m() == 'C'
> {code}
> Actual: under static compilation, {{ClassCastException: class java.lang.Class
> cannot be cast to class T$Trait$Helper}}. A native lambda, a lambda typed
> Closure, or a lambda inside a closure fails the same way. Dynamic Groovy and
> @TypeChecked return 'T' instead of 'C': the call goes to the helper's static
> method with a null self, so the implementing class's override is skipped.
> With arguments it throws MissingMethodException; for a super-trait method it
> throws NoSuchElementException. In a static trait method,
> {{this.getSimpleName()}} inside a closure returns 'Helper' instead of 'C'.
> Expected: 'C'. An explicit {{this.m()}} in a closure targets the implementing
> instance, as a bare {{this}} in a trait closure already does, and as
> {{this.m()}} in a closure of a plain class does (never the delegate).
> Cause: TraitReceiverTransformer keeps the original {{this}} receiver for
> every method call on this inside a closure. That is right for implicit-this
> calls, which go through the closure's resolve strategy. For an explicit
> this.m() the receiver ends up as the helper class, because the trait method
> body lives in a static helper method.
> Seen in 4.0.33, 5.0.8 and 6.0.0.
> PR: https://github.com/apache/groovy/pull/3139
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)