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 @@ -89,16 +89,19 @@ public Boolean visitClassType(Type.ClassType lhsType, Type rhsType) {
}
List<Type> lhsTypeArguments = lhsType.getTypeArguments();
List<Type> rhsTypeArguments = rhsTypeAsSuper.getTypeArguments();
List<Type> correspondingTypeVariables = lhsType.tsym.type.getTypeArguments();
// This is impossible, considering the fact that standard Java subtyping succeeds before
// running NullAway
if (lhsTypeArguments.size() != rhsTypeArguments.size()) {
if (lhsTypeArguments.size() != rhsTypeArguments.size()
|| lhsTypeArguments.size() != correspondingTypeVariables.size()) {
throw new RuntimeException(
"Number of types arguments in " + rhsTypeAsSuper + " does not match " + lhsType);
}
for (int i = 0; i < lhsTypeArguments.size(); i++) {
Type lhsTypeArgument = lhsTypeArguments.get(i);
Type rhsTypeArgument = rhsTypeArguments.get(i);
if (!typeArgumentContainedBy(lhsTypeArgument, rhsTypeArgument)) {
Type.TypeVar correspondingTypeVariable = (Type.TypeVar) correspondingTypeVariables.get(i);
if (!typeArgumentContainedBy(lhsTypeArgument, rhsTypeArgument, correspondingTypeVariable)) {
return false;
}
}
Expand Down Expand Up @@ -142,8 +145,17 @@ public Boolean visitType(Type t, Type type) {
* <a href="https://jspecify.dev/docs/spec/#subtyping">JSpecify's nullability-aware subtype
* relation</a>. Non-wildcard pairs require matching nullability annotations and recursively
* matching nested type arguments. Wildcard formals are delegated to {@link #wildcardContains}.
*
* @param lhsTypeArgument the formal type argument on the left
* @param rhsTypeArgument the actual type argument on the right whose containment is checked
* @param correspondingTypeVariable the formal type variable for this type-argument position, used
* in wildcard containment checks so they can compute effective wildcard bounds when javac has
* not stored the corresponding formal type variable on the wildcard itself (see <a
* href="https://github.com/uber/NullAway/issues/1732">#1732</a>)
* @return whether {@code rhsTypeArgument} is contained by {@code lhsTypeArgument}
*/
private boolean typeArgumentContainedBy(Type lhsTypeArgument, Type rhsTypeArgument) {
private boolean typeArgumentContainedBy(
Type lhsTypeArgument, Type rhsTypeArgument, Type.TypeVar correspondingTypeVariable) {
if (!config.handleWildcardGenerics()) {
if (lhsTypeArgument.getKind().equals(TypeKind.WILDCARD)
|| rhsTypeArgument.getKind().equals(TypeKind.WILDCARD)) {
Expand All @@ -159,7 +171,7 @@ private boolean typeArgumentContainedBy(Type lhsTypeArgument, Type rhsTypeArgume
lhsTypeArgument instanceof Type.WildcardType wildcardType ? wildcardType : null;
Type.WildcardType rhsWildcard = GenericsUtils.asWildcard(rhsTypeArgument);
if (lhsWildcard != null) {
return wildcardContains(lhsWildcard, rhsTypeArgument);
return wildcardContains(lhsWildcard, rhsTypeArgument, correspondingTypeVariable);
}
if (rhsWildcard != null) {
// This case should only arise when generic method invocation inference / capture conversion
Expand Down Expand Up @@ -194,8 +206,17 @@ private boolean haveIdenticalNullability(Type lhsType, Type rhsType) {
* formal {@code ? super S} contains concrete actuals {@code T} and wildcard actuals {@code ?
* super T} when {@code S <: T}; and a formal {@code ?} is treated as {@code ? extends B}, where
* {@code B} is the corresponding type variable's upper bound.
*
* @param lhsWildcard the formal wildcard type argument on the left
* @param rhsTypeArgument the actual type argument on the right whose containment is checked
* @param correspondingTypeVariable the formal type variable for this type-argument position, used
* to compute effective wildcard bounds when javac has not stored the corresponding formal
* type variable on the wildcard itself (see <a
* href="https://github.com/uber/NullAway/issues/1732">#1732</a>)
* @return whether {@code lhsWildcard} contains {@code rhsTypeArgument}
*/
private boolean wildcardContains(Type.WildcardType lhsWildcard, Type rhsTypeArgument) {
private boolean wildcardContains(
Type.WildcardType lhsWildcard, Type rhsTypeArgument, Type.TypeVar correspondingTypeVariable) {
Set<Type> activeRhsArguments = activeWildcardComparisons.get(lhsWildcard);
if (activeRhsArguments == null) {
activeRhsArguments = Collections.newSetFromMap(new IdentityHashMap<>());
Expand All @@ -210,8 +231,10 @@ private boolean wildcardContains(Type.WildcardType lhsWildcard, Type rhsTypeArgu
return switch (lhsWildcard.kind) {
case UNBOUND, EXTENDS ->
extendsBoundContains(
GenericsUtils.wildcardUpperBound(lhsWildcard, state, config, handler),
rhsTypeArgument);
GenericsUtils.wildcardUpperBound(
lhsWildcard, correspondingTypeVariable, state, config, handler),
rhsTypeArgument,
correspondingTypeVariable);
case SUPER -> superWildcardContains(lhsWildcard, rhsTypeArgument);
};
} finally {
Expand All @@ -226,11 +249,23 @@ private boolean wildcardContains(Type.WildcardType lhsWildcard, Type rhsTypeArgu
* Returns whether a formal {@code ? extends S} contains the actual type argument on the right.
* For concrete actuals {@code T}, wildcard actuals {@code ? extends T}, and non-extends wildcard
* actuals whose effective upper bound is {@code T}, containment holds when {@code T <: S}.
*
* @param lhsBound the effective upper bound {@code S} of the formal wildcard on the left
* @param rhsTypeArgument the actual type argument on the right whose containment is checked
* @param correspondingTypeVariable the formal type variable for this type-argument position, used
* to compute the effective upper bound of a wildcard actual when javac has not stored the
* corresponding formal type variable on the wildcard itself (see <a
* href="https://github.com/uber/NullAway/issues/1732">#1732</a>)
* @return whether a formal wildcard whose effective upper bound is {@code lhsBound} contains
* {@code rhsTypeArgument}
*/
private boolean extendsBoundContains(Type lhsBound, Type rhsTypeArgument) {
private boolean extendsBoundContains(
Type lhsBound, Type rhsTypeArgument, Type.TypeVar correspondingTypeVariable) {
Type.WildcardType rhsWildcard = GenericsUtils.asWildcard(rhsTypeArgument);
if (rhsWildcard != null) {
Type rhsUpperBound = GenericsUtils.wildcardUpperBound(rhsWildcard, state, config, handler);
Type rhsUpperBound =
GenericsUtils.wildcardUpperBound(
rhsWildcard, correspondingTypeVariable, state, config, handler);
return typeArgumentSubtype(lhsBound, rhsUpperBound);
}
return typeArgumentSubtype(lhsBound, rhsTypeArgument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ static Type effectiveWildcardUpperBound(
*/
static Type wildcardUpperBound(
WildcardType wildcardType, VisitorState state, Config config, Handler handler) {
return wildcardUpperBound(wildcardType, wildcardType.bound, state, config, handler);
}

/**
* Returns the effective upper bound of a wildcard, using {@code correspondingTypeVariable} when
* javac has not stored one on the wildcard itself.
*
* <p>Before JDK 23, javac does not associate wildcard type arguments read from classfiles with
* their corresponding formal type variables. The {@code correspondingTypeVariable} parameter
* allows the caller to provide that information, when available (see <a
* href="https://github.com/uber/NullAway/issues/1732">#1732</a>).
*/
static Type wildcardUpperBound(
WildcardType wildcardType,
Type.@Nullable TypeVar correspondingTypeVariable,
VisitorState state,
Config config,
Handler handler) {
Type upperBound;
if (wildcardType.kind == BoundKind.EXTENDS) {
upperBound = wildcardType.getExtendsBound();
Expand All @@ -68,7 +86,8 @@ static Type wildcardUpperBound(
// passed (confusingly stored in the `bound` field). E.g., if we have class Foo<T extends
// @Nullable Object>, and then see Foo<? super String>, we use @Nullable Object as the upper
// bound. If not present, default to Object.
Type.TypeVar formalTypeVar = wildcardType.bound;
Type.TypeVar formalTypeVar =
wildcardType.bound != null ? wildcardType.bound : correspondingTypeVariable;
upperBound =
formalTypeVar == null
? Symtab.instance(state.context).objectType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,57 @@ void testNonNullContents(List<String> list) {
.doTest();
}

@Test
public void issue1732UnboundedWildcardInBytecode() {
makeHelper()
.addSourceLines(
"Test.java",
"""
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
class Test {
static boolean bulkOperations(
List<String> list,
Collection<@Nullable String> nullableCollection,
Set<@Nullable String> nullableSet) {
return list.containsAll(nullableCollection)
|| list.removeAll(nullableSet)
|| list.retainAll(nullableCollection);
}

static boolean disjoint(
Collection<@Nullable String> first,
Collection<@Nullable String> second) {
return Collections.disjoint(first, second);
}

static boolean ownUnbounded(Collection<?> collection) {
return collection.isEmpty();
}

static boolean callOwnUnbounded(Collection<@Nullable String> collection) {
return ownUnbounded(collection);
}

static boolean ownExtendsObject(Collection<? extends Object> collection) {
return collection.isEmpty();
}

static boolean callOwnExtendsObject(Collection<@Nullable String> collection) {
// BUG: Diagnostic contains: incompatible types
return ownExtendsObject(collection);
}
}
""")
.doTest();
}

@Test
public void listContainingNullsWithoutModel() {
makeTestHelperWithArgs(
Expand Down
Loading