typetools / checker-framework

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

Question on inference behavior on conditional expressions with existing upper bound #6113

Closed nimakarimipour closed 1 year ago

nimakarimipour commented 1 year ago

Tested on Checker Framework version 3.37.0-SNAPSHOT (master branch)


I was working with TaintingChecker and saw the reported errors from the CF on the code snippet below ([demo](http://eisop.uwaterloo.ca/live/#mode=display&code=import+org.checkerframework.checker.tainting.qual.Untainted%3B%0Aimport+java.util.Collections%3B%0Aimport+java.util.Collection%3B%0Aimport+java.util.List%3B%0Aimport+java.util.ArrayList%3B%0A%0Aclass+Foo%3CE%3E+%7B%0A++public+void+bar(List%3CE%3E+list)+%7B%0A++++++Collection%3C%3F+extends+E%3E+c+%3D+list+!%3D+null+%3F+list+%3A+Collections.emptyList()%3B%0A++%7D%0A%7D&typeSystem=nullness)):

import org.checkerframework.checker.tainting.qual.Untainted;
import java.util.Collections;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;

class Foo<E> {
  public void bar(List<E> list) {
      Collection<? extends E> c = list != null ? list : Collections.emptyList(); // reported error here
  }
}

The Checker Framework reports three errors on the designated line, please find them below:

error: [assignment] incompatible types in assignment.
    Collection<? extends E> c = list != null ? list : Collections.emptyList();
                                             ^
  found   : @Tainted Collection<?[ extends E[ extends @Tainted Object super @Tainted Void] super @Untainted Void]>
  required: @Tainted Collection<?[ extends E[ extends @Tainted Object super @Untainted Void] super @Untainted Void]>
error: [conditional] incompatible types in conditional expression.
    Collection<? extends E> c = list != null ? list : Collections.emptyList();
                                               ^
  found   : @Tainted List<E[ extends @Tainted Object super @Untainted Void]>
  required: @Tainted Collection<?[ extends E[ extends @Tainted Object super @Tainted Void] super @Untainted Void]>
error: [conditional] incompatible types in conditional expression.
    Collection<? extends E> c = list != null ? list : Collections.emptyList();
                                                                           ^
  found   : @Tainted List<E[ extends @Tainted Object super @Untainted Void]>
  required: @Tainted Collection<?[ extends E[ extends @Tainted Object super @Tainted Void] super @Untainted Void]>

Please note that if the line is written in any of the cases below, we won't see any errors:

Would you please let me know if this behavior is expected, or if there is way to resolve the errors. Thank you very much.

smillst commented 1 year ago

Thanks for reporting! This is a bug, which I've fixed in #6114.

nimakarimipour commented 1 year ago

@smillst Thank you for the quick reply and the fix, will try the fix.