Scott Murphy Heiberg created GROOVY-12613:
---------------------------------------------

             Summary: Compound assignment in a trait method evaluates its 
target twice when the target is not a trait field
                 Key: GROOVY-12613
                 URL: https://issues.apache.org/jira/browse/GROOVY-12613
             Project: Groovy
          Issue Type: Bug
    Affects Versions: 5.0.8, 4.0.33, 6.0.0
            Reporter: Scott Murphy Heiberg


{code: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]]
{code}
Actual: {{new C().m()}} returns {{[2, [1, 0]]}}. The index expression runs 
twice. The same method in a class returns {{[1, [1, 0]]}}. The receiver or 
subscript of any compound assignment in a trait method is evaluated twice, for 
example a map subscript key() is called twice. This happens with and without 
@CompileStatic / @TypeChecked.

Expected: {{[1, [1, 0]]}}. A compound assignment evaluates its target once, as 
it does in a class.

Cause: the trait transformation (NAryOperationRewriter) rewrites every compound 
assignment in trait code into a plain assignment (a = a op b). The rewrite is 
only needed when the target is a trait field, which becomes a field-helper 
getter/setter call.

Seen in 4.0.33, 5.0.8 and 6.0.0.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to