[
https://issues.apache.org/jira/browse/GROOVY-12838?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18151962#comment-18151962
]
Jochen Theodorou commented on GROOVY-12838:
-------------------------------------------
we should consider handling this like conversion to boolean, plus cast. In that
case invokedynamic would not even be used
> Assigning a primitive number to a Boolean fails at run time with
> WrongMethodTypeException
> -----------------------------------------------------------------------------------------
>
> Key: GROOVY-12838
> URL: https://issues.apache.org/jira/browse/GROOVY-12838
> Project: Groovy
> Issue Type: Bug
> Affects Versions: 4.0.33, 5.1.3, 6.0.0
> Reporter: Paul King
> Priority: Major
>
> Assigning a primitive {{int}}, {{long}} or {{double}} to a variable, field or
> return value of type {{Boolean}} fails at run time, in dynamic,
> {{@TypeChecked}} and {{@CompileStatic}} code alike:
> {code:groovy}
> Boolean b = 0
> {code}
> {noformat}
> java.lang.invoke.WrongMethodTypeException: cannot convert
> MethodHandle(int)int to (int)Boolean
> at
> org.codehaus.groovy.vmplugin.v8.Selector$CastSelector.handleBoolean(Selector.java:366)
> at
> org.codehaus.groovy.vmplugin.v8.Selector$CastSelector.setCallSiteTarget(Selector.java:287)
> at
> org.codehaus.groovy.vmplugin.v8.IndyInterface.fallback(IndyInterface.java:777)
> {noformat}
> Expected: {{false}} (Groovy truth), as for a boxed value or a primitive
> {{boolean}} target.
> Results (same on 4.0.28, 5.1.3, 6.0.0 and current master):
> ||Code||Dynamic||@TypeChecked||@CompileStatic||
> |{{Boolean b = 0}} (also {{1}}, {{0L}}, {{0.0d}}, an {{int}}
> variable)|WrongMethodTypeException|WrongMethodTypeException|WrongMethodTypeException|
> |{{Boolean b; b = 0}}, a {{Boolean}} field initialised with {{0}}, {{Boolean
> f() \{ 0 \}}}|-|-|WrongMethodTypeException|
> |{{Integer n = 0; Boolean b = n}}|-|-|{{false}}|
> |{{boolean b = 0}}|-|-|{{false}}|
> |{{Boolean b = 'x'}}|-|-|{{true}}|
> |{{(Boolean) 0}}|-|-|compile error "Inconvertible types" (expected)|
> Likely cause: for a {{Boolean}} target, {{CastSelector.handleBoolean}} guards
> the {{asBoolean}} call with a null check whose null branch is
> {{MethodHandles.identity(staticSourceType).asType(methodType(Boolean.class,
> staticSourceType))}}. For a primitive source type that conversion does not
> exist ({{int}} boxes to {{Integer}}, not {{Boolean}}), hence the exception. A
> primitive cannot be null, so the guard could be skipped, or the value boxed
> first, when {{staticSourceType}} is primitive. The {{boolean}} target works
> because its null branch is {{constant(false)}}.
> Workaround: assign through a boxed value ({{Integer n = 0; Boolean b = n}})
> or a primitive {{boolean}}.
> On master this also shows through the GROOVY-12535 map value conversion:
> {{Map<String,Boolean> m = [:]; m.k = 0}} compiles and then fails with the
> same exception.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)