Skip to content

[java] Strict phase rejects lambda and method reference results that need boxing #7148

Closed

Description

@saryn17

Affects PMD Version: 7.28.0 (also 7.29.0-SNAPSHOT)

Description:

In the first sample, javac selects f(int, Function) and m(Supplier, int) in phase 1. PMD finds both overloads of each pair applicable in the loose phase only and ends with an ambiguity, so getOverloadSelectionInfo().isFailed() is true for both calls.

In the strict phase, the result of a lambda body and the return type of a method reference are checked with the checker of that phase, which does not box:

ExprChecker exprChecker =
(ctx, exprType, formalType1) -> checkConvertibleOrDefer(ctx, exprType, formalType1, arg, phase, site);
ExprCheckHelper helper = new ExprCheckHelper(infCtx, phase, exprChecker, site, this);
if (!helper.isCompatible(formalType, arg)) {
throw ResolutionFailedException.incompatibleFormalExprNoReason(logger, arg, formalType);
}
}
/**
* Add a compatibility constraint between an exprType and a formalType.
* This asserts {@code exprType <: formalType}, the arg parameter is only
* used for reporting.
*
* <p>This method is called back to by {@link ExprCheckHelper#isCompatible(JTypeMirror, ExprMirror)}.
*/
void checkConvertibleOrDefer(InferenceContext infCtx, JTypeMirror exprType, JTypeMirror formalType, ExprMirror arg, MethodResolutionPhase phase, @Nullable MethodCallSite site) {
if (!infCtx.isGround(formalType) || !infCtx.isGround(exprType)) {
// defer the check
infCtx.addInstantiationListener(setOf(formalType, exprType), solvedCtx -> checkConvertibleOrDefer(solvedCtx, exprType, formalType, arg, phase, site));
}
JTypeMirror groundE = infCtx.ground(exprType);
JTypeMirror groundF = infCtx.ground(formalType);
// This method call does all the work of adding constraints
// If groundE or groundF are in fact not ground, then constraints
// on the ivars that appear within them are implicitly added during
// the subtyping check. The call then returns true and we return
// normally
// If they are ground, then they must conform to each other else
// the exception stops the resolution process.
Convertibility isConvertible = isConvertible(groundE, groundF, phase.canBox());

ExprCheckHelper uses that checker for method reference return types (lines 374 and 480) and for lambda result expressions (line 635). So () -> 1 is rejected for Supplier<Integer>, and LambdaResult::len with int len(String) for Function<String, Integer>. JLS 15.27.3 requires each result expression to be "compatible with R in an assignment context", and a note there says that "the result expression appears in an assignment context, regardless of the context enclosing the lambda expression". 15.13.2 checks the return type of a method reference in an assignment context too. Boxing is allowed there (JLS 5.2).

The second sample adds an implicitly typed lambda, so the invocation phase is not skipped. It rebuilds the declaration without the failed flag (Infer.java:387-395), and the AST ends up with m(Supplier, Integer, Predicate) as the selected method. javac selects m(Supplier, int, Predicate) in phase 1. UnusedPrivateMethod reports that method on line 6 as unused.

Found while working on #7136.

Code Sample demonstrating the issue:

import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.ToIntFunction;

public class LambdaResult {
    static int len(String s) { return s.length(); }

    static void f(int x, Function<String, Integer> g) { }
    static void f(Integer x, ToIntFunction<String> g) { }

    static void m(Supplier<Integer> s, int x) { }
    static void m(Supplier<Integer> s, Integer x) { }

    void t(int i) {
        f(i, LambdaResult::len);
        m(() -> 1, i);
    }
}
import java.util.function.Predicate;
import java.util.function.Supplier;

public class ImplicitLambda {
    private static void m(Supplier<Integer> s, Integer x, Predicate<String> p) { }
    private static void m(Supplier<Integer> s, int x, Predicate<String> p) { }

    void t(int i) {
        m(() -> 1, i, t -> t.isEmpty());
    }
}

Steps to reproduce:

  1. javac LambdaResult.java ImplicitLambda.java, also with --release 8, then javap -c -p LambdaResult ImplicitLambda. LambdaResult calls f:(ILjava/util/function/Function;)V and m:(Ljava/util/function/Supplier;I)V. ImplicitLambda calls m:(Ljava/util/function/Supplier;ILjava/util/function/Predicate;)V.
  2. Run PMD on LambdaResult.java with any Java rule and PMD_JAVA_X_TYPE_INFERENCE_LOGGING=simple. stderr shows [WARNING] Ambiguity error: all methods are maximally specific for both calls.
  3. Run PMD on ImplicitLambda.java with category/java/bestpractices.xml/UnusedPrivateMethod. It reports line 6.

Running PMD through: Other (PmdAnalysis API)

Activity

  1. added
    in:type-resolution
    on Oct 27, 2026
  2. added this to the 7.29.0 milestone on Oct 27, 2026
  3. added a commit that references this issue on Oct 27, 2026
    785a6a0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in:type-resolution

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions