[
https://issues.apache.org/jira/browse/GROOVY-9516?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17124200#comment-17124200
]
Eric Milles commented on GROOVY-9516:
-------------------------------------
The code in StaticTypeCheckingVisitor.visitClosureExpression is saving the
metadata of all closure shared variables before visitation and restoring it to
the saved state afterwards. This is what is preventing the assignment type
within the closure expression from living on past the closure. This could
easily be changed if desired.
Here are the test cases that were added to check for this behavior:
{code:groovy}
void testClosureShouldNotChangeInferredType() {
assertScript '''
def x = '123';
{ -> x = new StringBuffer() }
x.charAt(0)
'''
}
void testClosureSharedVariableWithIncompatibleType() {
shouldFailWithMessages '''
def x = '123';
{ -> x = 1 }
x.charAt(0)
''', 'A closure shared variable [x] has been assigned with various
types and the method [charAt(int)] does not exist in the lowest upper bound'
}
{code}
> 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)