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

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

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

   A method pointer assigned to or returned as a functional interface has its 
return type checked since GROOVY-12623, against the common type of every 
overload of the name. So an overloaded method that works on 6.0.0 no longer 
compiles on master:
   
   ```groovy
   @groovy.transform.CompileStatic
   class C {
       Function<Integer,Integer> f() { Math.&abs }                 // Possible 
loss of precision from double to java.lang.Integer
       BiFunction<Integer,Integer,Integer> g() { Math.&max }       // same
   }
   ```
   
   An `Integer` argument selects `abs(int)` and `max(int,int)` at run time, so 
`abs(double)` should not count.
   
   The check now uses the methods that values of the functional interface's 
parameter types can select: the best match for those types, and any method with 
narrower parameter types, which a value of a subtype selects. 
`ToIntFunction<Object> g = C.&f` with `int f(Object)` and `long f(Long)` is 
still reported, as `f(Long)` may be called, and so is `ToIntFunction<Number> h 
= Math.&abs`. All the candidates still count when the parameter types select 
none of them, or when an instance method referenced through its class takes the 
first parameter as its receiver. A method reference (`::`) is not affected.
   
   #3134 (GROOVY-12607) includes this commit, as it depends on it.
   
   ## Tests
   
   `MethodReferenceTest.testMethodPointerToOverloadsSelectedByParameterTypes` 
fails without the change, on its assignments and return of `Math.&abs` and 
`Math.&max`. It also runs them and covers the overloads that must still be 
reported. `groovy.transform.stc.*` and `org.codehaus.groovy.classgen.asm.sc.*` 
pass. The Checkstyle and CodeNarc gates pass.
   




> STC: an overloaded method pointer assigned to a functional interface is 
> checked against overloads its parameter types cannot select
> -----------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-12716
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12716
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>            Reporter: Scott Murphy Heiberg
>            Priority: Major
>
> {code:groovy}
> import java.util.function.*
> @groovy.transform.CompileStatic
> class C {
>     Function<Integer,Integer> f() { Math.&abs }
>     BiFunction<Integer,Integer,Integer> g() { Math.&max }
>     void h() { Function<Integer,Integer> i = Math.&abs; assert i.apply(-1) == 
> 1 }
> }
> {code}
> *Actual:* {{Possible loss of precision from double to java.lang.Integer}} for 
> each method pointer.
> *Expected:* it compiles and runs, as on 6.0.0. An {{Integer}} argument 
> selects {{abs(int)}} and {{max(int,int)}} at run time.
> The check for the return value of a method pointer takes the common type of 
> all the overloads of the name, here {{double}}, including those that values 
> of the functional interface's parameter types never select. Since 
> GROOVY-12623 (commit d1710f88eb) a method pointer gets its target type when 
> it is assigned or returned, so such a pointer now reaches this check.
> The overloads that count are the best match for the parameter types, and any 
> with narrower parameter types, which a value of a subtype selects. So 
> {{ToIntFunction<Object> g = C.&f}} with {{int f(Object)}} and {{long 
> f(Long)}} is still reported, as {{f(Long)}} may be called.
> Master only: 6.0.0 compiles and runs the example.



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

Reply via email to