[ 
https://issues.apache.org/jira/browse/GROOVY-12638?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18143356#comment-18143356
 ] 

ASF GitHub Bot commented on GROOVY-12638:
-----------------------------------------

paulk-asert merged PR #3161:
URL: https://github.com/apache/groovy/pull/3161




> 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
>    Affects Versions: 4.0.33, 5.1.3, 6.0.0
>            Reporter: Paul King
>            Priority: Major
>
> {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)

Reply via email to