[
https://issues.apache.org/jira/browse/GROOVY-12834?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18151162#comment-18151162
]
ASF GitHub Bot commented on GROOVY-12834:
-----------------------------------------
paulk-asert commented on PR #3361:
URL: https://github.com/apache/groovy/pull/3361#issuecomment-7176055152
AI recommendation: merge it, then backport to 6.0.x and 5.1.x. It's a clear
regression since 5.0.0-alpha-3 and the fix is small.
> 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)