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

   In a trait method, every compound assignment `a op= b` was rewritten to `a = 
a op b`, so its target was evaluated twice:
   
   ```groovy
   trait T {
       def m() { def l = [0, 0]; int i = 0; l[i++] += 1; [i, l] }
   }
   class C implements T {}
   assert new C().m() == [1, [1, 0]]   // was [2, [1, 0]]; a class method gives 
[1, [1, 0]]
   ```
   
   4.0.33, 5.0.8 and 6.0.0 behave the same. `NAryOperationRewriter` does this 
rewrite so that a compound assignment to a trait field can become a 
field-helper getter and setter call. It applied it to every target, so for a 
local, a subscript or another object's property it changed the evaluation order 
and count: `l[i++] += 1` incremented `i` twice and `map[key()] += 1` called 
`key()` twice. The rewriter now leaves a compound assignment unchanged unless 
its target is a trait field or a `this.`-qualified trait field 
(`isInternalFieldAccess`, the same check the prefix and postfix rejection 
uses). Compound assignment to private, public and static trait fields and to 
trait properties is rewritten as before.
   
   #3130 (GROOVY-12603) also changes 
`NAryOperationRewriter.transformBinaryExpression`, to visit closures; whichever 
lands second keeps this unconditional field check and drops the closure-only 
one there.
   
   ## Tests
   
   `TraitASTTransformationTest.testCompoundAssignmentEvaluatesTargetOnce` fails 
without the change in dynamic, `@TypeChecked` and `@CompileStatic` modes. 
`testCompoundAssignmentOfTraitFieldOrProperty` covers compound assignment to 
trait fields and properties in the same three modes. 
`org.codehaus.groovy.transform.traitx`, `groovy.transform.stc`, 
`org.codehaus.groovy.classgen.asm.sc`, `gls.innerClass`, `PackedClosure*` 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