Repository navigation
Description
With JSpecify mode on, writing NonNullParameter<T> where NonNullParameter<X> declares no @Nullable bound and T is declared T extends @Nullable Object is accepted, as a parameter type, as new NonNullParameter<T>(t), and as the explicit type argument Test.<T>nonNullIdentity(t). The same declaration written NonNullParameter<@Nullable String> is reported. The caller below instantiates T as @Nullable String, which NullAway allows, and throws NullPointerException inside NonNullParameter.hash() with no diagnostic anywhere.
Versions: NullAway master at 2b25d0f (0.14.3-SNAPSHOT), the 0.14.2 release, and the head of #1834 (b84ffe7). 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 org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
class Test {
static class NonNullParameter<X> {
X x;
NonNullParameter(X x) { this.x = x; }
int hash() { return x.hashCode(); }
}
static <U> U nonNullIdentity(U u) { return u; }
static <T extends @Nullable Object> int wrap(T t) {
return new NonNullParameter<T>(t).hash();
}
static <T extends @Nullable Object> void parameter(NonNullParameter<T> p, Class<T> c) {}
static <T extends @Nullable Object> void explicitTypeArgument(T t) {
Test.<T>nonNullIdentity(t);
}
static void written(NonNullParameter<@Nullable String> p) {}
static int caller() {
return Test.<@Nullable String>wrap(null);
}
}Expected: a diagnostic on new NonNullParameter<T>(t) (line 12), on both parameters of parameter (line 14), and on Test.<T>nonNullIdentity(t) (line 16), as there is one on line 18 today:
Generic type parameter cannot be @Nullable, as type variable X of type Test.NonNullParameter does not have a @Nullable upper bound
The wording is one possible shape: the argument on lines 12, 14, and 16 is not annotated @Nullable, it is a type variable whose declared bound admits null, and the message may want to say so. The requirement is the report. JSpecify's conformance sample samples/OutOfBoundsTypeVariable.java is this case: Lib<F> with F extends @Nullable Foo and interface Lib<T> is marked jspecify_nullness_mismatch. NullAway already applies the rule to a written @Nullable argument (line 18, GenericsChecks.checkInstantiationForParameterizedTypedTree, and checkGenericMethodCallTypeArguments for an explicit type argument at a call), and the Class<T> on line 14 is the same case against the JDK: Class<T> has no @Nullable bound, and a Class<@Nullable String> is refused today.
Actual: no diagnostic on lines 12, 14, and 16, on master, on 0.14.2, and with #1834 applied; line 18 is reported on all three. With a // BUG: Diagnostic contains: Generic type parameter cannot be @Nullable marker above line 12 and another above line 18, which shift them to 13 and 20, the test fails with:
Did not see an error on line 13 matching Generic type parameter cannot be @Nullable. All errors:
/Test.java:20: warning: [NullAway] Generic type parameter cannot be @Nullable, as type variable X of type Test.NonNullParameter does not have a @Nullable upper bound
caller compiles clean and throws:
java.lang.NullPointerException: Cannot invoke "Object.hashCode()" because "this.x" is null
Analysis, a guess; the report stands without it. Both checks collect only the type-argument trees that carry a @Nullable annotation (nullableTypeArguments in checkInstantiationForParameterizedTypedTree and in checkGenericMethodCallTypeArguments) and compare those against the type parameter's bound, so a type-variable argument is never examined. The question the checks need to ask of such an argument, whether a value of it may be null under some instantiation, is the one #1834 answers for a wildcard requirement with isNullExclusive in CheckIdenticalNullabilityVisitor; the same predicate applied to the argument against the parameter's bound would report lines 12, 14, and 16 and keep accepting NonNullParameter<T> for <T> and <T extends @NonNull Object>.
This is why #1834 compares a type-variable actual against an unbounded ? as written: other.isAssignableFrom(type) with a Class<T> whose T admits null would otherwise be reported at the call, while the Class<T> parameter that makes it ill-formed is not reported where it is written. Once the declaration is reported, that exception in the containment check can go. I did not survey how often Class<T> with a nullable-bounded T occurs in annotated code; a Class of a type that can be null is a type that does not exist, so each report should be a real mistake, but the count is for the maintainers to weigh.
Related: #1834 (the use-site check and its ? exception), #1836 (the same two checks read an intersection bound wrongly), #1721 (closed; the inference-failure message for a @NonNull bound).
Assisted-by: Claude Code (claude-fable-5-1)
Activity
Report a type variable whose bound admits null where a non-null wildc…
Report a type variable whose bound admits null where a non-null wildc…
Report a type variable whose bound admits null where a non-null wildc…