From 142ba549b83ca2c1afcf0037c72d7ecabcd176ac Mon Sep 17 00:00:00 2001 From: Manu Sridharan Date: Sat, 22 Aug 2026 19:09:07 -0700 Subject: [PATCH] Fix for restoring nullness annotations from unbounded wildcard to captured type --- .../generics/TypeSubstitutionUtils.java | 18 +++++++++- .../uber/nullaway/jspecify/WildcardTests.java | 34 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java b/nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java index 82bfc19ceb..c26667730a 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java @@ -448,7 +448,23 @@ public Type visitCapturedType(Type.CapturedType t, Type other) { Type.WildcardType otherWildcard = GenericsUtils.asWildcard(other); Type.WildcardType updatedWildcard; if (otherWildcard != null) { - updatedWildcard = (Type.WildcardType) t.wildcard.accept(this, otherWildcard); + if (t.wildcard.kind == BoundKind.EXTENDS && otherWildcard.kind == BoundKind.UNBOUND) { + // Substitution can turn the capture's backing wildcard into an explicit extends + // wildcard while the corresponding declared wildcard remains unbounded. Its `type` + // field is just an Object placeholder; annotations must be restored from the implicit + // upper bound of its formal type variable instead. + Type.TypeVar formalTypeVariable = otherWildcard.bound; + if (formalTypeVariable == null) { + return updated; + } + Type updatedBound = t.wildcard.type.accept(this, formalTypeVariable.getUpperBound()); + if (updatedBound == t.wildcard.type) { + return updated; + } + updatedWildcard = TYPE_METADATA_BUILDER.createWildcardType(t.wildcard, updatedBound); + } else { + updatedWildcard = (Type.WildcardType) t.wildcard.accept(this, otherWildcard); + } } else if (t.wildcard.kind == BoundKind.EXTENDS) { Type updatedBound = t.wildcard.type.accept(this, other); if (updatedBound == t.wildcard.type) { diff --git a/nullaway/src/test/java/com/uber/nullaway/jspecify/WildcardTests.java b/nullaway/src/test/java/com/uber/nullaway/jspecify/WildcardTests.java index 1f6b7c2693..91243f3287 100644 --- a/nullaway/src/test/java/com/uber/nullaway/jspecify/WildcardTests.java +++ b/nullaway/src/test/java/com/uber/nullaway/jspecify/WildcardTests.java @@ -549,6 +549,40 @@ void test(Foo f) { .doTest(); } + @Test + public void issue1715() { + makeHelper() + .addSourceLines( + "Test.java", + """ + import org.jspecify.annotations.NullMarked; + import org.jspecify.annotations.Nullable; + @NullMarked + class Test { + interface I1 {} + interface I2> { + Y get(); + } + I1 crash(I2 i2) { + var i2var = i2; + return i2var.get(); + } + + interface NullableI1 {} + interface NullableI2< + X extends @Nullable Object, Y extends NullableI1<@Nullable X>> { + Y get(); + } + NullableI1 incompatible(NullableI2 i2) { + var i2var = i2; + // BUG: Diagnostic contains: incompatible types + return i2var.get(); + } + } + """) + .doTest(); + } + @Test public void wildcardCaptureLocals() { makeHelper()