Frédéric Chuong created GROOVY-10518:
----------------------------------------
Summary: @groovy.transform.Field does not remove the annotated
variable from the local VariableScope
Key: GROOVY-10518
URL: https://issues.apache.org/jira/browse/GROOVY-10518
Project: Groovy
Issue Type: Bug
Affects Versions: 4.0.0, 3.0.9, 2.5.15, 2.4.21
Reporter: Frédéric Chuong
h1. Context
According to the {{\@groovy.transform.Field}}
[documentation|https://groovy-lang.org/metaprogramming.html#xform-Field], it
should "convert" a local variable into a class member variable for the
dynamically generated Script subclass, by changing the scope of the variable.
h1. Problem
The following code does not compile:
{code:groovy|borderColor=red}
@groovy.transform.Field
String foo = 'field'
String foo = 'local variable'
println foo
{code}
with the following error message:
{noformat}
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
MyScript.groovy: 4: The current scope already contains a variable of the name
foo
@ line 4, column 8.
String foo = 'local variable'
^
1 error
{noformat}
h1. Preliminary analysis
A quick look at the implementation seems to reveal that
[{{VariableScopeVisitor}}|https://github.com/apache/groovy/blob/GROOVY_4_0_0/src/main/java/org/codehaus/groovy/classgen/VariableScopeVisitor.java#L150]
(triggered from
[{{ResolveVisitor}}|https://github.com/apache/groovy/blob/GROOVY_4_0_0/src/main/java/org/codehaus/groovy/control/ResolveVisitor.java#L1360])
is run before
[{{FieldASTTransformation}}|https://github.com/apache/groovy/blob/GROOVY_4_0_0/src/main/java/org/codehaus/groovy/transform/FieldASTTransformation.java],
as both are designed to run in the {{SEMANTIC_ANALYSIS}} phase, so I'm not
sure whether it could be easily fixed.
--
This message was sent by Atlassian Jira
(v8.20.1#820001)