Repository navigation
Description
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:
pmd/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/internal/infer/Infer.java
Lines 948 to 981 in 5c9271c
| 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:
javac LambdaResult.java ImplicitLambda.java, also with--release 8, thenjavap -c -p LambdaResult ImplicitLambda.LambdaResultcallsf:(ILjava/util/function/Function;)Vandm:(Ljava/util/function/Supplier;I)V.ImplicitLambdacallsm:(Ljava/util/function/Supplier;ILjava/util/function/Predicate;)V.- Run PMD on
LambdaResult.javawith any Java rule andPMD_JAVA_X_TYPE_INFERENCE_LOGGING=simple. stderr shows[WARNING] Ambiguity error: all methods are maximally specificfor both calls. - Run PMD on
ImplicitLambda.javawithcategory/java/bestpractices.xml/UnusedPrivateMethod. It reports line 6.
Running PMD through: Other (PmdAnalysis API)
Activity
[java] Fix pmd#7148: Allow boxing of lambda and method reference resu…