Repository navigation
Description
In JSpecify mode, assigning a generic method reference to a functional interface whose parameter is @Nullable reports a nullability mismatch, although the referenced method's type variable can be inferred as @Nullable. #2175 removes the warning when the reference is passed directly as a call argument. The same reference against the same target type still gets the warning in a field initializer, a local variable initializer, a return, a conditional expression passed as an argument, and a lambda that returns the reference.
import java.util.function.Function;
import java.util.function.Supplier;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
class Test {
static class Box<T extends @Nullable Object> {}
static <T extends @Nullable Object> Box<T> box(T value) { return new Box<>(); }
static void acceptsNullable(Function<@Nullable String, Box<@Nullable String>> f) {}
static void acceptsSupplier(Supplier<Function<@Nullable String, Box<@Nullable String>>> s) {}
Function<@Nullable String, Box<@Nullable String>> field = Test::box;
void argument() { acceptsNullable(Test::box); }
void local() { Function<@Nullable String, Box<@Nullable String>> f = Test::box; }
Function<@Nullable String, Box<@Nullable String>> returned() { return Test::box; }
void conditional(boolean b) { acceptsNullable(b ? Test::box : Test::box); }
void lambdaResult() { acceptsSupplier(() -> Test::box); }
}I ran it as a CompilationTestHelper test in nullaway/src/test/java/com/uber/nullaway/jspecify/, with JSpecifyJavacConfig.withJSpecifyModeArgs and -XepOpt:NullAway:OnlyNullMarked=true, on JDK 21.0.9.
With #2175 applied (GenericsChecks.java from 8c40e57 on top of master), argument() on line 12 is accepted, and every other method still warns:
/Test.java:11: warning: [NullAway] parameter value of referenced method is @NonNull, but parameter in functional interface method java.util.function.Function.apply(@Nullable String) is @Nullable
/Test.java:13: warning: [NullAway] parameter value of referenced method is @NonNull, but parameter in functional interface method java.util.function.Function.apply(@Nullable String) is @Nullable
/Test.java:14: warning: [NullAway] parameter value of referenced method is @NonNull, but parameter in functional interface method java.util.function.Function.apply(@Nullable String) is @Nullable
/Test.java:15: warning: [NullAway] parameter value of referenced method is @NonNull, but parameter in functional interface method java.util.function.Function.apply(@Nullable String) is @Nullable
/Test.java:15: warning: [NullAway] parameter value of referenced method is @NonNull, but parameter in functional interface method java.util.function.Function.apply(@Nullable String) is @Nullable
/Test.java:16: warning: [NullAway] parameter value of referenced method is @NonNull, but parameter in functional interface method java.util.function.Function.apply(@Nullable String) is @Nullable
On master (01842a6, 0.14.5-SNAPSHOT, Error Prone 2.52.0) the same lines warn, and line 12 warns as well, with the same message and with referenced method returns Box<T>, but functional interface method returns Box<@Nullable String>, which has mismatched type parameter nullability.
Expected behavior
No warning on lines 11 and 13–16, as on line 12 with #2175 applied. box is declared as <T extends @Nullable Object>, so T can be inferred as @Nullable String from the target type Function<@Nullable String, Box<@Nullable String>> in each of these positions, just as it is for the direct argument.
Related
#2063 reports this false positive for a reference passed directly as an argument, and #2175 fixes that position. The Mono::from assignment in #2063's reproducer is accepted on master because its target has no @Nullable top-level parameter, so it does not exercise the case above. The placeholder PR #2080 says it applies consistent checks to method references across assignments, arguments, and returns; this issue tracks those positions until that work is split out.
Possible cause (from reading the source, not confirmed by an experiment)
In #2175, the target-based inference runs only from the call-argument check in GenericsChecks.compareGenericTypeParameterNullabilityForCall (direct or parenthesized argument). The other positions resolve the reference's method type through getInferredMethodTypeForGenericMethodReference, find no cached result, and check the declared type, where T is @NonNull.
Found while reviewing #2175 with Claude Code; I ran the reproducer above on both commits.
Activity
msridhar commentedon Nov 18, 2026
Also track generic method references passed to a generic call whose parameter is a bare type variable, as described in the review of #2175. These remain outside #2175's scope even when the reference is passed directly as an argument.
Expected: all four methods are accepted. The review reports that the first three still produce
parameter t of referenced method is @NonNull, but parameter in functional interface method java.util.function.Function.apply(@Nullable String) is @Nullablewith #2175 applied; the direct functional-interface argument is accepted.As the review explains, the enclosing call's joint inference sees an inference variable as the reference's target, adds no constraint on the reference's own
T, and still caches a result for the reference. The later check uses the solved targetFunction<@Nullable String, @Nullable String>with the reference'sTleft non-null. The follow-up should handle these cases together with the existing cases when resolving the reference's method type, while preserving #2175'sgenericEnclosingCallandexplicitEnclosingTypeArgumenttests.