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

   In trait code, a compound assignment nested in the right operand of another 
compound assignment was not rewritten:
   
   ```groovy
   trait T { int a = 1, b = 1; def m() { a += (b += 1); [a, b] } }
   class C implements T {}
   assert new C().m() == [3, 2]
   ```
   
   This threw `ClassCastException: Cannot cast java.lang.Integer to 
T$Trait$FieldHelper` in dynamic and type-checked code and `GroovyCastException` 
under `@CompileStatic`, in 4.0.33, 5.0.8 and 6.0.0 too. Static trait fields 
gave a `VerifyError` or a compile error. `a += (b++)` compiled and lost the 
increment, although a postfix update of a trait field is otherwise rejected.
   
   `NAryOperationRewriter` rewrites `x op= y` into `x = x op y` but built the 
new expression from the untransformed right operand. It now transforms the 
right operand first, so nested compound assignments are rewritten too and 
nested prefix/postfix updates of trait fields get the usual compile error.
   
   This touches only the right-operand line of 
`NAryOperationRewriter.transformBinaryExpression`, so it merges without 
conflict with #3130 (GROOVY-12603) and #3140 (GROOVY-12613), which change the 
same method.
   
   ## Tests
   
   `TraitASTTransformationTest.testNestedCompoundAssignmentOnTraitFields` and 
`testNestedCompoundAssignmentOnStaticTraitFields` run in dynamic, 
`@TypeChecked` and `@CompileStatic` modes. They cover properties, private and 
public fields, static fields, `this.a += (this.b += 1)`, three levels of 
nesting, `a = (b += 1)`, a compound inside a method argument and a local 
target. `testPostfixInRightOperandOfCompoundAssignmentOnTraitFieldRejected` 
checks that `a += (b++)` is rejected. All of them fail without the change. 
`org.codehaus.groovy.transform.traitx.*`, `groovy.transform.stc.*`, 
`org.codehaus.groovy.classgen.asm.sc.*`, `gls.*` and `groovy.ThisAndSuperTest` 
pass. The Checkstyle and CodeNarc gates pass.
   


-- 
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