Repository navigation
Description
Kind of problem
False negative (NullAway misses a nullness error)
Minimal reproducer
A T whose bound is @Nullable Object is passed to a generic method whose U excludes null, directly (take1) and as the type argument of an invariant List<U> (take2). javac infers U := T for both calls, and the program throws NullPointerException inside the callee with no diagnostic anywhere.
import java.util.List;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
class Test {
static <U> void take1(U x) { x.toString(); }
static <T extends @Nullable Object> void f1(T x) {
take1(x);
}
static <U> void take2(List<U> xs) { xs.get(0).toString(); }
static <T extends @Nullable Object> void f2(List<T> xs) {
take2(xs);
}
public static void main(String[] args) {
try { Test.<@Nullable String>f1(null); } catch (NullPointerException e) { System.out.println("f1: " + e); }
java.util.List<@Nullable String> l = new java.util.ArrayList<>(); l.add(null);
try { Test.<@Nullable String>f2(l); } catch (NullPointerException e) { System.out.println("f2: " + e); }
}
}Actual behavior
no error reported
Running main with javac -cp jspecify-1.0.0.jar -d . Test.java && java -cp .:jspecify-1.0.0.jar Test:
f1: java.lang.NullPointerException: Cannot invoke "Object.toString()" because "<parameter1>" is null
f2: java.lang.NullPointerException: Cannot invoke "Object.toString()" because the return value of "java.util.List.get(int)" is null
Expected behavior
A diagnostic on take1(x) and on take2(xs), since the type argument inferred for U, T, admits null and U's bound does not. No diagnostic on the two calls in main: @Nullable String is within the bound of T.
The same instantiation written out is already a JSpecify mismatch: samples/OutOfBoundsTypeVariable.java marks Lib<F> with F extends @Nullable Foo and interface Lib<T>, and #1946 tracks the explicit forms NonNullParameter<T> and Test.<T>m(). Here javac writes the same type argument by inference.
The wording is the maintainers' to choose. #1955 reports the ? extends U form of this call, take3(List<? extends U> xs) with a List<T>, as inference failure: type variable U of Test.take3 is constrained to be @Nullable, but its upper bound requires it to be @NonNull; the type argument T passed for ? extends U admits null, since T of Test.f3 has a @Nullable upper bound.
NullAway configuration
-XepOpt:NullAway:OnlyNullMarked=true
-XepOpt:NullAway:JSpecifyMode=true
-XepOpt:NullAway:JSpecifyExperimental=true
-XDaddTypeAnnotationsToSymbol=true
NullAway version
master at af5ba08 (0.14.5-SNAPSHOT), run as a CompilationTestHelper test with JSpecifyJavacConfig.withJSpecifyModeArgs. The branch of #1834 at 6860631, which carries #1955 rebased onto that master, reports neither call either.
Error Prone version
2.53.0
JDK version
21.0.9
Additional context
Raised by @msridhar in a comment on #1955, which reports only the ? extends U form. Related: #1946 (the same check for an explicit type argument), #1932 (constraint solver rewrite), #1585 (inference of full types).
Analysis, not checked by a test; the report stands without it. The two calls likely need different fixes. For take2, U := T exactly, so checking the inferred type argument against U's bound, as #1946 asks for an explicit one, would report it. For take1, a check based only on T's declared bound would also report if (x != null) take1(x), because dataflow reads a T value as non-null whether or not it was checked (#1727, and #2027 for the dereference of such a value); avoiding that false positive needs dataflow to tell a checked T from an unchecked one.
Assisted-by: Claude Code (claude-opus-5-5)
Activity