typetools / typetools/checker-framework
Interaction between @PolyNull and method type argument inference
Open
@wmdietl is already working on this.
Since Jul 17, 2019.
False Positive (false warning or imprecision)
- Dominant language
- Java
- Stars
- 1.1k
- Forks
- 440
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 134
Description
Take:
import org.checkerframework.checker.nullness.qual.PolyNull;
import java.util.function.Function;
interface Transform<S, T> {
@PolyNull T transform(@PolyNull S in);
}
abstract class Demo {
abstract <A, B> Function<A, B> map(Function<? super A, ? extends B> f);
<C> C then(C in) { return in; }
void use(Transform<Object, String> tf) {
then(map(x -> tf.transform(x)));
then(map(tf::transform));
}
}
The signature of Transform::transform ensures that the nullness of the parameter and return type are equal, ignoring the nullness on the type arguments.
Functions map and then have signatures similar to many streaming APIs.
Running the Nullness Checker gives:
PolyTest.java:15: error: [methodref.return.invalid] Incompatible return type
then(map(tf::transform));
^
Method
@Initialized @Nullable String transform(@Initialized @NonNull Transform<@Initialized @NonNull Object, @Initialized @NonNull String> this, @Initialized @Nullable Object p0) in Transform
is not a valid method reference for
@Initialized @NonNull String apply(@Initialized @NonNull Function<? extends @Initialized @Nullable Object, @Initialized @NonNull String> this, ? extends @Initialized @Nullable Object p0) in java.util.function.Function
found : @Initialized @Nullable String
required: @Initialized @NonNull String
1 error
Note that the error is only raised for the method reference.
The equivalent use of a lambda doesn't raise this error.
This is probably related to #979, but I didn't see any tests that use @PolyNull in checker/tests/nullness/java8inference.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.