[ 
https://issues.apache.org/jira/browse/GROOVY-10518?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17501293#comment-17501293
 ] 

Paul King commented on GROOVY-10518:
------------------------------------

Your analysis is correct. Transforms are always run after the intrinsic 
compiler steps. Groovy 4 does have the ability to provide a priority which 
impacts the order in which transforms are run within a phase but it doesn't 
help here as it is expected that ResolveVisitor will have already run, i.e. it 
is regarded as an intrinsic step.

A workaround would be to move your script contents to a method.
{code}
def method() {
  String foo = 'local variable'
  println foo
}

method()
{code}

> @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: 2.4.21, 2.5.15, 3.0.9, 4.0.0
>            Reporter: Frédéric Chuong
>            Priority: Minor
>
> 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)

Reply via email to