[
https://issues.apache.org/jira/browse/GROOVY-9516?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17089695#comment-17089695
]
Eric Milles commented on GROOVY-9516:
-------------------------------------
The type checker does not distinguish between deferred closure and
immediately-evaluated closure. In your case, it looks pretty simple, but you
could also assign the closure literal to a variable and make the call a few
statement later within the method. A complete solution would need to handle
both cases.
The closure body is assumed to be run later, so the straight line path (method
body statements) are examined for assignments. Even if "typeof(z)" was C
during STC, you would still get an error for trying to call the method from B.
If you want to create closures and evaluate them in this way, you can use
{{@CompileDynamic}} so that you do not run into compiler-supplied casts and
errors.
> Static compiler infers wrong type for implicitly shared variables
> -----------------------------------------------------------------
>
> Key: GROOVY-9516
> URL: https://issues.apache.org/jira/browse/GROOVY-9516
> Project: Groovy
> Issue Type: Bug
> Components: Static compilation
> Affects Versions: 3.0.0, 2.5.11
> Reporter: Konstantin Nisht
> Priority: Major
>
> Consider the following code
> {code:groovy}
> class A {}
> class B extends A{ def b() {}}
> class C extends A{}
> @CompileStatic
> static foo() {
> def x = new B()
> ({ x = new C() })()
> def z = x
> z.b()
> }
> {code}
> Here type of {{z}} in the last line of the method is deduced to be {{B}}.
> Attempt to execute {{z.b()}} leads to cast exception, which is unexpected
> under static compilation.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)