Paul King created GROOVY-12638:
----------------------------------
Summary: Elvis assignment (?=) to a trait field does not assign
the field
Key: GROOVY-12638
URL: https://issues.apache.org/jira/browse/GROOVY-12638
Project: Groovy
Issue Type: Bug
Reporter: Paul King
{code:groovy}
trait T {
Integer n
def m() { n ?= 5; n }
}
class C implements T {}
println new C().m()
{code}
Actual: prints {{null}} in dynamic and @TypeChecked code. With @CompileStatic,
compilation fails with {{General error during instruction selection: Internal
compiler error}}. The same happens inside a closure in trait code.
Expected: 5, as for the same field and method in an ordinary class, or for a
local variable in trait code.
Cause: NAryOperationRewriter rewrites the compound assignments of trait fields
using TokenUtil.removeAssignment, which has no entry for ELVIS_EQUAL, so {{n ?=
5}} is not rewritten. TraitReceiverTransformer handles only plain assignment
({{=}}) to a trait field specially, so the field on the left of {{?=}} is
transformed as a read (a call of the field helper's getter), and the value is
never written to the field. Rewriting {{x ?= y}} for a trait field as {{x = x
?: y}} (an ElvisOperatorExpression, since there is no binary elvis operator to
pass back) would give the expected result.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)