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

Scott Murphy Heiberg updated GROOVY-12587:
------------------------------------------
    Description: 
{code:groovy}
@groovy.transform.CompileStatic
trait T {
    static boolean flag = true
    static boolean sm() { this.flag }
    boolean im() { this.flag }
}
class C implements T {}
assert C.sm() && new C().im()
{code}
Actual, under both @CompileStatic and @TypeChecked:
{noformat}
[Static type checking] - Cannot find matching method 
java.lang.Class#T__flag$get() or static method T#T__flag$get(). Please check if 
the declared type is correct and if the method exists.
[Static type checking] - Cannot find matching method 
T$Trait$FieldHelper#T__flag$get(). Please check if the declared type is correct 
and if the method exists.
{noformat}
The first error is for the static method, the second for the instance method. 
The same happens for a static trait property or field of any type, for compound 
assignments such as {{this.count += 1}}, and inside closures and lambdas.

Expected: the script compiles and the assertion passes, as it does without the 
annotation and when the property is referenced without {{this.}}.

Cause: the trait receiver transformer rewrites {{this.flag}} with a placeholder 
non-static field instead of the trait's declared field, so the accessor call 
targets the field helper (instance method) or the Class receiver (static 
method), without the dynamic-dispatch marker that static trait fields get.

Seen in 4.0.33, 5.0.8, 6.0.0 and master.

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

  was:
{code:groovy}
@groovy.transform.CompileStatic
trait T {
    static boolean flag = true
    static boolean sm() { this.flag }
    boolean im() { this.flag }
}
class C implements T {}
assert C.sm() && new C().im()
{code}
Actual, under both @CompileStatic and @TypeChecked:
{noformat}
[Static type checking] - Cannot find matching method 
java.lang.Class#T__flag$get() or static method T#T__flag$get(). Please check if 
the declared type is correct and if the method exists.
[Static type checking] - Cannot find matching method 
T$Trait$FieldHelper#T__flag$get(). Please check if the declared type is correct 
and if the method exists.
{noformat}
The first error is for the static method, the second for the instance method. 
The same happens for a static trait property or field of any type, for compound 
assignments such as {{this.count += 1}}, and inside closures and lambdas.

Expected: the script compiles and the assertion passes, as it does without the 
annotation and when the property is referenced without {{this.}}.

Cause: the trait receiver transformer rewrites {{this.flag}} with a placeholder 
non-static field instead of the trait's declared field, so the accessor call 
targets the field helper (instance method) or the Class receiver (static 
method), without the dynamic-dispatch marker that static trait fields get.

Seen in 4.0.33, 5.0.8, 6.0.0 and master.


> STC rejects reading a static trait property through "this" inside a trait 
> method
> --------------------------------------------------------------------------------
>
>                 Key: GROOVY-12587
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12587
>             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 {
>     static boolean flag = true
>     static boolean sm() { this.flag }
>     boolean im() { this.flag }
> }
> class C implements T {}
> assert C.sm() && new C().im()
> {code}
> Actual, under both @CompileStatic and @TypeChecked:
> {noformat}
> [Static type checking] - Cannot find matching method 
> java.lang.Class#T__flag$get() or static method T#T__flag$get(). Please check 
> if the declared type is correct and if the method exists.
> [Static type checking] - Cannot find matching method 
> T$Trait$FieldHelper#T__flag$get(). Please check if the declared type is 
> correct and if the method exists.
> {noformat}
> The first error is for the static method, the second for the instance method. 
> The same happens for a static trait property or field of any type, for 
> compound assignments such as {{this.count += 1}}, and inside closures and 
> lambdas.
> Expected: the script compiles and the assertion passes, as it does without 
> the annotation and when the property is referenced without {{this.}}.
> Cause: the trait receiver transformer rewrites {{this.flag}} with a 
> placeholder non-static field instead of the trait's declared field, so the 
> accessor call targets the field helper (instance method) or the Class 
> receiver (static method), without the dynamic-dispatch marker that static 
> trait fields get.
> Seen in 4.0.33, 5.0.8, 6.0.0 and master.
> PR: https://github.com/apache/groovy/pull/3112



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

Reply via email to