G
YouTube TikTok Games
Skip to content

GROOVY-12716: check a method pointer against the overloads its parameter types can select - #3250

Open
codeconsole wants to merge 1 commit into
apache:masterfrom
codeconsole:stc-method-pointer-selectable-methods
Open

codeconsole wants to merge 1 commit into
apache:masterfrom
codeconsole:stc-method-pointer-selectable-methods

Conversation

@codeconsole

Copy link
Copy Markdown
Contributor

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

…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-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.3417%. Comparing base (7a317c9) to head (a95c86b).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@                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     
Files with missing lines Coverage Δ
...roovy/transform/stc/StaticTypeCheckingVisitor.java 88.0772% <100.0000%> (+0.0368%) ⬆️

... and 13 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@testlens-app

testlens-app Bot commented Nov 6, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: a95c86b
▶️ Tests: 122015 executed
⚪️ Checks: 26/26 completed


Learn more about TestLens at testlens.app/docs.

@daniellansun daniellansun left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants