typetools / checker-framework

Pluggable type-checking for Java
http://checkerframework.org/
Other
1.02k stars 354 forks source link

Error messages involving generic anonymous classes #1287

Open wmdietlGC opened 7 years ago

wmdietlGC commented 7 years ago

Run the Nullness Checker on:

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

interface I<T> {
  void foo(T p);
}

class Foo {
  void foo(I<@Nullable Object> p) {}
  void bar() {
    foo(new I<Object>() {
        public void foo(Object p) {}
      });
  }
}

You will get:

Bug.java:10: error: [argument.type.incompatible] incompatible types in argument.
    foo(new I<Object>() {
        ^
  found   : Foo.@Initialized @NonNull <anonymous Foo$1>
  required: @Initialized @NonNull I<@Initialized @Nullable Object>
1 error

The <anonymous Foo$1> is making it hard to see that the type argument mismatches.

If you change the example to have a Java type mismatch, e.g. to:

  void foo(I<String> p) {}
  void bar() {
    foo(new I<Object>() {

you get:

Bug.java:10: error: incompatible types: <anonymous I<Object>> cannot be converted to I<String>

That is, instead of Foo$1 you see the more useful I<Object>.

We should do the same thing for the Checker Framework.

wmdietl commented 7 years ago

Maybe fix together with #890