[
https://issues.apache.org/jira/browse/GROOVY-12615?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Scott Murphy Heiberg updated GROOVY-12615:
------------------------------------------
Description:
{code:groovy}
trait T { int a = 1, b = 1; def m() { a += (b += 1); [a, b] } }
class C implements T {}
assert new C().m() == [3, 2]
{code}
Actual: dynamic and type-checked code throws {{java.lang.ClassCastException:
Cannot cast java.lang.Integer to T$Trait$FieldHelper}}; with @CompileStatic it
throws {{GroovyCastException: Cannot cast object '2' with class
'java.lang.Integer' to class 'T'}}. With static trait fields it throws a
VerifyError (dynamic) or fails to compile (@CompileStatic). A postfix update in
the same position, {{a += (b++)}}, compiles and silently loses the increment,
although postfix updates of trait fields are a compile error everywhere else.
Expected: [3, 2], and a postfix update there rejected with "Postfix expressions
on trait fields/properties are not supported in traits."
Cause: NAryOperationRewriter rewrites x op= y into x = x op y but reuses the
right operand without transforming it, so a nested compound assignment to a
trait field stays a compound assignment applied to the field helper call, and a
nested postfix update is never checked.
Seen in 4.0.33, 5.0.8 and 6.0.0.
PR: https://github.com/apache/groovy/pull/3141
was:
{code:groovy}
trait T { int a = 1, b = 1; def m() { a += (b += 1); [a, b] } }
class C implements T {}
assert new C().m() == [3, 2]
{code}
Actual: dynamic and type-checked code throws {{java.lang.ClassCastException:
Cannot cast java.lang.Integer to T$Trait$FieldHelper}}; with @CompileStatic it
throws {{GroovyCastException: Cannot cast object '2' with class
'java.lang.Integer' to class 'T'}}. With static trait fields it throws a
VerifyError (dynamic) or fails to compile (@CompileStatic). A postfix update in
the same position, {{a += (b++)}}, compiles and silently loses the increment,
although postfix updates of trait fields are a compile error everywhere else.
Expected: [3, 2], and a postfix update there rejected with "Postfix expressions
on trait fields/properties are not supported in traits."
Cause: NAryOperationRewriter rewrites x op= y into x = x op y but reuses the
right operand without transforming it, so a nested compound assignment to a
trait field stays a compound assignment applied to the field helper call, and a
nested postfix update is never checked.
Seen in 4.0.33, 5.0.8 and 6.0.0.
> Compound assignment to a trait field nested in the right operand of another
> compound assignment fails at run time
> -----------------------------------------------------------------------------------------------------------------
>
> Key: GROOVY-12615
> URL: https://issues.apache.org/jira/browse/GROOVY-12615
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 4.0.33, 5.0.8, 6.0.0
> Reporter: Scott Murphy Heiberg
> Priority: Major
>
> {code:groovy}
> trait T { int a = 1, b = 1; def m() { a += (b += 1); [a, b] } }
> class C implements T {}
> assert new C().m() == [3, 2]
> {code}
> Actual: dynamic and type-checked code throws {{java.lang.ClassCastException:
> Cannot cast java.lang.Integer to T$Trait$FieldHelper}}; with @CompileStatic
> it throws {{GroovyCastException: Cannot cast object '2' with class
> 'java.lang.Integer' to class 'T'}}. With static trait fields it throws a
> VerifyError (dynamic) or fails to compile (@CompileStatic). A postfix update
> in the same position, {{a += (b++)}}, compiles and silently loses the
> increment, although postfix updates of trait fields are a compile error
> everywhere else.
> Expected: [3, 2], and a postfix update there rejected with "Postfix
> expressions on trait fields/properties are not supported in traits."
> Cause: NAryOperationRewriter rewrites x op= y into x = x op y but reuses the
> right operand without transforming it, so a nested compound assignment to a
> trait field stays a compound assignment applied to the field helper call, and
> a nested postfix update is never checked.
> Seen in 4.0.33, 5.0.8 and 6.0.0.
> PR: https://github.com/apache/groovy/pull/3141
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)