Scott Murphy Heiberg created GROOVY-12622:
---------------------------------------------

             Summary: SC evaluates the operand twice for ++ and -- on a 
non-primitive property or subscript
                 Key: GROOVY-12622
                 URL: https://issues.apache.org/jira/browse/GROOVY-12622
             Project: Groovy
          Issue Type: Bug
    Affects Versions: 5.0.8, 4.0.33, 6.0.0
            Reporter: Scott Murphy Heiberg


{code:groovy}
@groovy.transform.CompileStatic
class Box {
    int gets = 0
    BigDecimal d = 1
    BigDecimal getD() { gets++; d }
    void setD(BigDecimal v) { d = v }
}
@groovy.transform.CompileStatic
def m() { def b = new Box(); b.d++; [b.gets, b.@d] }
assert m() == [1, 2]

@groovy.transform.CompileStatic
def n() { int idx = 0; List<Integer> list = [10, 20]; def r = list[idx++]++; 
[idx, list, r] }
assert n() == [1, [11, 20], 10]
{code}
Actual: m() returns [2, 2] (getD is called twice), and n() returns [2, [21, 
20], 10]: the index expression runs twice and the incremented value of list[1] 
is stored into list[0].

Expected: [1, 2] and [1, [11, 20], 10], as in dynamic Groovy and with 
@TypeChecked.

When next() or previous() has a resolved target, 
StaticTypesBinaryExpressionMultiTypeDispatcher.writePostOrPrefixMethod discards 
the operand value already loaded and makes the call on the operand expression 
again. That evaluates the getter or getAt (and a subscript's receiver and 
index) a second time.

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