Repository navigation
Description
Passing a generic method reference directly as a method argument reports a type-parameter nullability mismatch, while assigning the same reference to the same functional interface type succeeds. Equivalent lambdas and references with an explicit nullable type argument also succeed.
Environment
- NullAway: 0.14.4-SNAPSHOT, locally published snapshot.
- Error Prone: 2.50.0.
- JSpecify annotations: 1.0.1.
- JDK: Temurin 25.0.5+8, compiling with
--release 17. - NullAway options:
-XepDisableAllChecks
-Xep:NullAway:ERROR
-XepOpt:NullAway:OnlyNullMarked=true
-XepOpt:NullAway:JSpecifyMode=true
-XepOpt:NullAway:JSpecifyExperimental=true
Both examples below were verified with standalone javac, with only JSpecify annotations on the compilation classpath and Error Prone/NullAway plus their dependencies on the processor path. Publisher and Mono are local declarations; the only JDK type used is java.util.function.Function.
Reproducer 1: all declarations under @NullMarked
Save as Repro.java:
import java.util.function.Function;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
class Repro {
@NullMarked
interface Publisher<T extends @Nullable Object> {}
static class Mono<T extends @Nullable Object> implements Publisher<T> {
static <T extends @Nullable Object> Mono<T> from(Publisher<? extends T> source) {
return new Mono<>();
}
}
Function<Publisher<?>, Object> assigned = Mono::from;
static void register(Function<Publisher<?>, Object> adapter) {}
void inlineReference() {
register(Mono::from);
}
void explicitTypeArgument() {
register(Mono::<@Nullable Object>from);
}
void lambda() {
register(source -> Mono.from(source));
}
void assignedReference() {
Function<Publisher<?>, Object> adapter = Mono::from;
register(adapter);
}
}Expected: All methods and assignments compile. In particular, Publisher, Mono, and from all permit nullable type arguments, so from can be instantiated with T = @Nullable Object for the Publisher<?> input.
Actual: Exactly one NullAway error, on register(Mono::from) in inlineReference():
error: [NullAway] parameter type of referenced method is Publisher<? extends T>, but parameter in functional interface method has type Publisher<?>, which has mismatched type parameter nullability
register(Mono::from);
^
The field initializer, local-variable assignment, explicit type argument, and lambda in the same file are all accepted.
Reproducer 2: local @NullUnmarked Publisher
Save as UnmarkedPublisherRepro.java:
import java.util.function.Function;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.NullUnmarked;
import org.jspecify.annotations.Nullable;
@NullMarked
class UnmarkedPublisherRepro {
@NullUnmarked
interface Publisher<T> {}
static class Mono<T extends @Nullable Object> implements Publisher<T> {
static <T extends @Nullable Object> Mono<T> from(Publisher<? extends T> source) {
return new Mono<>();
}
}
Function<Publisher<?>, Object> assigned = Mono::from;
static void register(Function<Publisher<?>, Object> adapter) {}
void inlineReference() {
register(Mono::from);
}
void explicitTypeArgument() {
register(Mono::<@Nullable Object>from);
}
void lambda() {
register(source -> Mono.from(source));
}
void assignedReference() {
Function<Publisher<?>, Object> adapter = Mono::from;
register(adapter);
}
}This produces exactly the same single error on the inline reference; all other forms are accepted. Mono and from retain nullable bounds as in the first reproducer.
Bound control
A separate verified variant with @NullMarked interface Publisher<T>, class Mono<T>, and static <T> Mono<T> from(Publisher<? extends T> source) accepts both the inline and assigned references. The discrepancy appears when the publisher's wildcard can have a nullable upper bound. The first reproducer demonstrates it with explicit nullable bounds and entirely null-marked application code.
Activity