Skip to content

fix: implement phase-2 poly-expression inference for method/construct or references (JLS §15.12.2.7)#5031

Merged
jlerbsc merged 1 commit into
javaparser:masterfrom
jlerbsc:master
May 30, 2026
Merged

fix: implement phase-2 poly-expression inference for method/construct or references (JLS §15.12.2.7)#5031
jlerbsc merged 1 commit into
javaparser:masterfrom
jlerbsc:master

Conversation

@jlerbsc

@jlerbsc jlerbsc commented May 30, 2026

Copy link
Copy Markdown
Collaborator

After the initial type-variable inference pass (Phase 1), remaining type variables whose only source of information is a poly-expression argument (lambda or method/constructor reference) were left unresolved — for example K in groupingBy(String::new, counting()) stayed as InferenceVariable instead of resolving to String.

Add inferTypeVariablesFromPolyExpressionArguments in MethodCallExprContext: for each argument position whose AST node is a method or constructor reference, retrieve the SAM of the formal functional interface type (with Phase-1 substitutions applied), unwrap any ? extends/? super wildcard on its return type, compute the actual return type of the referenced callable (X for X::new, declared return type for Foo::bar), and match the two to derive additional type-variable bindings. Call this Phase-2 pass in both the instance-method branch and the static (NameExpr) branch of solveMethodAsUsage. Promote JavaParserFacade.toMethodUsage from protected to public to allow access from MethodCallExprContext.

Update the resolveStreamCollectGroupingByWithConstructorReference test to assert the fully resolved type Map<String, Long> instead of the previous relaxed structural checks.

@jlerbsc jlerbsc merged commit 844c440 into javaparser:master May 30, 2026
31 of 32 checks passed
@jlerbsc jlerbsc added this to the next release milestone May 30, 2026
@jlerbsc jlerbsc added PR: Fixed A PR that offers a fix or correction PR: Added A PR that introduces new behaviour (e.g. functionality, tests) and removed PR: Fixed A PR that offers a fix or correction labels May 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: Added A PR that introduces new behaviour (e.g. functionality, tests)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant