typetools / checker-framework

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

Allow dataflow refinement of wildcard bounds #470

Open cushon opened 9 years ago

cushon commented 9 years ago

The checker fails to infer a non-null type for the wildcard in List<?> lx = ...:

import org.checkerframework.checker.nullness.qual.NonNull;
import java.util.List;

abstract class Test {
  abstract List<? extends @NonNull Object> getList();
  void f() {
    List<?> lx = getList();
    for (Object l : lx) {
      l.toString();
    }
  }
}
$ ~/jsr308/checker-framework-1.9.5/checker/bin/javac -processor org.checkerframework.checker.nullness.NullnessChecker -version Test.java
javac 1.8.0-jsr308-1.9.5
Test.java:9: error: [dereference.of.nullable] dereference of possibly-null reference l
      l.toString();
      ^
1 error
JonathanBurke commented 9 years ago

Thank you for raising this issue. Currently, our data flow framework does not refine the qualifiers on type arguments. This is a feature we have discussed in the past and wish to include in the Checker Framework but have not yet implemented.

wmdietlGC commented 7 years ago

Cross-ref to #260. One situation where one runs into this:

import java.util.Map;
import java.util.Map.Entry;

class Demo {
    void foo(Map<String, Object> m) {
        for (Entry<?, ?> e : m.entrySet()) {
            e.getValue().toString();
        }
    }
}

This gives an error on the call of toString(), because the wildcard is not refined.