Repository navigation
Description
With JSpecify mode on, dereferencing an element of a generic call's result is not reported when the call's type variable U is inferred as @Nullable and the element sits in an array component (U[]) or in a type argument inside a wildcard bound (List<? extends List<U>>). Below, arrayOf(null, "x")[0].toString() and wildcardOfLists(null, "x").get(0).get(0).toString() dereference a null with no diagnostic, while the same return types written with an explicit @Nullable are reported, and so is List<List<U>> inferred the same way.
Versions: NullAway master at 2b25d0f (0.14.3-SNAPSHOT), the 0.14.2 release, and the head of #1834 (ec072b0). Error Prone 2.50.0, JDK 21.0.9. Flags: -XepOpt:NullAway:JSpecifyMode=true -XDaddTypeAnnotationsToSymbol=true -XepOpt:NullAway:JSpecifyExperimental=true -XepOpt:NullAway:OnlyNullMarked=true.
Code sample. I ran it as a test in nullaway/src/test/java/com/uber/nullaway/jspecify/, with the helper WildcardTests uses: makeTestHelperWithArgs(JSpecifyJavacConfig.withJSpecifyModeArgs(Arrays.asList("-XepOpt:NullAway:OnlyNullMarked=true"))).
import java.util.List;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
class Test {
static <U extends @Nullable Object> U[] arrayOf(U a, U b) {
throw new UnsupportedOperationException();
}
static <U extends @Nullable Object> List<? extends List<U>> wildcardOfLists(U a, U b) {
throw new UnsupportedOperationException();
}
static @Nullable Object[] explicitArray() {
throw new UnsupportedOperationException();
}
static List<? extends List<@Nullable Object>> explicitWildcardOfLists() {
throw new UnsupportedOperationException();
}
void inferred() {
// BUG: Diagnostic contains: dereferenced expression 'arrayOf(null, "x")[0]' is @Nullable
arrayOf(null, "x")[0].toString();
// BUG: Diagnostic contains: dereferenced expression 'wildcardOfLists(null, "x").get(0).get(0)' is @Nullable
wildcardOfLists(null, "x").get(0).get(0).toString();
}
void explicit() {
// BUG: Diagnostic contains: dereferenced expression 'explicitArray()[0]' is @Nullable
explicitArray()[0].toString();
// BUG: Diagnostic contains: dereferenced expression 'explicitWildcardOfLists().get(0).get(0)' is @Nullable
explicitWildcardOfLists().get(0).get(0).toString();
}
}Expected: all four // BUG lines report the message their marker names, for example dereferenced expression 'arrayOf(null, "x")[0]' is @Nullable on line 20. The null argument makes NullAway infer U as @Nullable, so the two inferred calls return @Nullable Object[] and List<? extends List<@Nullable Object>>, the types that explicitArray and explicitWildcardOfLists declare, and NullAway reports dereferences of those.
Actual: only the explicit lines, 26 and 28, are reported, on master, on 0.14.2, and with #1834 applied. The test fails with:
Did not see an error on line 20 matching dereferenced expression 'arrayOf(null, "x")[0]' is @Nullable. All errors:
/Test.java:26: warning: [NullAway] dereferenced expression 'explicitArray()[0]' is @Nullable
explicitArray()[0].toString();
^
(see http://t.uber.com/nullaway )
/Test.java:28: warning: [NullAway] dereferenced expression 'explicitWildcardOfLists().get(0).get(0)' is @Nullable
explicitWildcardOfLists().get(0).get(0).toString();
^
(see http://t.uber.com/nullaway )
In the same setup, static <U extends @Nullable Object> List<List<U>> listOfLists(U a, U b) called as listOfLists(null, "x").get(0).get(0).toString() is reported on master (dereferenced expression 'listOfLists(null, "x").get(0).get(0)' is @Nullable), so a type argument nested in an invariant one keeps the inferred nullness; the array component and the type argument inside the wildcard bound do not. The two are separate positions and may well need separate fixes.
With #1834 applied, a class-type result that a T extends @Nullable Object reaches, as in arrayOf(t, "x") where javac infers Object, is also inferred @Nullable, and these two positions lose it the same way: listOf(t, "x").get(0).toString() is reported there, arrayOf(t, "x")[0].toString(), for (Object o : arrayOf(t, "x")) o.toString(); and wildcardOfLists(t, "x").get(0).get(0).toString() are not.
Analysis, a guess I have not verified; the report stands without it. Since the declared forms are reported, the loss looks like it is where the inferred nullness is applied to the call's type, TypeSubstitutionUtils.updateTypeWithInferredNullability, which copies annotations from the substituted declared type onto javac's type and may not carry them into an array's component type or into the type arguments of a wildcard's bound.
Related: #1947 is a List<T> that loses its T through an intermediate generic call; here there is one call and the loss is in a position of its result. #1919 (closed) was nested nullness lost when a lambda returns its implicit parameter.
Assisted-by: Claude Code (claude-opus-5-5)
Activity