[
https://issues.apache.org/jira/browse/GROOVY-12602?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18137436#comment-18137436
]
ASF GitHub Bot commented on GROOVY-12602:
-----------------------------------------
codeconsole commented on PR #3129:
URL: https://github.com/apache/groovy/pull/3129#issuecomment-6610863489
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.
> SC: a lambda in a trait method calling a trait instance method by implicit
> this throws ClassCastException: Class cannot be cast to the trait
> --------------------------------------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12602
> URL: https://issues.apache.org/jira/browse/GROOVY-12602
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 4.0.33, 5.0.8, 6.0.0
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> {code:groovy}
> import java.util.function.Supplier
> @groovy.transform.CompileStatic
> trait U {
> String getUname() { 'u' }
> String a() { Supplier<String> s = () -> getUname(); s.get() }
> }
> class C implements U {}
> assert new C().a() == 'u'
> {code}
> Actual:
> {noformat}
> java.lang.ClassCastException: class java.lang.Class cannot be cast to class U
> at U$Trait$Helper$_a_lambda1.doCall(...)
> {noformat}
> The same happens for a super trait's accessor read as {{uname}} or called as
> {{getUname()}} in a lambda, for a lambda in a lambda, for a closure in a
> lambda and for a lambda in a trait field initializer.
> Expected: the assertion passes, as it does without {{@CompileStatic}}, with
> {{@TypeChecked}}, and with a closure instead of the lambda.
> A closure in a trait method is rehydrated with the trait instance as its this
> object. A statically compiled lambda is not, and the lambda writer passes the
> trait helper class as the lambda's this object because the helper method is
> static. The implicit-this call then casts that class to the trait.
> Seen in 4.0.33, 5.0.8 and 6.0.0.
> PR: https://github.com/apache/groovy/pull/3129
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)