[ 
https://issues.apache.org/jira/browse/GROOVY-12624?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Scott Murphy Heiberg updated GROOVY-12624:
------------------------------------------
    Description: 
{code:groovy}
@groovy.transform.CompileStatic
trait V {
    static String who() { this.simpleName }
    def m() { new Object().with { who() } }
}
class E implements V {}
assert new E().m() == 'E'

class Dg {
    String who(Class c) { 'delegate' }
}
@groovy.transform.CompileStatic
trait T {
    static String who() { this.simpleName }
    def m() { new Dg().with { who() } }
}
class C implements T {}
assert new C().m() == 'C'
{code}
Actual: under static compilation the first m() returns 'Object' (the delegate's 
class), also when who is declared in a super trait or called as this.who(). The 
second m() returns 'delegate': it calls Dg.who(Class) with the self argument 
that the trait transformation added.

Expected: 'E' and 'C', as dynamic Groovy and @TypeChecked return.

Cause: TraitReceiverTransformer rewrites the call to a trait helper call with 
the receiver class as first argument. Both the outer helper call and the inner 
getClass() call keep the default implicit-this flag, so the static compiler 
resolves them against the closure's delegate.

Seen on master; 5.0.8 and 6.0.0 reported the same.

PR: https://github.com/apache/groovy/pull/3150

  was:
{code:groovy}
@groovy.transform.CompileStatic
trait V {
    static String who() { this.simpleName }
    def m() { new Object().with { who() } }
}
class E implements V {}
assert new E().m() == 'E'

class Dg {
    String who(Class c) { 'delegate' }
}
@groovy.transform.CompileStatic
trait T {
    static String who() { this.simpleName }
    def m() { new Dg().with { who() } }
}
class C implements T {}
assert new C().m() == 'C'
{code}
Actual: under static compilation the first m() returns 'Object' (the delegate's 
class), also when who is declared in a super trait or called as this.who(). The 
second m() returns 'delegate': it calls Dg.who(Class) with the self argument 
that the trait transformation added.

Expected: 'E' and 'C', as dynamic Groovy and @TypeChecked return.

Cause: TraitReceiverTransformer rewrites the call to a trait helper call with 
the receiver class as first argument. Both the outer helper call and the inner 
getClass() call keep the default implicit-this flag, so the static compiler 
resolves them against the closure's delegate.

Seen on master; 5.0.8 and 6.0.0 reported the same.


> SC resolves calls that the trait transformation adds in a closure against the 
> closure's delegate
> ------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12624
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12624
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 6.0.0
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> {code:groovy}
> @groovy.transform.CompileStatic
> trait V {
>     static String who() { this.simpleName }
>     def m() { new Object().with { who() } }
> }
> class E implements V {}
> assert new E().m() == 'E'
> class Dg {
>     String who(Class c) { 'delegate' }
> }
> @groovy.transform.CompileStatic
> trait T {
>     static String who() { this.simpleName }
>     def m() { new Dg().with { who() } }
> }
> class C implements T {}
> assert new C().m() == 'C'
> {code}
> Actual: under static compilation the first m() returns 'Object' (the 
> delegate's class), also when who is declared in a super trait or called as 
> this.who(). The second m() returns 'delegate': it calls Dg.who(Class) with 
> the self argument that the trait transformation added.
> Expected: 'E' and 'C', as dynamic Groovy and @TypeChecked return.
> Cause: TraitReceiverTransformer rewrites the call to a trait helper call with 
> the receiver class as first argument. Both the outer helper call and the 
> inner getClass() call keep the default implicit-this flag, so the static 
> compiler resolves them against the closure's delegate.
> Seen on master; 5.0.8 and 6.0.0 reported the same.
> PR: https://github.com/apache/groovy/pull/3150



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to