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

false negative when calling two polymorphic functions with omitted type arguments

Open theosotr opened this issue 1 year ago • 1 comments

Command

javac -J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \
  -J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED \
-processor org.checkerframework.checker.nullness.NullnessChecker \
-cp checker-framework/checker/dist/checker.jar Test.java

File

import java.util.*;
import java.util.function.*;
import org.checkerframework.checker.nullness.qual.*;


class A {
    public <T> @Nullable T m(T x) {
        return null;
    }

}

public class Test {
    public static <T> void invokeAny(Collection<Supplier<T>> p) {
        p.toString();

    }

    public static void main(String[] args) {
        A x = new A();
        invokeAny(x.m(new LinkedList<Supplier<String>>()));

    }
}

Actual behavior

The code passes the checks, but there's NPE at runtime

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Collection.toString()" because "<parameter1>" is null
	at Test.invokeAny(Test.java:15)
	at Test.main(Test.java:21)

Expected behavior

The code should have been rejected.

theosotr avatar Oct 02 '24 13:10 theosotr

It seems that it has been addressed in 3.51.1

Test.java:21: error: [argument] incompatible argument for parameter p of Test.invokeAny.
        invokeAny(x.m(new LinkedList<Supplier<String>>()));
                     ^
  found   : @Initialized @Nullable LinkedList<@Initialized @NonNull Supplier<@Initialized @NonNull String>>
  required: @Initialized @NonNull Collection<@Initialized @NonNull Supplier<@Initialized @NonNull String>>
1 error

theosotr avatar Oct 24 '25 13:10 theosotr