typetools / typetools/checker-framework

Method type argument inference and @MonotonicNonNull

Open
#1,394 1 comment 0 reactions 1 assignee View on GitHub

@wmdietl is already working on this.

Since Aug 1, 2017.

False Positive (false warning or imprecision)
Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
1d 12h
Merged PRs (30d)
134

Description

Run the Nullness Checker on the following example:

import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

abstract class InferenceMonotonicNonNull {
    abstract <T extends @Nullable Object> @NonNull T foo(T p);
    @MonotonicNonNull Object field;

    void foo() {
        this.foo(field);
        this.<@Nullable Object>foo(field);
    }
}

We get:

MNN.java:10: error: [monotonic.type.incompatible] cannot assign org.checkerframework.checker.nullness.qual.MonotonicNonNull to org.checkerframework.checker.nullness.qual.MonotonicNonNull (monotonic type).
                        this.foo(field);
                                     ^
  full type found: @Initialized @MonotonicNonNull Object
1 error

The read of field has type @MonotonicNonNull, which is then the inferred type argument type for foo.
However, when the checker then checks whether the argument type is a subtype of the parameter type, the rule for @MonotonicNonNull prevents that the @MonotonicNonNull is assigned to itself.

I think in general the type of the field read this.field should be @Nullable, not @MonotonicNonNull.
When making the method type argument explicitly @Nullable, the problem goes away.

The alternative would be to change method type argument inference to avoid inferring certain annotations, in particular @MonotonicNonNull.
For @MonotonicNonNull I think I would first try changing the type of the read to @Nullable; if we can already think of other cases where avoiding certain annotations would be required, we should add it as another constraint for #979.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.