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 @@ -100,6 +100,21 @@ public static Type restoreExplicitNullabilityAnnotations(
.visit(newType, origType);
}

/**
* Returns a copy of {@code type} with {@code wildcard} as its backing wildcard.
*
* <p>The copy is necessary because javac capture types can be shared across attributed types.
*/
public static Type.CapturedType replaceCapturedTypeWildcard(
Comment on lines +103 to +108

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 | 🔵 Trivial | 💤 Low value

Add @param/@return tags for consistency.

Every other public method in this file documents @param and @return (see asSuper, memberType, removeNullableAnnotation). Add the same tags here for consistency.

📝 Proposed Javadoc update
   /**
    * Returns a copy of {`@code` type} with {`@code` wildcard} as its backing wildcard.
    *
    * <p>The copy is necessary because javac capture types can be shared across attributed types.
+   *
+   * `@param` type the captured type to copy
+   * `@param` wildcard the wildcard to use as the backing wildcard of the copy
+   * `@return` the copy of {`@code` type} with the updated backing wildcard
    */
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/**
* Returns a copy of {@code type} with {@code wildcard} as its backing wildcard.
*
* <p>The copy is necessary because javac capture types can be shared across attributed types.
*/
public static Type.CapturedType replaceCapturedTypeWildcard(
/**
* Returns a copy of {`@code` type} with {`@code` wildcard} as its backing wildcard.
*
* <p>The copy is necessary because javac capture types can be shared across attributed types.
*
* `@param` type the captured type to copy
* `@param` wildcard the wildcard to use as the backing wildcard of the copy
* `@return` the copy of {`@code` type} with the updated backing wildcard
*/
public static Type.CapturedType replaceCapturedTypeWildcard(
🤖 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/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java`
around lines 103 - 108, Update the Javadoc for replaceCapturedTypeWildcard to
include `@param` tags describing type and wildcard, and an `@return` tag describing
the copied CapturedType result, matching the documentation style of the other
public methods in TypeSubstitutionUtils.

Type.CapturedType type, Type.WildcardType wildcard) {
Type.CapturedType updated =
(Type.CapturedType)
TYPE_METADATA_BUILDER.cloneTypeWithMetadata(
type, TYPE_METADATA_BUILDER.create(type.getAnnotationMirrors()));
updated.wildcard = wildcard;
return updated;
}

/**
* Updates a type {@code typeToUpdate} by applying inferred nullability for type variables. The
* update proceeds in three steps:
Expand Down Expand Up @@ -339,9 +354,27 @@ public Type visitTypeVar(Type.TypeVar t, Type other) {
return updateDirectNullabilityAnnotationsForType(t, other);
}

/**
* Restores annotations on both the captured type {@code t} and its backing wildcard.
*
* <p>The corresponding type {@code other} may be an ordinary wildcard because javac can
* capture-convert {@code t} without capture-converting {@code other}. In such cases, the
* annotation on the bound of {@code other} should be restored to the bound of the wildcard
* corresponding to {@code t}.
*/
@Override
public Type visitCapturedType(Type.CapturedType t, Type other) {
return updateDirectNullabilityAnnotationsForType(t, other);
Type updated = updateDirectNullabilityAnnotationsForType(t, other);
Type.WildcardType otherWildcard = GenericsUtils.asWildcard(other);
if (otherWildcard == null) {
return updated;
}
Type.WildcardType updatedWildcard =
(Type.WildcardType) t.wildcard.accept(this, otherWildcard);
if (updatedWildcard == t.wildcard) {
return updated;
}
return replaceCapturedTypeWildcard((Type.CapturedType) updated, updatedWildcard);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.uber.nullaway.generics.TypeMetadataBuilder.TYPE_METADATA_BUILDER;

import com.google.common.base.Verify;
import com.google.common.collect.ImmutableList;
import com.sun.tools.javac.code.BoundKind;
import com.sun.tools.javac.code.Type;
Expand Down Expand Up @@ -99,11 +100,11 @@ public Type visitWildcardType(Type.WildcardType t, Integer pathIndex) {
if (entry.kind() != NestedAnnotationInfo.TypePathEntry.Kind.WILDCARD_BOUND) {
return t;
}
int boundIndex = entry.index();
if (t.type == null) {
if (t.kind == BoundKind.UNBOUND) {
// TODO we need to add logic to _introduce_ a bound if none exists (add follow-up issue)
return t;
}
int boundIndex = entry.index();
if (boundIndex == 0 && t.kind == BoundKind.EXTENDS) {
Type newBound = t.type.accept(this, pathIndex + 1);
return newBound == t.type ? t : TYPE_METADATA_BUILDER.createWildcardType(t, newBound);
Expand All @@ -115,6 +116,34 @@ public Type visitWildcardType(Type.WildcardType t, Integer pathIndex) {
return t;
}

/**
* Updates a captured type while preserving the backing wildcard used by NullAway's wildcard-bound
* reasoning.
*
* <p>javac represents a captured wildcard as a type variable plus its original wildcard. A direct
* annotation on the capture is insufficient because effective-bound computations unwrap the
* backing wildcard, so updates must be reflected there.
*/
@Override
public Type visitCapturedType(Type.CapturedType t, Integer pathIndex) {
Type.WildcardType updatedWildcard;
if (pathIndex < typePath.size()) {
updatedWildcard = (Type.WildcardType) t.wildcard.accept(this, pathIndex);
} else {
Verify.verify(pathIndex == typePath.size(), "path index out of bounds");
if (t.wildcard.kind == BoundKind.UNBOUND) {
// Do not turn javac's placeholder bound for an unbounded wildcard into an explicit bound.
return t;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Type updatedBound = TypeSubstitutionUtils.typeWithAnnot(t.wildcard.type, annotationType);
updatedWildcard = TYPE_METADATA_BUILDER.createWildcardType(t.wildcard, updatedBound);
}
if (updatedWildcard == t.wildcard) {
return t;
}
return TypeSubstitutionUtils.replaceCapturedTypeWildcard(t, updatedWildcard);
}

@Override
public Type visitType(Type t, Integer pathIndex) {
if (pathIndex == typePath.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
/* @NullMarked */
@SuppressWarnings("DoNotCallSuggester")
public class NestedAnnots<T /* extends @Nullable Object */> {
public NestedAnnots<T> self() {
return this;
}

public static <T /* extends @Nullable Object */> NestedAnnots<T> genericMethod(
Class</* @NonNull */ T> clazz) {
return new NestedAnnots<>();
Expand All @@ -22,6 +26,10 @@ public static void wildcardUpper(NestedAnnots<? extends /* @NonNull */ String> t

public static void wildcardLower(NestedAnnots<? super /* @Nullable */ String> t) {}

public NestedAnnots<? extends /* @Nullable */ T> wildcardUpperTypeVariable() {
throw new RuntimeException();
}

public static void multipleArgs(
NestedAnnots<String> t1, NestedAnnots</* @Nullable */ Integer> t2) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ public ImmutableSetMultimap<MethodRef, Integer> methodTypeVariablesWithNullableU
ImmutableList.of(
new TypePathEntry(TYPE_ARGUMENT, 0),
new TypePathEntry(WILDCARD_BOUND, 1)))))
.put(
methodRef("com.uber.lib.unannotated.NestedAnnots", "wildcardUpperTypeVariable()"),
ImmutableSetMultimap.of(
-1,
new NestedAnnotationInfo(
Annotation.NULLABLE,
ImmutableList.of(
new TypePathEntry(TYPE_ARGUMENT, 0),
new TypePathEntry(WILDCARD_BOUND, 0)))))
.put(
methodRef(
"com.uber.lib.unannotated.NestedAnnots",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,34 @@ void testLower(NestedAnnots<String> t) {
.doTest();
}

@Test
public void nestedWildcardWithCapturedTypeVariableBound() {
makeLibraryModelsTestHelperWithArgs(
JSpecifyJavacConfig.withJSpecifyModeArgs(
Arrays.asList(
"-d",
temporaryFolder.getRoot().getAbsolutePath(),
"-XepOpt:NullAway:OnlyNullMarked=true")))
.addSourceLines(
"Test.java",
"""
import com.uber.lib.unannotated.NestedAnnots;
import org.jspecify.annotations.*;
@NullMarked
public class Test {
NestedAnnots<? extends String> test(
NestedAnnots<? extends String> receiver) {
// should reject since return type of wildcardUpperTypeVariable
// is modeled to be NestedAnnots<? extends @Nullable T>, which
// here is incompatible with the return type NestedAnnots<? extends String>
// BUG: Diagnostic contains: incompatible types
return receiver.self().wildcardUpperTypeVariable();
}
}
""")
.doTest();
}
Comment on lines +572 to +598

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 to the new test method.

nestedWildcardWithCapturedTypeVariableBound configures a JSpecify compilation test and verifies a capture-conversion diagnostic. Document this behavior.

As per coding guidelines, “Add Javadoc to every non-trivial method, including private methods.”

Proposed Javadoc
+  /**
+   * Verifies that a modeled nullable wildcard bound is incompatible after capture conversion.
+   */
   `@Test`
   public void nestedWildcardWithCapturedTypeVariableBound() {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@Test
public void nestedWildcardWithCapturedTypeVariableBound() {
makeLibraryModelsTestHelperWithArgs(
JSpecifyJavacConfig.withJSpecifyModeArgs(
Arrays.asList(
"-d",
temporaryFolder.getRoot().getAbsolutePath(),
"-XepOpt:NullAway:OnlyNullMarked=true")))
.addSourceLines(
"Test.java",
"""
import com.uber.lib.unannotated.NestedAnnots;
import org.jspecify.annotations.*;
@NullMarked
public class Test {
NestedAnnots<? extends String> test(
NestedAnnots<? extends String> receiver) {
// should reject since return type of wildcardUpperTypeVariable
// is modeled to be NestedAnnots<? extends @Nullable T>, which
// here is incompatible with the return type NestedAnnots<? extends String>
// BUG: Diagnostic contains: incompatible types
return receiver.self().wildcardUpperTypeVariable();
}
}
""")
.doTest();
}
/**
* Verifies that a modeled nullable wildcard bound is incompatible after capture conversion.
*/
`@Test`
public void nestedWildcardWithCapturedTypeVariableBound() {
makeLibraryModelsTestHelperWithArgs(
JSpecifyJavacConfig.withJSpecifyModeArgs(
Arrays.asList(
"-d",
temporaryFolder.getRoot().getAbsolutePath(),
"-XepOpt:NullAway:OnlyNullMarked=true")))
.addSourceLines(
"Test.java",
"""
import com.uber.lib.unannotated.NestedAnnots;
import org.jspecify.annotations.*;
`@NullMarked`
public class Test {
NestedAnnots<? extends String> test(
NestedAnnots<? extends String> receiver) {
// should reject since return type of wildcardUpperTypeVariable
// is modeled to be NestedAnnots<? extends `@Nullable` T>, which
// here is incompatible with the return type NestedAnnots<? extends String>
// BUG: Diagnostic contains: incompatible types
return receiver.self().wildcardUpperTypeVariable();
}
}
""")
.doTest();
}
🤖 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
`@test-library-models/src/test/java/com/uber/nullaway/CustomLibraryModelsTests.java`
around lines 572 - 598, Add Javadoc to the non-trivial test method
nestedWildcardWithCapturedTypeVariableBound describing that it verifies the
expected incompatibility diagnostic from capture conversion of the nested
wildcard type under JSpecify nullness checking.

Source: Coding guidelines


@Test
public void multipleArgs() {
makeLibraryModelsTestHelperWithArgs(
Expand Down
Loading