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

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

jdaugherty opened a new pull request, #3361:
URL: https://github.com/apache/groovy/pull/3361

   Since 5.0.0-alpha-3, `x as boolean` on a null value throws 
`IllegalArgumentException: null to boolean` when the class is compiled with 
classic call sites. With invokedynamic, and on Groovy 4 in both modes, it is 
`false`. Dynamic and `@CompileStatic` code behave the same; casts, assignments 
and Groovy truth are not affected.
   
   `InvocationWriter.coerce()` passed the target type as written to 
`ScriptBytecodeAdapter.asType()`, so the call reached 
`NullObject.asType(boolean.class)`, which rejects primitive types since the 
change for GROOVY-11196. `InvokeDynamicWriter.coerce()` asks for the wrapper 
type and lets the cast that follows convert to the primitive. The classic 
writer now does the same.
   
   With classic call sites, `null as int` consequently fails as it does with 
invokedynamic, with a `NullPointerException` from the unboxing instead of 
`IllegalArgumentException: null to int`.
   
   `bugs.Groovy12834` coerces a null, an empty and a non-empty value to 
`boolean` in dynamic and in `@CompileStatic` code, with invokedynamic and with 
classic call sites, and coerces non-null values to `int` and `char`. The two 
classic `boolean` cases fail without the fix.
   
   - Jira: [GROOVY-12834](https://issues.apache.org/jira/browse/GROOVY-12834)
   - Reproducer: 
https://github.com/jdaugherty/groovy-null-as-primitive-reproducer
   - Backport to `GROOVY_6_0_X`: 
https://github.com/jdaugherty/groovy/commit/c0b1330cfbfde3f58474815e8037eca4b0d34dc2,
 on the branch 
[`GROOVY-12834-6_0_X`](https://github.com/jdaugherty/groovy/tree/GROOVY-12834-6_0_X).
   - Backport to `GROOVY_5_1_X`: 
https://github.com/jdaugherty/groovy/commit/514673a59a72f3a20b162c6e3ed462c3357a41f0,
 on the branch 
[`GROOVY-12834-5_1_X`](https://github.com/jdaugherty/groovy/tree/GROOVY-12834-5_1_X).
   
   Reproduced on 5.0.7, 5.1.3, 6.0.0 and master with classic call sites only; 
4.0.33 is not affected. Could this be backported to 6.0.x and 5.x once it is 
merged?




> null as boolean throws IllegalArgumentException with classic call sites
> -----------------------------------------------------------------------
>
>                 Key: GROOVY-12834
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12834
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 5.1.3, 6.0.0
>            Reporter: James Daugherty
>            Priority: Major
>
> Coercing a null value to {{boolean}} with the {{as}} operator
> {code:groovy}
> class Holder {
>     Object value
> }
> boolean present(Holder h) {
>     h.value as boolean
> }
> assert !present(new Holder())
> {code}
> gives {{false}} on Groovy 4, and on Groovy 5 and later when the class is 
> compiled for invokedynamic. Compiled with classic call sites ({{indy=false}}) 
> on Groovy 5 and later it throws:
> {noformat}
> java.lang.IllegalArgumentException: null to boolean
> {noformat}
> The result is the same in dynamic and in {{@CompileStatic}} code. Only the 
> {{as}} form is affected: a cast ({{(boolean) x}}), an assignment to a 
> {{boolean}}, a {{boolean}} return and Groovy truth ({{x ? true : false}}, 
> {{!!x}}) give {{false}} in every mode.
> ||Expression, value is null||4.0.33, indy||4.0.33, classic||5.x / 6.x / 
> master, indy||5.x / 6.x / master, classic||
> |{{x as boolean}}|false|false|false|*IllegalArgumentException: null to 
> boolean*|
> |{{(boolean) x}}|false|false|false|false|
> |{{boolean b = x}}|false|false|false|false|
> |{{x ? true : false}}|false|false|false|false|
> {{x as boolean}} is a common way to write "is this set", so an application 
> that compiles without invokedynamic (the Grails default up to Grails 8) meets 
> this at run time, at each such expression whose value happens to be null, 
> after moving from Groovy 4 to Groovy 5.
> *Reproducer:* 
> https://github.com/jdaugherty/groovy-null-as-primitive-reproducer 
> ({{./gradlew -q reproduce}}) compiles and runs each form with 4.0.33, 5.1.3 
> and 6.0.0, with invokedynamic and with classic call sites.
> *Cause:* the two bytecode writers coerce differently. 
> {{InvocationWriter.coerce()}} (classic) passes the target type as written to 
> {{ScriptBytecodeAdapter.asType(Object, Class)}}, so {{null as boolean}} calls 
> {{NullObject.asType(boolean.class)}}. Since 5.0.0-alpha-3 that method rejects 
> primitive types (it came in with the change for GROOVY-11196):
> {code:java}
> public Object asType(final Class c) {
>     if (c.isPrimitive()) throw new IllegalArgumentException("null to " + c);
>     return null;
> }
> {code}
> Before, it returned null and the cast to {{boolean}} that follows turned null 
> into {{false}}. {{InvokeDynamicWriter.coerce()}} asks for the wrapper type 
> instead, {{asType(Boolean)}}, and casts the result to {{boolean}}, so the 
> check is never reached.
> *Suggested fix:* make the classic writer agree with the invokedynamic writer: 
> in {{InvocationWriter.coerce()}}, call {{asType}} with the wrapper of a 
> primitive target type and leave the conversion to the primitive to the cast 
> that follows.
> Reproduced with classic call sites on 5.0.7, 5.1.3, 6.0.0 and master. Not 
> reproduced on 4.0.33, or on any of these versions with invokedynamic.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to