Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method type argument inference and @MonotonicNonNull #1394

Open
wmdietlGC opened this issue Jul 10, 2017 · 1 comment
Open

Method type argument inference and @MonotonicNonNull #1394

wmdietlGC opened this issue Jul 10, 2017 · 1 comment

Comments

@wmdietlGC
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants