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

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

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

   Under `@CompileStatic`, a method reference whose method's return value must 
be converted by Groovy to the functional interface's primitive return type 
compiled, but failed at bootstrap:
   
   ```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  // LambdaConversionException: class 
java.lang.String is not convertible to boolean
           assert !p.test('') && p.test('x')
       }
   }
   ```
   
   Dynamic Groovy, `@TypeChecked` and the method pointer `C.&str` apply Groovy 
truth and give `[false, true]`. 4.0.33, 5.0.8 and 6.0.0 fail the same way. The 
same failure affected the other primitive returns the type checker accepts but 
`LambdaMetafactory` cannot convert, for example `ToDoubleFunction` from a 
`BigDecimal` method and `ToIntFunction` from a `long` or `Long` method. 
`LambdaMetafactory` converts a return value to a primitive type only by 
unboxing and widening primitive conversion.
   
   `StaticTypesMethodReferenceExpressionWriter` already routes some method 
references through a synthetic static adapter to convert `GString` parameters. 
That adapter is now also used when the return value needs a conversion the 
metafactory does not perform. The adapter then returns the SAM's primitive 
type, and its statically compiled `return` converts the value like a cast: 
Groovy truth for `boolean`, Number conversion for the numeric types. Method 
references whose return value the metafactory can convert still bind the method 
directly.
   
   ## Tests
   
   `MethodReferenceTest` adds three tests that fail without the change: 
`testPrimitiveBooleanSAMFromNonBooleanReturnType` (dynamic, `@TypeChecked` and 
`@CompileStatic`, with static, instance, bound, extension, constructor and 
variadic references), 
`testPrimitiveBooleanSAMFromNonBooleanReturnTypeSerializable` and 
`testPrimitiveNumberSAMFromNonWideningReturnType`. The `groovy.transform.stc`, 
`org.codehaus.groovy.classgen.asm.sc`, `bugs` and `groovy.bugs` tests and 
`LambdasTest` pass. The Checkstyle and CodeNarc gates pass.
   




> 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.



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

Reply via email to