[ 
https://issues.apache.org/jira/browse/GROOVY-12617?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Scott Murphy Heiberg updated GROOVY-12617:
------------------------------------------
    Description: 
{code:groovy}
import java.util.function.Predicate
@groovy.transform.CompileStatic
class C {
    static String str(String s) { s }
    static void main(args) {
        Predicate<String> p = C::str
        assert !p.test('') && p.test('x')
    }
}
{code}
Actual: the code compiles, but evaluating the method reference throws 
{{java.lang.BootstrapMethodError}}, caused by 
{{java.lang.invoke.LambdaConversionException: Type mismatch for lambda expected 
return: class java.lang.String is not convertible to boolean}}. The same 
happens for every primitive return that the type checker accepts but the JVM 
cannot convert: boolean from any reference type or from int, ToDoubleFunction 
from a BigDecimal method, and ToIntFunction from a long or Long method.

Expected: the value is converted as in dynamic Groovy, @TypeChecked code and a 
method pointer ({{C.&str}}), which all give [false, true] here (Groovy truth).

Cause: the static compiler binds the method directly through LambdaMetafactory, 
which converts a return value to a primitive type only by unboxing and widening 
primitive conversion.

Seen in 4.0.33, 5.0.8 and 6.0.0.

PR: https://github.com/apache/groovy/pull/3143

  was:
{code:groovy}
import java.util.function.Predicate
@groovy.transform.CompileStatic
class C {
    static String str(String s) { s }
    static void main(args) {
        Predicate<String> p = C::str
        assert !p.test('') && p.test('x')
    }
}
{code}
Actual: the code compiles, but evaluating the method reference throws 
{{java.lang.BootstrapMethodError}}, caused by 
{{java.lang.invoke.LambdaConversionException: Type mismatch for lambda expected 
return: class java.lang.String is not convertible to boolean}}. The same 
happens for every primitive return that the type checker accepts but the JVM 
cannot convert: boolean from any reference type or from int, ToDoubleFunction 
from a BigDecimal method, and ToIntFunction from a long or Long method.

Expected: the value is converted as in dynamic Groovy, @TypeChecked code and a 
method pointer ({{C.&str}}), which all give [false, true] here (Groovy truth).

Cause: the static compiler binds the method directly through LambdaMetafactory, 
which converts a return value to a primitive type only by unboxing and widening 
primitive conversion.

Seen in 4.0.33, 5.0.8 and 6.0.0.


> SC: a method reference to a functional interface with a primitive return type 
> fails at bootstrap when the return value needs a Groovy conversion (e.g. 
> String to boolean)
> -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12617
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12617
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 4.0.33, 5.0.8, 6.0.0
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> {code:groovy}
> import java.util.function.Predicate
> @groovy.transform.CompileStatic
> class C {
>     static String str(String s) { s }
>     static void main(args) {
>         Predicate<String> p = C::str
>         assert !p.test('') && p.test('x')
>     }
> }
> {code}
> Actual: the code compiles, but evaluating the method reference throws 
> {{java.lang.BootstrapMethodError}}, caused by 
> {{java.lang.invoke.LambdaConversionException: Type mismatch for lambda 
> expected return: class java.lang.String is not convertible to boolean}}. The 
> same happens for every primitive return that the type checker accepts but the 
> JVM cannot convert: boolean from any reference type or from int, 
> ToDoubleFunction from a BigDecimal method, and ToIntFunction from a long or 
> Long method.
> Expected: the value is converted as in dynamic Groovy, @TypeChecked code and 
> a method pointer ({{C.&str}}), which all give [false, true] here (Groovy 
> truth).
> Cause: the static compiler binds the method directly through 
> LambdaMetafactory, which converts a return value to a primitive type only by 
> unboxing and widening primitive conversion.
> Seen in 4.0.33, 5.0.8 and 6.0.0.
> PR: https://github.com/apache/groovy/pull/3143



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

Reply via email to