codeconsole opened a new pull request, #3219:
URL: https://github.com/apache/groovy/pull/3219

   Under static compilation, annotations placed on a multiple-assignment 
declaration such as `@Override def (a, b) = [1, 2]` were silently dropped, so 
the annotation checks that `ExtendedVerifier` performs during class generation 
(target validation, duplicate detection) never ran for that declaration. The 
same declaration in a dynamic class, or a single declaration `@Override def a = 
1` in a static class, reports "Annotation @java.lang.Override is not allowed on 
element LOCAL_VARIABLE".
   
   The cause is in `BinaryExpressionTransformer#transformMultipleAssignment` 
(instruction selection phase): it rewrites the multiple assignment into one 
`DeclarationExpression` per variable, built with `new 
DeclarationExpression(left, operation, right)` and `setSourcePosition(right)` 
only, so the rebuilt declarations carry no annotations by the time the verifier 
and any later-phase transformation look at them. 
`DeclarationExpression#transformExpression`, the canonical rebuild, copies the 
annotations; this change does the same for each single declaration the multiple 
assignment is split into (`bexp.addAnnotations(bin.getAnnotations())`).
   
   With the change, a misplaced annotation on a multiple assignment in a 
`@CompileStatic` class is reported once per declared variable, which matches 
what `@Override def a = 1, b = 2` already reports in both dynamic and static 
classes (the parser attaches the annotations to each declarator). A multiple 
assignment carrying a valid local-variable annotation, for example 
`@SuppressWarnings('unused') def (x, y) = [1, 2]`, or one with a TYPE_USE 
annotation, keeps compiling as before.
   
   Tests: `AssignmentsStaticCompileTest#testMultiAssignWithMisplacedAnnotation` 
fails without the fix ("Test passed but should have failed") and passes with 
it; `STCAssignmentTest#testMultiAssignWithAnnotation` covers the valid case 
under both `@TypeChecked` and `@CompileStatic`. Verified with `./gradlew :test 
--tests org.codehaus.groovy.classgen.asm.sc.AssignmentsStaticCompileTest 
--tests groovy.transform.stc.STCAssignmentTest` (132 + 131 tests, all green), 
then the surrounding packages `org.codehaus.groovy.classgen.asm.sc.*` (110 
classes, 2454 tests, 4 skipped, no failures) and `groovy.transform.stc.*` (49 
classes, 2211 tests, no failures), and `./gradlew :checkstyleMain` (same totals 
as without the change: 339 files, 574 warnings, 2755 errors, 9 info, all 
pre-existing). The reproducer script was also run against a distribution built 
from this branch and against 6.0.0, the GROOVY_6_0_X snapshot and unpatched 
master.
   
   Suggested backport: GROOVY_6_0_X (the defect reproduces on 6.0.0 and on the 
branch at 6.0.1-SNAPSHOT).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to