[
https://issues.apache.org/jira/browse/GROOVY-12790?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18147985#comment-18147985
]
ASF GitHub Bot commented on GROOVY-12790:
-----------------------------------------
paulk-asert opened a new pull request, #3315:
URL: https://github.com/apache/groovy/pull/3315
https://issues.apache.org/jira/browse/GROOVY-12790
When a script has an instance `main` or a `run()` method and no statements
other than declarations, `ModuleNode.createStatementsClass()` turns its
declarations into fields. The multiple-assignment handling had two problems:
- A right-hand side that was not a list literal hit a bare `break`, so that
declaration and every later one were silently dropped:
```groovy
def (a, b) = "xy".toList()
def c = 3
void main() { println "a=$a c=$c" } // MissingPropertyException: No such
property: a
```
- With a list literal, targets were paired with elements one by one, which
is wrong for the newer forms: `def (h, *t) = [1, 2, 3]` gave `t == 2`.
Each target is still a field, but the multiple assignment is now kept as a
`(a, b) = rhs` assignment in the class's instance initializer, with the
targets' rest-binding and map-key metadata. So a rest binding, map style, typed
or `val` targets and any right-hand side behave as they do elsewhere. Field
initializers run before the instance initializer, so declarations after the
first multiple assignment are assigned there too, to keep their order.
Declarations before it, and scripts without one, compile as before.
`Groovy12790` covers the instance-`main` and `run()` paths, a later
declaration that uses the tuple's values, rest binding, map style, typed and
final targets, and a `@CompileStatic` `main`. All 8 tests fail on master and
pass with the fix. `bugs.*`, `gls.*`, the parser tests,
`ScriptsAndClassesSpecTest` (4143 tests) and `:checkstyleGate :codenarcGate
:rat` pass locally on JDK 17.
The same loop is on GROOVY_5_0_X, GROOVY_5_1_X and GROOVY_6_0_X, from
GROOVY-11118. #3314 (`@NoScript`) touches the same method and will be rebased
on this.
> A multiple-assignment declaration with a non-literal right-hand side is
> dropped, with every later declaration, from a script with an instance main or
> a run method
> ------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12790
> URL: https://issues.apache.org/jira/browse/GROOVY-12790
> Project: Groovy
> Issue Type: Bug
> Components: Compiler
> Reporter: Paul King
> Priority: Major
>
> In a script with no statements other than declarations, plus a {{main}}
> method (the JEP 445 style class) or a no-arg {{run}} method, top-level
> declarations are turned into fields. A multiple-assignment declaration whose
> right-hand side is not a list literal is silently dropped instead, together
> with every declaration that follows it.
> {code:groovy}
> def (a, b) = "xy".toList()
> def c = 3
> void main() { println "a=$a c=$c" }
> {code}
> fails with:
> {noformat}
> groovy.lang.MissingPropertyException: No such property: a for class: M
> {noformat}
> The generated class's declared fields:
> ||Script||Fields||
> |{{def (a, b) = [1, 2]; def c = 3; void main() {}}}|a, b, c|
> |{{def (a, b) = "xy".toList(); def c = 3; void main() {}}}|(none)|
> |{{def (a, b) = "xy".toList(); def c = 3; def run() {}}}|(none)|
> Neither {{a}}, {{b}} nor {{c}} becomes a field, and the declaration's
> right-hand side is never evaluated. There is no compile error or warning.
> The cause is the declaration loop in {{ModuleNode.createStatementsClass()}}:
> {code:java}
> if (!(de.getRightExpression() instanceof ListExpression)) break;
> {code}
> The {{break}} leaves the loop without setting {{hasUncontainedStatements}},
> so the class is still built from the fields collected so far, and the
> remaining declarations are discarded. The loop was added by GROOVY-11118
> (5.0.0-alpha-1).
> Possible fixes: treat such a declaration as an uncontained statement, which
> gives the normal script behaviour; or create the fields and assign them in an
> instance initializer, which keeps the plain class.
> Reproduced on master at 077ff21217 and on 6.0.0-beta-2, both on JDK 17.0.20.
> The same code is on GROOVY_5_0_X, GROOVY_5_1_X and GROOVY_6_0_X, but not on
> GROOVY_4_0_X.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)