Repository navigation
GROOVY-12716: check a method pointer against the overloads its parameter types can select - #3250
codeconsole wants to merge 1 commit into
Conversation
…ter types can select A method pointer assigned to or returned as a functional interface now has its return type checked, but against the common type of every overload of the name. So Function<Integer,Integer> f = Math.&abs reported a loss of precision from double, which 6.0.0 compiled and ran: an Integer selects abs(int) at run time. Only the best match for the parameter types counts, and any method with narrower parameter types, which a value of a subtype selects.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3250 +/- ##
==================================================
+ Coverage 74.3385% 74.3417% +0.0032%
- Complexity 44310 44317 +7
==================================================
Files 1699 1699
Lines 150860 150875 +15
Branches 28755 28760 +5
==================================================
+ Hits 112147 112163 +16
+ Misses 28439 28436 -3
- Partials 10274 10276 +2
🚀 New features to boost your workflow:
|
✅ All tests passed ✅🏷️ Commit: a95c86b Learn more about TestLens at testlens.app/docs. |
daniellansun
left a comment
There was a problem hiding this comment.
LGTM for GROOVY-12716.
After GROOVY-12623 checked a method pointer’s return against the LUB of every overload, Math.&abs → Function<Integer,Integer> wrongly saw abs(double). Restricting the checked set to overloads the SAM parameter types can select (best match, plus narrower parameter types a subtype value may pick), and falling back to all candidates when none match or when a class-expression instance method takes the receiver as the first SAM parameter, matches run-time selection.
Tests cover abs/max positives, and the intentional ToIntFunction<Number> / ToIntFunction<Long> precision errors. CI fully green.
Merge notes: already merged into #3134’s tip — land this first, or treat #3134 as the combined landing vehicle. Related in spirit to Paul’s #3257 (functional overload choice), but this filters by parameter-type selectability for return checking, not by functional-argument arity.
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:
An
Integerargument selectsabs(int)andmax(int,int)at run time, soabs(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.&fwithint f(Object)andlong f(Long)is still reported, asf(Long)may be called, and so isToIntFunction<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.testMethodPointerToOverloadsSelectedByParameterTypesfails without the change, on its assignments and return ofMath.&absandMath.&max. It also runs them and covers the overloads that must still be reported.groovy.transform.stc.*andorg.codehaus.groovy.classgen.asm.sc.*pass. The Checkstyle and CodeNarc gates pass.