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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to