Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.uber.nullaway.handlers.Handler;
import java.util.List;
import javax.lang.model.type.NullType;
import javax.lang.model.type.TypeKind;

/**
* Visitor that checks for identical nullability annotations at all nesting levels within two types.
Expand All @@ -37,7 +36,7 @@ public Boolean visitClassType(Type.ClassType lhsType, Type rhsType) {
if (rhsType instanceof NullType || rhsType.isPrimitive()) {
return true;
}
if (rhsType.getKind().equals(TypeKind.WILDCARD)) {
if (GenericsUtils.asWildcard(rhsType) != null) {
// TODO Handle wildcard types
return true;
}
Expand Down Expand Up @@ -120,16 +119,21 @@ public Boolean visitType(Type t, Type type) {
* matching nested type arguments. Wildcard formals are delegated to {@link #wildcardContains}.
*/
private boolean typeArgumentContainedBy(Type lhsTypeArgument, Type rhsTypeArgument) {
if (!config.handleWildcardGenerics()
&& (lhsTypeArgument.getKind().equals(TypeKind.WILDCARD)
|| rhsTypeArgument.getKind().equals(TypeKind.WILDCARD))) {
// Do not use GenericsUtils.asWildcard() for the LHS. A captured LHS is a type variable, not a
// wildcard formal; unwrapping it can repeatedly expand recursive upper bounds (for example,
// F-bounded type parameters) as containment delegates back into subtype checking. See test
// com.uber.nullaway.jspecify.WildcardTests.capturedLhsWithFBoundedTypeParametersDoesNotRecurse
Type.WildcardType lhsWildcard =
lhsTypeArgument instanceof Type.WildcardType wildcardType ? wildcardType : null;
Type.WildcardType rhsWildcard = GenericsUtils.asWildcard(rhsTypeArgument);
if (!config.handleWildcardGenerics() && (lhsWildcard != null || rhsWildcard != null)) {
// Preserve the pre-flag behavior of skipping wildcard-aware checks entirely.
return true;
}
if (lhsTypeArgument instanceof Type.WildcardType lhsWildcard) {
if (lhsWildcard != null) {
return wildcardContains(lhsWildcard, rhsTypeArgument);
}
if (rhsTypeArgument.getKind().equals(TypeKind.WILDCARD)) {
if (rhsWildcard != null) {
// This case should only arise when generic method invocation inference / capture conversion
// lets a wildcard actual argument flow into a non-wildcard formal type argument, e.g.,
// passing Foo<? extends T> to <U> void m(Foo<U>). We do not yet support wildcard inference.
Expand Down Expand Up @@ -186,7 +190,8 @@ private boolean extendsBoundContains(Type lhsBound, Type rhsTypeArgument) {
private boolean superWildcardContains(Type.WildcardType lhsWildcard, Type rhsTypeArgument) {
// caller must ensure that lhsWildcard has a super bound
Type lhsBound = castToNonNull(lhsWildcard.getSuperBound());
if (rhsTypeArgument instanceof Type.WildcardType rhsWildcard) {
Type.WildcardType rhsWildcard = GenericsUtils.asWildcard(rhsTypeArgument);
if (rhsWildcard != null) {
if (rhsWildcard.kind != BoundKind.SUPER) {
// This case cannot occur outside of inference: if the rhs is ? extends T, that is never
// assignable to ? super S, since the rhs could be an arbitrary subtype of T (which may be a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,82 @@ Foo<?> test(Foo<@Nullable Void> foo) {
.doTest();
}

@Test
public void capturedSuperWildcardReturnCheckedForContainment() {
makeHelper()
.addSourceLines(
"Test.java",
"""
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
class Test {
interface Box<T extends @Nullable Object> {}
static <T extends @Nullable Object> Box<T> identity(Box<T> box) {
return box;
}
void test(
Box<? super String> nonNullBoundBox,
Box<? super @Nullable String> nullableBoundBox) {
Box<? super String> ok = identity(nullableBoundBox);
// BUG: Diagnostic contains: incompatible types
Box<? super @Nullable String> bad = identity(nonNullBoundBox);
}
}
""")
.doTest();
}

@Test
public void capturedSuperWildcardReturnCheckedRecursively() {
makeHelper()
.addSourceLines(
"Test.java",
"""
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
class Test {
interface Box<T extends @Nullable Object> {}
interface Nested<T extends @Nullable Object> {}
static <T extends @Nullable Object> Box<T> identity(Box<T> box) {
return box;
}
void test(Box<? super Nested<String>> box) {
// BUG: Diagnostic contains: incompatible types
Box<? super Nested<@Nullable String>> bad = identity(box);
}
}
""")
.doTest();
}

@Test
public void capturedLhsWithFBoundedTypeParametersDoesNotRecurse() {
makeHelper()
.addSourceLines(
"Test.java",
"""
import org.jspecify.annotations.NullMarked;
@NullMarked
class Test {
interface Value<V extends Value<V>> {}
interface Store<S extends Store<S>> {}
interface Transfer<V extends Value<V>, S extends Store<S>> {}
static class Analysis<
V extends Value<V>,
S extends Store<S>,
T extends Transfer<V, S>> {
Analysis(T transfer) {}
}
static Analysis<?, ?, ?> create(Transfer<?, ?> transfer) {
return new Analysis<>(transfer);
}
}
""")
.doTest();
}
Comment on lines +906 to +980

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Add Javadoc for the new regression tests.

Document the direct containment, recursive containment, and F-bounded recursion scenarios in the three new public test methods. As per coding guidelines, “Add Javadoc to every non-trivial method, including private methods.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@nullaway/src/test/java/com/uber/nullaway/jspecify/WildcardTests.java` around
lines 906 - 980, Add concise Javadoc immediately above the three new public test
methods: capturedSuperWildcardReturnCheckedForContainment should document direct
wildcard containment checking, capturedSuperWildcardReturnCheckedRecursively
should document recursive nested containment checking, and
capturedLhsWithFBoundedTypeParametersDoesNotRecurse should document that
F-bounded type parameters must not trigger recursive analysis.

Source: Coding guidelines


@Test
public void nullableOnWildcard() {
makeHelper()
Expand Down
Loading