checker-framework icon indicating copy to clipboard operation
checker-framework copied to clipboard

Nullness Checker error message uses incorrect parameter name

Open msridhar opened this issue 2 years ago • 1 comments
trafficstars

Test case:

import org.checkerframework.checker.nullness.qual.Nullable;
class Test {
    interface A<T1 extends @Nullable Object> {
        String fn(T1 o);
    }
    static String foo(Object o) { return o.toString(); }
    static void testPositive() {
        A<@Nullable Object> p = Test::foo;
    }
}

Error output from the Nullness Checker:

/tmp/Test.java:8: error: [methodref.param] Incompatible parameter type for o
        A<@Nullable Object> p = Test::foo;
                                ^
  found   : @Initialized @NonNull Object
  required: @Initialized @Nullable Object
  Consequence: method in @Initialized @NonNull Test
    @Initialized @NonNull String foo(@Initialized @NonNull Object p0)
  is not a valid method reference for method in @UnknownInitialization @Nullable A<@Initialized @Nullable Object>
    @Initialized @NonNull String fn(@Initialized @NonNull A<@Initialized @Nullable Object> this, @Initialized @Nullable Object p0)
1 error

In a couple of places the error message refers to p0 which is not the name of any variable in the program.

I also find the error message somewhat hard to read with all the (irrelevant) initializer annotations, but perhaps that is a separate issue.

msridhar avatar Oct 13 '23 21:10 msridhar

The p0 is coming from here: https://github.com/typetools/checker-framework/blob/0b8da55e27d1df4d4bee62ec6de2560384a176cf/framework/src/main/java/org/checkerframework/framework/type/DefaultAnnotatedTypeFormatter.java#L347-L348

There will still be cases where the parameter name is not in the element, but the parameter names for your test case will be there since they are from source.

I also find the error message somewhat hard to read with all the (irrelevant) initializer annotations, but perhaps that is a separate issue.

We have an issue for this: https://github.com/typetools/checker-framework/issues/2276. It's not actually too hard to fix, we just haven't done it.

smillst avatar Oct 13 '23 22:10 smillst