[
https://issues.apache.org/jira/browse/GROOVY-12613?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Paul King resolved GROOVY-12613.
--------------------------------
Fix Version/s: 7.0.0-alpha-1
6.0.1
Resolution: Fixed
> 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: 4.0.33, 5.0.8, 6.0.0
> Reporter: Scott Murphy Heiberg
> Assignee: Paul King
> Priority: Major
> Fix For: 7.0.0-alpha-1, 6.0.1
>
>
> {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.
> PR: https://github.com/apache/groovy/pull/3140
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)