Repository navigation
Conversation
4c2a1f1 to
1cb64f9
Compare
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Advanced Run ID: 📒 Files selected for processing (1)
Included review availability: Your plan provides up to 8 included reviews per hour; 6 remain after this review. WalkthroughThe change treats an intersection upper bound as nullable only when every component has a direct Suggested reviewers: Priority: ➖ Normal Severity of issue fixed: Medium Merge Risk: ⚪ Minimal · up to Intersection bounds now allow nullable type arguments only when every explicit bound component is nullable, with coverage for source and bytecode cases. No current merge-blocking risk remains. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
1cb64f9 to
6f78fa2
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1836 +/- ##
=========================================
Coverage 87.72% 87.72%
- Complexity 3450 3462 +12
=========================================
Files 110 110
Lines 11470 11488 +18
Branches 2353 2357 +4
=========================================
+ Hits 10062 10078 +16
Misses 650 650
- Partials 758 760 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
A type variable whose upper bound is an intersection, such as `<T extends @nullable Object & @nullable Serializable>`, was read through `upperBound.getAnnotationMirrors()`. javac stores type annotations on the elements of an intersection, and an `IntersectionClassType` cannot carry one at all — `cloneWithMetadata` throws — so that read answered "not `@Nullable`" for every intersection, whatever its elements said. Both directions were wrong. An all-`@Nullable` bound came out null-exclusive, so NullAway stayed silent where it should report: a value read through `Box<?>` for such a `Box`, and an override that narrows or widens the bound. At a generic method call the same read produced a false positive instead, rejecting a legal type argument with Type argument cannot be @nullable, as method <T>box(T)'s type variable T is not @nullable A bound read from a class file went the other way. Before JDK 22 javac drops these annotations from the symbol (JDK-8225377), and NullAway falls back on `Symbol.getRawTypeAttributes()`. That fallback ignored `bound_index` and marked the whole type parameter `@Nullable` when any one bound carried the annotation, so `<T extends @nullable Object & Serializable>` accepted a `@Nullable` type argument it should refuse. JSpecify assigns a nullness operator to each element of an intersection separately and fixes the operator of the whole to `NO_CHANGE`; a type variable has nullness-subtype-establishing edges to all the elements, so one non-null element makes the bound null-exclusive. `GenericsUtils.isNullableAnnotated` states that rule once, and the sites that decide a bound's nullability call it. It reads `getExplicitComponents()`: an intersection whose bounds are all interfaces carries an implicit `Object` supertype the declaration did not write, and counting that one would make every such bound non-null. The raw-attributes fallback now collects the annotated bounds per type parameter and requires all of them. An annotation's type path is relative to the one bound its `bound_index` names, so the index is resolved to that element before `NullabilityUtil.isDirectTypeUseAnnotation` reads the path — otherwise an annotation on a type argument inside the bound, or on the enclosing type of a nested-class bound (`@Nullable Outer.Inner` rather than `Outer.@nullable Inner`), counts as annotating the bound. Assisted-by: Claude Code (claude-opus-5)
6f78fa2 to
75576a0
Compare
msridhar
left a comment
There was a problem hiding this comment.
Again a couple comments on the tests, but I'll hold off on reviewing the main code changes until conflicts are resolved
| * {@code com.uber.lib.generics.NestedNullableInBound}. Before JDK 22, javac drops the annotations | ||
| * on such a bound (<a href="https://bugs.openjdk.org/browse/JDK-8225377">JDK-8225377</a>), and | ||
| * NullAway falls back on the raw type attributes of the class file, which number the bounds of an | ||
| * intersection separately. |
There was a problem hiding this comment.
Since we now strongly require -XDaddTypeAnnotationsToSymbol=true and check for its support, do we need these extensive docs around what happens when the flag isn't passed? Now, if in JSpecify mode and the flag is not passed or is not supported, NullAway will just crash
| } | ||
|
|
||
| @Test | ||
| public void interfaceOnlyBoundWithNonNullElementRefusesNullableTypeArgumentOnGenericMethodCall() { |
There was a problem hiding this comment.
What does "interfaceOnlyBound" mean here?
msridhar
left a comment
There was a problem hiding this comment.
Actually the code logic changes here are mostly straightforward so I went through them. I am wondering, though: should we clean up / remove the "legacy" code for reading type annotations out of raw type attributes before landing this one to simplify it? That would be #1639, though maybe in a more limited sense just for JSpecify mode code if we want. I think that would reduce the need for many of the changes in GenericsChecks, if I'm understanding correctly
| * <p>JSpecify assigns a nullness operator to each element of an intersection type separately, and | ||
| * fixes the operator of the intersection as a whole to {@code NO_CHANGE}; javac stores type | ||
| * annotations on the elements, and an {@code IntersectionClassType} cannot carry one at all. A | ||
| * type variable is null-exclusive as soon as one element of its bound is, since that element | ||
| * still establishes a nullness-subtyping edge. See the <a | ||
| * href="https://jspecify.dev/docs/spec/#intersection-types">JSpecify specification</a>. |
There was a problem hiding this comment.
Nit: I think this should be an inline comment under the type instanceof Type.IntersectionClassType intersectionType case
| * @return {@code true} if the annotation should be treated as applying directly to the top-level | ||
| * type, false otherwise | ||
| */ | ||
| public static boolean isDirectTypeUseAnnotation(Attribute.TypeCompound t, Type type) { |
There was a problem hiding this comment.
These changes give me more motivation to get rid of this fallback code entirely, so we don't need to keep maintaining it.
| com.sun.tools.javac.util.List<Attribute.TypeCompound> rawTypeAttributes = | ||
| tsym.getRawTypeAttributes(); | ||
| if (rawTypeAttributes != null) { | ||
| Map<Integer, Set<Integer>> nullableBoundsPerTypeParam = new LinkedHashMap<>(); |
There was a problem hiding this comment.
Am I correct that we should actually be removing all this code involving rawTypeAttributes now that we have stronger checks for -XDaddTypeAnnotationsToSymbol=true support in JSpecify mode?
…ard is required Previously, a `Box<T>` declared with `T extends @nullable Object` was accepted where a `Box<? extends Object>` was required, so a null could reach a position that rejects one. A wildcard actual in that position was judged by its effective upper bound, but a type variable was judged by its use site, which carries no nullness of its own, so every such assignment, argument, and return passed. No diagnostic marks the defect; the symptom is the missing error. `CheckIdenticalNullabilityVisitor` now compares a type variable that carries no `@Nullable` or `@NonNull` at its use site as its declared upper bound. That bound admits null when it is annotated `@Nullable`, when the variable is declared in `@NullUnmarked` code, or when a library model overrides it. A requirement that names a type variable, `? extends T`, admits whatever `T` is instantiated as, so there the two types are compared as written: `Box<T>` and `Box<S extends T>` stay assignable to `Box<? extends T>`, and `Box<@nullable T>` is still reported. The check then also asks whether the actual may be null where this instantiation of `T` may not: it walks the actual's declared bounds up to `T` and looks for a link annotated or declared `@Nullable`. So `Box<S>` with `S extends @nullable T` passed to `Box<? extends T>` is reported whatever `T`'s own bound, and so is `Box<T>` passed to `Box<? extends @nonnull T>` when `T`'s bound admits null. Where the walk does not reach `T`, as between the type variables of an overriding and an overridden generic method, the declared bounds of the two are compared instead. Substituting `T`'s bound instead was tried: on the requirement side alone it reports Caffeine's `CacheLoader.asyncReload`, where `CompletableFuture.supplyAsync` infers `CompletableFuture<V>` against `CompletableFuture<? extends V>`, and on both sides it silences the `Box<@nullable T>` report. ```text CacheLoader.java:196: error: [NullAway] incompatible types: CompletableFuture<V> cannot be converted to CompletableFuture<? extends V> ``` A captured actual skips the null check, because capture conversion drops type-use annotations. In Caffeine's `CacheLoader.asyncLoadAll`, a `Map<? extends K, ? extends @nonnull V>` returned into a `CompletableFuture<? extends Map<? extends K, ? extends @nonnull V>>` reaches the check as `Map<capture of ? extends K, capture of ? extends V>`, with no `@NonNull` left on `V`. Caller sees: - `incompatible types: Box<T> cannot be converted to Box<? extends Object>` where `T`'s declared bound admits null; accepted before. - `mismatched type parameter nullability` on an override that returns `List<V>` where the overridden method returns `List<? extends @nonnull V>` and `V` may be null; accepted before. Caffeine's `AsyncCacheLoader.bulk` is such an override. Code that compiled clean can need a narrower return type or a suppression. - `mismatched type parameter nullability` on an override of a generic method whose type variable admits null, when the override declares that variable without `@Nullable`, for example `<U> CompletionStage<U> thenApply(Function<? super T, ? extends U> fn)` overriding the unannotated JDK method. It comes beside the existing `Method type variable U has a non-null upper bound` report for the same declaration, because the overridden method's type variables are not renamed to the overrider's before the comparison. Not changed: - `ConstraintSolverImpl` builds its constraints from wildcard upper bounds without this rule, so a type variable whose bound admits null, inferred into a non-null `? extends` position, may still go unreported. - `? super` containment does not resolve a type variable actual; javac rejects a `Box<T>` for a `Box<? super Object>` before NullAway runs. - A captured actual still skips the null check, so `Source<? extends T>.load()` returning `List<E>` passes as `List<? extends @nonnull T>` when `T`'s bound admits null. Such a capture cannot be told apart from the one in `asyncLoadAll`, whose `@NonNull` inference dropped. - A type variable bounded by an intersection whose every element is `@Nullable`, such as `T extends @nullable Object & @nullable Serializable`, is still treated as excluding null: `upperBoundIsNullable` reads the annotations of the intersection, not of its elements. apache#1836 fixes that. The new tests in `WildcardTests` put the reported and the accepted cases of each rule in one source, and a mutation of each branch of the new code turns at least one of them red. The library-model rule has no test through the new code: a model keys the bound to a class's own type parameter, which checked code sees only after substitution. `GenericsChecks` decides whether a bound admits null by hand in three more places; unifying them is outside this change. This is the first commit of apache#1828 as a standalone change; apache#1828 rewrites the message of the same diagnostic in `CheckIdenticalNullabilityVisitor`, `GenericsChecks`, and `WildcardTests`, so it goes after this one. Assisted-by: Claude Code (claude-opus-5-5)
…ard is required Previously, a `Box<T>` declared with `T extends @nullable Object` was accepted where a `Box<? extends Object>` was required, so a null could reach a position that rejects one. A wildcard actual in that position was judged by its effective upper bound, but a type variable was judged by its use site, which carries no nullness of its own, so every such assignment, argument, and return passed. No diagnostic marks the defect; the symptom is the missing error. `CheckIdenticalNullabilityVisitor` now compares a type variable that carries no `@Nullable` or `@NonNull` at its use site as its declared upper bound. That bound admits null when it is annotated `@Nullable`, when a library model overrides it, or when the variable is declared in unannotated code and its bound is not annotated `@NonNull`. A requirement that names a type variable, `? extends T`, admits whatever `T` is instantiated as, so there the two types are compared as written: `Box<T>` and `Box<S extends T>` stay assignable to `Box<? extends T>`, and `Box<@nullable T>` is still reported. The check then also asks whether the actual may be null where this instantiation of `T` may not: it walks the actual's declared bounds up to `T` and looks for a link annotated or declared `@Nullable`. So `Box<S>` with `S extends @nullable T` passed to `Box<? extends T>` is reported whatever `T`'s own bound, and so is `Box<T>` passed to `Box<? extends @nonnull T>` when `T`'s bound admits null. Where the walk does not reach `T`, as between the type variables of an overriding and an overridden generic method, the declared bounds of the two are compared instead. Substituting `T`'s bound instead was tried: on the requirement side alone it reports Caffeine's `CacheLoader.asyncReload`, where `CompletableFuture.supplyAsync` infers `CompletableFuture<V>` against `CompletableFuture<? extends V>`, and on both sides it silences the `Box<@nullable T>` report. ```text CacheLoader.java:196: error: [NullAway] incompatible types: CompletableFuture<V> cannot be converted to CompletableFuture<? extends V> ``` A captured actual skips the null check, because capture conversion drops type-use annotations. In Caffeine's `CacheLoader.asyncLoadAll`, a `Map<? extends K, ? extends @nonnull V>` returned into a `CompletableFuture<? extends Map<? extends K, ? extends @nonnull V>>` reaches the check as `Map<capture of ? extends K, capture of ? extends V>`, with no `@NonNull` left on `V`. Caller sees: - `incompatible types: Box<T> cannot be converted to Box<? extends Object>` where `T`'s declared bound admits null; accepted before. - `mismatched type parameter nullability` on an override that returns `List<V>` where the overridden method returns `List<? extends @nonnull V>` and `V` may be null; accepted before. Caffeine's `AsyncCacheLoader.bulk` is such an override. Code that compiled clean can need a narrower return type or a suppression. - `mismatched type parameter nullability` on an override of a generic method whose type variable admits null, when the override declares that variable without `@Nullable`, for example `<U> CompletionStage<U> thenApply(Function<? super T, ? extends U> fn)` overriding the unannotated JDK method. It comes beside the existing `Method type variable U has a non-null upper bound` report for the same declaration, because the overridden method's type variables are not renamed to the overrider's before the comparison. - `incompatible types: List<String> cannot be converted to List<@nullable String>` where a `@Nullable` argument is passed to a generic method of unannotated code that declares `T extends @nonnull Object` and the result is assigned to `List<@nullable String>`; accepted before. `GenericsUtils.upperBoundIsNullable` now checks an explicit annotation on a bound before the default of unannotated code, as JSpecify requires, so inference treats such a type variable as non-null. Wildcard bounds and both sides of the override bound check change only for a type variable whose bound reaches one, such as `<U extends S>` with `S extends @nonnull Object` in unannotated code. Any annotation NullAway recognizes as non-null counts. Not changed: - `ConstraintSolverImpl` builds its constraints from wildcard upper bounds without this rule, so a type variable whose bound admits null, inferred into a non-null `? extends` position, may still go unreported. - `? super` containment does not resolve a type variable actual; javac rejects a `Box<T>` for a `Box<? super Object>` before NullAway runs. - A captured actual still skips the null check, so `Source<? extends T>.load()` returning `List<E>` passes as `List<? extends @nonnull T>` when `T`'s bound admits null. Such a capture cannot be told apart from the one in `asyncLoadAll`, whose `@NonNull` inference dropped. - A type variable bounded by an intersection whose every element is `@Nullable`, such as `T extends @nullable Object & @nullable Serializable`, is still treated as excluding null: `upperBoundIsNullable` reads the annotations of the intersection, not of its elements. apache#1836 fixes that. The new tests in `WildcardTests` put the reported and the accepted cases of each rule in one source, and a mutation of each branch of the new code turns at least one of them red. The library-model rule has no test through the new code: a model keys the bound to a class's own type parameter, which checked code sees only after substitution. `GenericsChecks` decides whether a bound admits null by hand in three more places; unifying them is outside this change. This is the first commit of apache#1828 as a standalone change; apache#1828 rewrites the message of the same diagnostic in `CheckIdenticalNullabilityVisitor`, `GenericsChecks`, and `WildcardTests`, so it goes after this one. Assisted-by: Claude Code (claude-opus-5-5)
…ard is required Previously, a `Box<T>` declared with `T extends @nullable Object` was accepted where a `Box<? extends Object>` was required, so a null could reach a position that rejects one. A wildcard actual in that position was judged by its effective upper bound, but a type variable was judged by its use site, which carries no nullness of its own, so every such assignment, argument, and return passed. No diagnostic marks the defect; the symptom is the missing error. `CheckIdenticalNullabilityVisitor` now compares a type variable that carries no `@Nullable` or `@NonNull` at its use site as its declared upper bound. That bound admits null when it is annotated `@Nullable`, when a library model overrides it, or when the variable is declared in unannotated code and its bound is not annotated `@NonNull`. A requirement that names a type variable, `? extends T`, admits whatever `T` is instantiated as, so there the two types are compared as written: `Box<T>` and `Box<S extends T>` stay assignable to `Box<? extends T>`, and `Box<@nullable T>` is still reported. The check then also asks whether the actual may be null where this instantiation of `T` may not: it walks the actual's declared bounds up to `T` and looks for a link annotated or declared `@Nullable`. So `Box<S>` with `S extends @nullable T` passed to `Box<? extends T>` is reported whatever `T`'s own bound, and so is `Box<T>` passed to `Box<? extends @nonnull T>` when `T`'s bound admits null. Where the walk does not reach `T`, as between the type variables of an overriding and an overridden generic method, the check adds nothing. The `@NonNull` that inference substitutes for a type variable it finds not `@Nullable` does not count as a written `? extends @nonnull T`, so `new HashSet<>(list)` and `copyOf(list)` with a `List<T>` keep compiling. Substituting `T`'s bound instead was tried: on the requirement side alone it reports Caffeine's `CacheLoader.asyncReload`, where `CompletableFuture.supplyAsync` infers `CompletableFuture<V>` against `CompletableFuture<? extends V>`, and on both sides it silences the `Box<@nullable T>` report. ```text CacheLoader.java:196: error: [NullAway] incompatible types: CompletableFuture<V> cannot be converted to CompletableFuture<? extends V> ``` A captured actual skips the null check, because capture conversion drops type-use annotations. In Caffeine's `CacheLoader.asyncLoadAll`, a `Map<? extends K, ? extends @nonnull V>` returned into a `CompletableFuture<? extends Map<? extends K, ? extends @nonnull V>>` reaches the check as `Map<capture of ? extends K, capture of ? extends V>`, with no `@NonNull` left on `V`. Caller sees: - `incompatible types: Box<T> cannot be converted to Box<? extends Object>` where `T`'s declared bound admits null; accepted before. - `mismatched type parameter nullability` on an override that returns `List<V>` where the overridden method returns `List<? extends @nonnull V>` and `V` may be null; accepted before. Caffeine's `AsyncCacheLoader.bulk` is such an override. Code that compiled clean can need a narrower return type or a suppression. - `incompatible types: List<String> cannot be converted to List<@nullable String>` where a `@Nullable` argument is passed to a generic method of unannotated code that declares `T extends @nonnull Object` and the result is assigned to `List<@nullable String>`; accepted before. `GenericsUtils.upperBoundIsNullable` now checks an explicit annotation on a bound before the default of unannotated code, as JSpecify requires, so inference treats such a type variable as non-null. Wildcard bounds and both sides of the override bound check change only for a type variable whose bound reaches one, such as `<U extends S>` with `S extends @nonnull Object` in unannotated code. Any annotation NullAway recognizes as non-null counts. Not changed: - `ConstraintSolverImpl` builds its constraints from wildcard upper bounds without this rule, so a type variable whose bound admits null, inferred into a non-null `? extends` position, may still go unreported. - `? super` containment does not resolve a type variable actual; javac rejects a `Box<T>` for a `Box<? super Object>` before NullAway runs. - A captured actual still skips the null check, so `Source<? extends T>.load()` returning `List<E>` passes as `List<? extends @nonnull T>` when `T`'s bound admits null. Such a capture cannot be told apart from the one in `asyncLoadAll`, whose `@NonNull` inference dropped. - A type variable bounded by an intersection whose every element is `@Nullable`, such as `T extends @nullable Object & @nullable Serializable`, is still treated as excluding null: `upperBoundIsNullable` reads the annotations of the intersection, not of its elements. apache#1836 fixes that. The new tests put the reported and the accepted cases of each rule in one source, and a mutation of each branch of the new code turns at least one of them red. The library-model rule has no test through the new code: a model keys the bound to a class's own type parameter, which checked code sees only after substitution. `GenericsChecks` decides whether a bound admits null by hand in three more places; unifying them is outside this change. This is the first commit of apache#1828 as a standalone change; apache#1828 rewrites the message of the same diagnostic in `CheckIdenticalNullabilityVisitor`, `GenericsChecks`, and `WildcardTests`, so it goes after this one. Assisted-by: Claude Code (claude-opus-5-5)
…ard is required Previously, a `Box<T>` declared with `T extends @nullable Object` was accepted where a `Box<? extends Object>` was required, so a null could reach a position that rejects one. A wildcard actual in that position was judged by its effective upper bound, but a type variable was judged by its use site, which carries no nullness of its own, so every such assignment, argument, and return passed. No diagnostic marks the defect; the symptom is the missing error. `CheckIdenticalNullabilityVisitor` now compares a type variable that carries no `@Nullable` or `@NonNull` at its use site as its declared upper bound. That bound admits null when it is annotated `@Nullable`, directly or through the bound of another type variable that carries no annotation, or when a library model overrides it. A type variable declared in unannotated code with no `@Nullable` on its bound and no library model is not reported, as NullAway stays optimistic about unannotated code: the unannotated default of `upperBoundIsNullable`, which lets inference, override, and wildcard-bound checks accept a nullable instantiation, does not apply to this check. A `@Nullable` written in unannotated code still counts, since JSpecify gives annotations in null-unmarked code their meaning. A requirement that names a type variable, `? extends T`, admits whatever `T` is instantiated as, so there the two types are compared as written: `Box<T>` and `Box<S extends T>` stay assignable to `Box<? extends T>`, and `Box<@nullable T>` is still reported. The check then also asks whether the actual may be null where this instantiation of `T` may not: it walks the actual's declared bounds up to `T` and looks for a link annotated or declared `@Nullable`. So `Box<S>` with `S extends @nullable T` passed to `Box<? extends T>` is reported whatever `T`'s own bound, and so is `Box<T>` passed to `Box<? extends @nonnull T>` when `T`'s bound admits null. Where the walk does not reach `T`, as between the type variables of an overriding and an overridden generic method, the check adds nothing. The `@NonNull` that inference substitutes for a type variable it finds not `@Nullable` does not count as a written `? extends @nonnull T`, so `new HashSet<>(list)` and `copyOf(list)` with a `List<T>` keep compiling. Substituting `T`'s bound instead was tried: on the requirement side alone it reports Caffeine's `CacheLoader.asyncReload`, where `CompletableFuture.supplyAsync` infers `CompletableFuture<V>` against `CompletableFuture<? extends V>`, and on both sides it silences the `Box<@nullable T>` report. ```text CacheLoader.java:196: error: [NullAway] incompatible types: CompletableFuture<V> cannot be converted to CompletableFuture<? extends V> ``` A captured actual skips the null check, because capture conversion drops type-use annotations. In Caffeine's `CacheLoader.asyncLoadAll`, a `Map<? extends K, ? extends @nonnull V>` returned into a `CompletableFuture<? extends Map<? extends K, ? extends @nonnull V>>` reaches the check as `Map<capture of ? extends K, capture of ? extends V>`, with no `@NonNull` left on `V`. Caller sees: - `incompatible types: Box<T> cannot be converted to Box<? extends Object>` where `T`'s declared bound admits null; accepted before. - `mismatched type parameter nullability` on an override that returns `List<V>` where the overridden method returns `List<? extends @nonnull V>` and `V` may be null; accepted before. Caffeine's `AsyncCacheLoader.bulk` is such an override. Code that compiled clean can need a narrower return type or a suppression. Not changed: - `ConstraintSolverImpl` builds its constraints from wildcard upper bounds without this rule, so a type variable whose bound admits null, inferred into a non-null `? extends` position, may still go unreported. - A `? extends @nonnull T` whose `@NonNull` comes from a library model or from stream nullability propagation carries NullAway's synthetic annotation, which this check cannot tell apart from the one inference substitutes, so it counts as `? extends T`. That can miss an error, not add one. - `? super` containment does not resolve a type variable actual; javac rejects a `Box<T>` for a `Box<? super Object>` before NullAway runs. - A captured actual still skips the null check against a requirement that names a type variable. So `Source<? extends T>.load()` returning `List<E>` passes as `List<? extends @nonnull T>` when `T`'s bound admits null, and `Holder<S>.get()` returning `Box<? extends X>` passes as `Box<? extends T>` for `S extends @nullable T`. Such a capture cannot be told apart from one whose `@NonNull` inference dropped, as in `asyncLoadAll` or a `Map<String, ? extends @nonnull S>` returned through `CompletableFuture.supplyAsync`, and checking it reported those. - A type variable bounded by an intersection whose every element is `@Nullable`, such as `T extends @nullable Object & @nullable Serializable`, is still treated as excluding null: `upperBoundIsNullable` and the new `GenericsUtils.boundIsExplicitlyNullable` read the annotations of the intersection, not of its elements. apache#1836 fixes `upperBoundIsNullable`; the new helper needs the same change. The new tests put the reported and the accepted cases of each rule in one source, and a mutation of each branch of the new code turns at least one of them red. The library-model rule has no test through the new code: a model keys the bound to a class's or method's own type parameter, which checked code sees only after substitution. `GenericsChecks` decides whether a bound admits null by hand in three more places; unifying them is outside this change. This is the first commit of apache#1828 as a standalone change; apache#1828 rewrites the message of the same diagnostic in `CheckIdenticalNullabilityVisitor`, `GenericsChecks`, and `WildcardTests`, so it goes after this one. Assisted-by: Claude Code (claude-opus-5-5)
…ard is required Previously, a `Box<T>` declared with `T extends @nullable Object` was accepted where a `Box<? extends Object>` was required, so a null could reach a position that rejects one. A wildcard actual in that position was judged by its effective upper bound, but a type variable was judged by its use site, which carries no nullness of its own, so every such assignment, argument, and return passed. No diagnostic marks the defect; the symptom is the missing error. Against a `? extends` requirement with a concrete bound, `CheckIdenticalNullabilityVisitor` now compares a type variable that carries no `@Nullable` or `@NonNull` at its use site as its declared upper bound. That bound admits null when it is annotated `@Nullable`, directly or through the bound of another type variable that carries no annotation, or when a library model overrides it. A type variable declared in unannotated code with no `@Nullable` on its bound and no library model is not reported, as NullAway stays optimistic about unannotated code: the unannotated default of `upperBoundIsNullable`, which lets inference, override, and wildcard-bound checks accept a nullable instantiation, does not apply to this check. A `@Nullable` written in unannotated code still counts, since JSpecify gives annotations in null-unmarked code their meaning. A requirement that names a type variable, `? extends T`, admits whatever `T` is instantiated as, so there the two types are compared as written: `Box<T>` and `Box<S extends T>` stay assignable to `Box<? extends T>`, and `Box<@nullable T>` is still reported. The check then also asks whether the actual may be null where this instantiation of `T` may not: it walks the actual's declared bounds up to `T` and looks for a link annotated or declared `@Nullable`. So `Box<S>` with `S extends @nullable T` passed to `Box<? extends T>` is reported whatever `T`'s own bound, and so is `Box<T>` passed to `Box<? extends @nonnull T>` when `T`'s bound admits null. Where the walk does not reach `T`, as between the type variables of an overriding and an overridden generic method, the check adds nothing. The `@NonNull` that inference substitutes for a type variable it finds not `@Nullable` does not count as a written `? extends @nonnull T`, so `new HashSet<>(list)` and `copyOf(list)` with a `List<T>` keep compiling. Substituting `T`'s bound instead was tried: on the requirement side alone it reports Caffeine's `CacheLoader.asyncReload`, where `CompletableFuture.supplyAsync` infers `CompletableFuture<V>` against `CompletableFuture<? extends V>`, and on both sides it silences the `Box<@nullable T>` report. ```text CacheLoader.java:196: error: [NullAway] incompatible types: CompletableFuture<V> cannot be converted to CompletableFuture<? extends V> ``` A captured actual skips the null check, because capture conversion drops type-use annotations. In Caffeine's `CacheLoader.asyncLoadAll`, a `Map<? extends K, ? extends @nonnull V>` returned into a `CompletableFuture<? extends Map<? extends K, ? extends @nonnull V>>` reaches the check as `Map<capture of ? extends K, capture of ? extends V>`, with no `@NonNull` left on `V`. Caller sees: - `incompatible types: Box<T> cannot be converted to Box<? extends Object>` where `T`'s declared bound admits null; accepted before. - `mismatched type parameter nullability` on an override that returns `List<V>` where the overridden method returns `List<? extends @nonnull V>` and `V` may be null; accepted before. Caffeine's `AsyncCacheLoader.bulk` is such an override. Code that compiled clean can need a narrower return type or a suppression. Not changed: - `ConstraintSolverImpl` builds its constraints from wildcard upper bounds without this rule, so a type variable whose bound admits null, inferred into a non-null `? extends` position, may still go unreported. - A `? extends @nonnull T` whose `@NonNull` comes from a library model or from stream nullability propagation carries NullAway's synthetic annotation, which this check cannot tell apart from the one inference substitutes, so it counts as `? extends T`. That can miss an error, not add one. - An unbounded `?` compares a type variable actual as written, as before, so a `Class<T>` with `T extends @nullable Object` still fits a `Class<?>` parameter such as the one of `Class.isAssignableFrom`. Such a `Class<T>` is ill-formed under JSpecify, since `Class` requires a non-null argument, but NullAway does not report it where it is written either. - `? super` containment does not resolve a type variable actual; javac rejects a `Box<T>` for a `Box<? super Object>` before NullAway runs. - A captured actual still skips the null check against a requirement that names a type variable. So `Source<? extends T>.load()` returning `List<E>` passes as `List<? extends @nonnull T>` when `T`'s bound admits null, and `Holder<S>.get()` returning `Box<? extends X>` passes as `Box<? extends T>` for `S extends @nullable T`. Such a capture cannot be told apart from one whose `@NonNull` inference dropped, as in `asyncLoadAll` or a `Map<String, ? extends @nonnull S>` returned through `CompletableFuture.supplyAsync`, and checking it reported those. - A type variable bounded by an intersection whose every element is `@Nullable`, such as `T extends @nullable Object & @nullable Serializable`, is still treated as excluding null: `upperBoundIsNullable` and the new `GenericsUtils.boundIsExplicitlyNullable` read the annotations of the intersection, not of its elements. apache#1836 fixes `upperBoundIsNullable`; the new helper needs the same change. The new tests put the reported and the accepted cases of each rule in one source, and a mutation of each branch of the new code turns at least one of them red. The library-model rule has no test through the new code: a model keys the bound to a class's or method's own type parameter, which checked code sees only after substitution. `GenericsChecks` decides whether a bound admits null by hand in three more places; unifying them is outside this change. This is the first commit of apache#1828 as a standalone change; apache#1828 rewrites the message of the same diagnostic in `CheckIdenticalNullabilityVisitor`, `GenericsChecks`, and `WildcardTests`, so it goes after this one. Assisted-by: Claude Code (claude-opus-5-5)
…ard is required Previously, a `Box<T>` declared with `T extends @nullable Object` was accepted where a `Box<? extends Object>` was required, so a null could reach a position that rejects one. A wildcard actual in that position was judged by its effective upper bound, but a type variable was judged by its use site, which carries no nullness of its own, so every such assignment, argument, and return passed. No diagnostic marks the defect; the symptom is the missing error. Against a `? extends` requirement with a concrete bound, `CheckIdenticalNullabilityVisitor` now compares a type variable that carries no `@Nullable` or `@NonNull` at its use site as its declared upper bound. That bound admits null when it is annotated `@Nullable`, directly or through the bound of another type variable that carries no annotation, or when a library model overrides it. A type variable declared in unannotated code with no `@Nullable` on its bound and no library model is not reported, as NullAway stays optimistic about unannotated code: the unannotated default of `upperBoundIsNullable`, which lets inference, override, and wildcard-bound checks accept a nullable instantiation, does not apply to this check. A `@Nullable` written in unannotated code still counts, since JSpecify gives annotations in null-unmarked code their meaning. A requirement that names a type variable, `? extends T`, admits whatever `T` is instantiated as, so there the two types are compared as written: `Box<T>` and `Box<S extends T>` stay assignable to `Box<? extends T>`, and `Box<@nullable T>` is still reported. The check then also asks whether the actual may be null where this instantiation of `T` may not: it walks the actual's declared bounds up to `T` and looks for a link annotated or declared `@Nullable`. So `Box<S>` with `S extends @nullable T` passed to `Box<? extends T>` is reported whatever `T`'s own bound, and so is `Box<T>` passed to `Box<? extends @nonnull T>` when `T`'s bound admits null. Where the walk does not reach `T`, as between the type variables of an overriding and an overridden generic method, the check adds nothing. The `@NonNull` that inference substitutes for a type variable it finds not `@Nullable` does not count as a written `? extends @nonnull T`, so `new HashSet<>(list)` and `copyOf(list)` with a `List<T>` keep compiling. Substituting `T`'s bound instead was tried: on the requirement side alone it reports Caffeine's `CacheLoader.asyncReload`, where `CompletableFuture.supplyAsync` infers `CompletableFuture<V>` against `CompletableFuture<? extends V>`, and on both sides it silences the `Box<@nullable T>` report. ```text CacheLoader.java:196: error: [NullAway] incompatible types: CompletableFuture<V> cannot be converted to CompletableFuture<? extends V> ``` A captured actual is compared as written, against a concrete requirement as well as one that names a type variable, because capture conversion drops type-use annotations. So is any actual against a requirement whose only nullness is the `@NonNull` inference substituted, since the constraint solver chose that bound without this rule. In Caffeine's `CacheLoader.asyncLoadAll`, a `Map<? extends K, ? extends @nonnull V>` returned into a `CompletableFuture<? extends Map<? extends K, ? extends @nonnull V>>` reaches the check as `Map<capture of ? extends K, capture of ? extends V>`, with no `@NonNull` left on `V`. Caller sees: - `incompatible types: Box<T> cannot be converted to Box<? extends Object>` where `T`'s declared bound admits null; accepted before. - `mismatched type parameter nullability` on an override that returns `List<V>` where the overridden method returns `List<? extends @nonnull V>` and `V` may be null; accepted before. Caffeine's `AsyncCacheLoader.bulk` is such an override. Code that compiled clean can need a narrower return type or a suppression. Not changed: - `ConstraintSolverImpl` builds its constraints from wildcard upper bounds without this rule, and the check compares an actual as written against a bound that inference chose. So `first(box)` with `<U> U first(Box<? extends U> b)` and a `Box<T>` whose `T` admits null goes unreported, though `U` cannot be null, and so does `take("x", box)` with `<U> void take(U u, Box<? extends U> b)`, where javac infers `U` as `Object`. - NullAway's synthetic `@NonNull` comes from inference, library models, and stream nullability propagation alike, and this check cannot tell them apart. On a requirement it counts as no annotation, so a modeled `? extends @nonnull T` counts as `? extends T`; on an actual it counts as written, so `Box<? extends Object> x = idBox(box)` with `<U> Box<U> idBox(Box<U> b)` and a `Box<T>` whose `T` admits null goes unreported. Both can miss an error, not add one. - An unbounded `?` compares a type variable actual as written, as before, so a `Class<T>` with `T extends @nullable Object` still fits a `Class<?>` parameter such as the one of `Class.isAssignableFrom`. Such a `Class<T>` is ill-formed under JSpecify, since `Class` requires a non-null argument, but NullAway does not report it where it is written either. - `? super` containment does not resolve a type variable actual; javac rejects a `Box<T>` for a `Box<? super Object>` before NullAway runs. - A captured actual is still compared as written. So `return get();` with `get()` returning `Box<? extends T>` passes as `Box<? extends Object>`, `Source<? extends T>.load()` returning `List<E>` passes as `List<? extends @nonnull T>` when `T`'s bound admits null, and `Holder<S>.get()` returning `Box<? extends X>` passes as `Box<? extends T>` for `S extends @nullable T`. Such a capture cannot be told apart from one whose `@NonNull` inference dropped, as in `asyncLoadAll` or a `Map<String, ? extends @nonnull S>` returned through `CompletableFuture.supplyAsync`, and checking it reported those. - A type variable bounded by an intersection whose every element is `@Nullable`, such as `T extends @nullable Object & @nullable Serializable`, is still treated as excluding null: `upperBoundIsNullable` and the new `GenericsUtils.boundIsExplicitlyNullable` read the annotations of the intersection, not of its elements. apache#1836 fixes `upperBoundIsNullable`; the new helper needs the same change. The new tests put the reported and the accepted cases of each rule in one source, and a mutation of each branch of the new code turns at least one of them red. The library-model rule has no test through the new code: a model keys the bound to a class's or method's own type parameter, which checked code sees only after substitution. `GenericsChecks` decides whether a bound admits null by hand in three more places; unifying them is outside this change. This is the first commit of apache#1828 as a standalone change; apache#1828 rewrites the message of the same diagnostic in `CheckIdenticalNullabilityVisitor`, `GenericsChecks`, and `WildcardTests`, so it goes after this one. Assisted-by: Claude Code (claude-opus-5-5)
…ard is required Previously, a `Box<T>` declared with `T extends @nullable Object` was accepted where a `Box<? extends Object>` was required, so a null could reach a position that rejects one. A wildcard actual in that position was judged by its effective upper bound, but a type variable was judged by its use site, which carries no nullness of its own, so every such assignment, argument, and return passed. No diagnostic marks the defect; the symptom is the missing error. Against a `? extends` requirement with a concrete bound, `CheckIdenticalNullabilityVisitor` now compares a type variable that carries no `@Nullable` or `@NonNull` at its use site as its declared upper bound. That bound admits null when it is annotated `@Nullable`, directly or through the bound of another type variable that carries no annotation, or when a library model overrides it. A type variable declared in unannotated code with no `@Nullable` on its bound and no library model is not reported, as NullAway stays optimistic about unannotated code: the unannotated default of `upperBoundIsNullable`, which lets inference, override, and wildcard-bound checks accept a nullable instantiation, does not apply to this check. A `@Nullable` written in unannotated code still counts, since JSpecify gives annotations in null-unmarked code their meaning. A requirement that names a type variable, `? extends T`, admits whatever `T` is instantiated as, so there the two types are compared as written: `Box<T>` and `Box<S extends T>` stay assignable to `Box<? extends T>`, and `Box<@nullable T>` is still reported. The check then also asks whether the actual may be null where this instantiation of `T` may not: it walks the actual's declared bounds up to `T` and looks for a link annotated or declared `@Nullable`. So `Box<S>` with `S extends @nullable T` passed to `Box<? extends T>` is reported whatever `T`'s own bound, and so is `Box<T>` passed to `Box<? extends @nonnull T>` when `T`'s bound admits null. Where the walk does not reach `T`, as between the type variables of an overriding and an overridden generic method, the check adds nothing. The `@NonNull` that inference substitutes for a type variable it finds not `@Nullable` does not count as a written `? extends @nonnull T`, so `new HashSet<>(list)` with a `List<T>`, and a `copyOf(list)` whose type variable admits null, keep compiling. Substituting `T`'s bound instead was tried: on the requirement side alone it reports Caffeine's `CacheLoader.asyncReload`, where `CompletableFuture.supplyAsync` infers `CompletableFuture<V>` against `CompletableFuture<? extends V>`, and on both sides it silences the `Box<@nullable T>` report. ```text CacheLoader.java:196: error: [NullAway] incompatible types: CompletableFuture<V> cannot be converted to CompletableFuture<? extends V> ``` A captured actual is compared as written, against a concrete requirement as well as one that names a type variable, because capture conversion drops type-use annotations. So is any actual against a requirement whose only nullness is the `@NonNull` inference substituted, since the constraint solver chose that bound, and applies this rule only to a variable whose bound excludes null. In Caffeine's `CacheLoader.asyncLoadAll`, a `Map<? extends K, ? extends @nonnull V>` returned into a `CompletableFuture<? extends Map<? extends K, ? extends @nonnull V>>` reaches the check as `Map<capture of ? extends K, capture of ? extends V>`, with no `@NonNull` left on `V`. Caller sees: - `incompatible types: Box<T> cannot be converted to Box<? extends Object>` where `T`'s declared bound admits null; accepted before. - `mismatched type parameter nullability` on an override that returns `List<V>` where the overridden method returns `List<? extends @nonnull V>` and `V` may be null; accepted before. Caffeine's `AsyncCacheLoader.bulk` is such an override. Code that compiled clean can need a narrower return type or a suppression. - `inference failure: type variable U is constrained to be @nullable, but its upper bound requires it to be @NonNull` where a `Box<T>` whose `T` admits null is passed as the `Box<? extends U>` of a generic method whose `U` excludes null, such as `List.copyOf` with a `List<T>`; accepted before. Where another argument fixes `U`, `incompatible types` comes beside it. Not changed: - The constraint solver applies this rule only to an inference variable whose bound excludes null. Against a bound it chose for one whose bound admits null, the check compares an actual as written, so `List<Object> l = new ArrayList<>(list)` with a `List<T>` whose `T` admits null goes unreported, while `take("x", box)` with `<U extends @nullable Object> void take(U u, Box<? extends U> b)` stays accepted, as it should. - NullAway's synthetic `@NonNull` comes from inference, library models, and stream nullability propagation alike, and this check cannot tell them apart. On a requirement it counts as no annotation, so a modeled `? extends @nonnull T` counts as `? extends T`; on an actual it counts as written, so `Box<? extends Object> x = idBox(box)` with `<U> Box<U> idBox(Box<U> b)` and a `Box<T>` whose `T` admits null goes unreported. Both can miss an error, not add one. - An unbounded `?` compares a type variable actual as written, as before, so a `Class<T>` with `T extends @nullable Object` still fits a `Class<?>` parameter such as the one of `Class.isAssignableFrom`. Such a `Class<T>` is ill-formed under JSpecify, since `Class` requires a non-null argument, but NullAway does not report it where it is written either. - `? super` containment does not resolve a type variable actual; javac rejects a `Box<T>` for a `Box<? super Object>` before NullAway runs. - A captured actual is still compared as written. So `return get();` with `get()` returning `Box<? extends T>` passes as `Box<? extends Object>`, `Source<? extends T>.load()` returning `List<E>` passes as `List<? extends @nonnull T>` when `T`'s bound admits null, and `Holder<S>.get()` returning `Box<? extends X>` passes as `Box<? extends T>` for `S extends @nullable T`. Such a capture cannot be told apart from one whose `@NonNull` inference dropped, as in `asyncLoadAll` or a `Map<String, ? extends @nonnull S>` returned through `CompletableFuture.supplyAsync`, and checking it reported those. - A type variable bounded by an intersection whose every element is `@Nullable`, such as `T extends @nullable Object & @nullable Serializable`, is still treated as excluding null: `upperBoundIsNullable` and the new `GenericsUtils.boundIsExplicitlyNullable` read the annotations of the intersection, not of its elements. apache#1836 fixes `upperBoundIsNullable`; the new helper needs the same change. The new tests put the reported and the accepted cases of each rule in one source, and a mutation of each condition of the new code turns at least one of them red, except two in the constraint solver: its `? extends` test, which an unbounded `?` never reaches with an inference variable, and its skip of captured actuals, which mirrors the check and which no test case separates. The library-model rule has no test through the new code: a model keys the bound to a class's or method's own type parameter, which checked code sees only after substitution. `GenericsChecks` decides whether a bound admits null by hand in three more places; unifying them is outside this change. This is the first commit of apache#1828 as a standalone change; apache#1828 rewrites the message of the same diagnostic in `CheckIdenticalNullabilityVisitor`, `GenericsChecks`, and `WildcardTests`, so it goes after this one. Assisted-by: Claude Code (claude-opus-5-5)
…ard is required Previously, a `Box<T>` declared with `T extends @nullable Object` was accepted where a `Box<? extends Object>` was required, and a `Box<Object>` was accepted where a `Box<? super T>` with that `T` was required, so a null could reach a position that excludes one. The containment check judged a type argument by the annotation on its use site, which a bare type variable has none of, so every such assignment, argument, and return passed. No diagnostic marks the defect; the symptom is the missing error. `CheckIdenticalNullabilityVisitor` now decides the top-level nullness of every wildcard containment check, for `? extends`, `?`, and `? super`, with the JSpecify nullness subtyping rules: a type is a nullness subtype of another where the supertype is null-inclusive, where the subtype is null-exclusive, or where the supertype is a type variable, not written `@NonNull`, that the subtype reaches through declared upper bounds none of which is nullable. A type variable is null-exclusive only through its bounds, so `T` with `T extends @nullable Object` fits neither `? extends Object` nor the lower bound of `? super T` from a `Box<Object>`, while `S` with `S extends T` still fits `? extends T` and `@Nullable T` does not. Each type usage carries one of the specification's four nullness operators, read in the specification's lenient "some world" sense, so the places where NullAway stays optimistic are the usages that get `UNSPECIFIED`: the synthetic `@NonNull` that inference substitutes for a variable it could not constrain, a bare class-type bound declared in unannotated code, a bare type variable left by capture conversion, which drops a written `@NonNull`, and a type variable actual against an unbounded `?`, whose implicit bound is the formal type parameter's own and already constrains the actual's declaration. A `@Nullable` written in unannotated code still counts, as JSpecify gives annotations in null-unmarked code their meaning. A rule written by hand for the type-variable case of `? extends` was tried first; every boundary with the rest of the checker needed an exception of its own, and the `? super` case was missed. The override check renames the overriding method's own type variables to the overridden method's before comparing, as javac does to decide override equivalence, so a `Box<V>` returned over a `Box<? extends V>` is judged with one `V`; without the renaming the path from one `V` to the other does not exist and every such override would be reported. At a generic method call, the constraint solver constrains the inference variable `U` of a `? extends U` to be `@Nullable` where the actual is an uncaptured, unannotated type variable whose bound is explicitly nullable and `U`'s own bound excludes null, which no solution satisfies, so the call reports an inference failure rather than an incompatible type after a `@NonNull` substitution. Caller sees: - `incompatible types: Box<T> cannot be converted to Box<? extends Object>` where `T`'s declared bound admits null; accepted before. - `incompatible types: Box<Object> cannot be converted to Box<? super T>` where `T`'s declared bound admits null, and the same for a `Box<? super Object>`; accepted before. - `incompatible types: Box<S> cannot be converted to Box<? extends T>` for `<S extends @nullable T>`, whatever `T`'s own bound, and `Box<T>` against `Box<? extends @nonnull T>` where `T`'s bound admits null; accepted before. - `incompatible types: Box<T> cannot be converted to Box<? extends Object>` for `<T extends @nullable Object & @nullable Serializable>`, since the elements of an intersection bound are read one by one and a bound with one non-null element excludes null; accepted before, which apache#1836 reports for the other places that read a bound. - `mismatched type parameter nullability` on an override that returns `List<V>` where the overridden method returns `List<? extends @nonnull V>` and `V` may be null; accepted before. Caffeine's `AsyncCacheLoader.bulk` is such an override. - `inference failure: type variable U is constrained to be @nullable, but its upper bound requires it to be @NonNull` where a `Box<T>` whose `T` admits null is passed as the `Box<? extends U>` of a generic method whose `U` excludes null, such as `List.copyOf` with a `List<T>`; accepted before. Not changed: - A type variable declared in unannotated code with no `@Nullable` on its bound and no library model is not reported; NullAway stays optimistic about unannotated code. The library-model branch has no test: the modeled bounds belong to type parameters of library classes and methods, and no modeled generic method has a wildcard bounded by its own type variable for an override to meet. - A `Box<T>` whose `T` admits null passed as the `Box<? extends U>` of `<U extends @nullable Object> U first(Box<? extends U> b)` infers `U` non-null, so `first(b).toString()` is not reported. Constraining `U` to be nullable there would substitute `@Nullable T` where javac inferred `T` and report `List<T> copy = copyOf(in)`, since the solver infers one nullness per variable. - A captured actual's bare type variable is not reported: `return get();` with `get()` returning `Box<? extends T>` still passes as `Box<? extends Object>`. Such a capture cannot be told from one whose written `@NonNull` capture conversion dropped, as in Caffeine's `CacheLoader.asyncLoadAll`, and checking it reported that method. - `Box<? extends Object> x = idBox(box)` with `<U extends @nullable Object> Box<U> idBox(Box<U> b)` and a `Box<T>` whose `T` admits null is not reported: the solver infers `U` non-null when nothing constrains it, and that substituted `@NonNull` counts as unspecified here. `new HashSet<>(list)` and `copyOf(list)` with a `List<T>` keep compiling for the same reason. - A `Class<T>` with `T extends @nullable Object` still fits a `Class<?>` parameter such as the one of `Class.isAssignableFrom`; the declaration `Class<T>` is not reported either. - Dereferencing a `T` whose bound admits null, and passing a `@Nullable T` where a `T` is required, are still not reported; that is apache#1727. An invariant type argument is still compared by annotation only, so a `Box<@nonnull T>` is accepted where a `Box<T>` is required. Neither has a test in this change. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
…ard is required Previously, a `Box<T>` declared with `T extends @nullable Object` was accepted where a `Box<? extends Object>` was required, and a `Box<Object>` was accepted where a `Box<? super T>` with that `T` was required, so a null could reach a position that excludes one. The containment check judged a type argument by the annotation on its use site, which a bare type variable has none of, so every such assignment, argument, and return passed. No diagnostic marks the defect; the symptom is the missing error. `CheckIdenticalNullabilityVisitor` now decides the top-level nullness of every wildcard containment check, for `? extends`, `?`, and `? super`, with the JSpecify nullness subtyping rules: a type is a nullness subtype of another where the supertype is null-inclusive, where the subtype is null-exclusive, or where the supertype is a type variable, not written `@NonNull`, that the subtype reaches through declared upper bounds none of which is nullable. A type variable is null-exclusive only through its bounds, so `T` with `T extends @nullable Object` fits neither `? extends Object` nor the lower bound of `? super T` from a `Box<Object>`, while `S` with `S extends T` still fits `? extends T` and `@Nullable T` does not. Each type usage carries one of the specification's four nullness operators, read in the specification's lenient "some world" sense, so the places where NullAway stays optimistic are the usages that get `UNSPECIFIED`: the synthetic `@NonNull` that inference substitutes for a variable it could not constrain, a bare class-type bound declared in unannotated code, a bare type variable left by capture conversion, which drops a written `@NonNull`, and a type variable actual against an unbounded `?`, whose implicit bound is the formal type parameter's own and already constrains the actual's declaration. A `@Nullable` written in unannotated code still counts, as JSpecify gives annotations in null-unmarked code their meaning. A rule written by hand for the type-variable case of `? extends` was tried first; every boundary with the rest of the checker needed an exception of its own, and the `? super` case was missed. The override check renames the overriding method's own type variables to the overridden method's before comparing, as javac does to decide override equivalence, so a `Box<V>` returned over a `Box<? extends V>` is judged with one `V`; without the renaming the path from one `V` to the other does not exist and every such override would be reported. At a generic method call, the constraint solver constrains the inference variable `U` of a `? extends U` to be `@Nullable` where the actual is an uncaptured, unannotated type variable whose bound is explicitly nullable and `U`'s own bound excludes null, which no solution satisfies, so the call reports an inference failure rather than an incompatible type after a `@NonNull` substitution. Caller sees: - `incompatible types: Box<T> cannot be converted to Box<? extends Object>` where `T`'s declared bound admits null; accepted before. - `incompatible types: Box<Object> cannot be converted to Box<? super T>` where `T`'s declared bound admits null, and the same for a `Box<? super Object>`; accepted before. - `incompatible types: Box<S> cannot be converted to Box<? extends T>` for `<S extends @nullable T>`, whatever `T`'s own bound, and `Box<T>` against `Box<? extends @nonnull T>` where `T`'s bound admits null; accepted before. - `incompatible types: Box<T> cannot be converted to Box<? extends Object>` for `<T extends @nullable Object & @nullable Serializable>`, since the elements of an intersection bound are read one by one and a bound with one non-null element excludes null; accepted before, which apache#1836 reports for the other places that read a bound. - `mismatched type parameter nullability` on an override that returns `List<V>` where the overridden method returns `List<? extends @nonnull V>` and `V` may be null; accepted before. Caffeine's `AsyncCacheLoader.bulk` is such an override. - `inference failure: type variable U is constrained to be @nullable, but its upper bound requires it to be @NonNull` where a `Box<T>` whose `T` admits null is passed as the `Box<? extends U>` of a generic method whose `U` excludes null, such as `List.copyOf` with a `List<T>`; accepted before. Not changed: - A type variable declared in unannotated code with no `@Nullable` on its bound and no library model is not reported; NullAway stays optimistic about unannotated code. The library-model branch has no test: the modeled bounds belong to type parameters of library classes and methods, and no modeled generic method has a wildcard bounded by its own type variable for an override to meet. - A `Box<T>` whose `T` admits null passed as the `Box<? extends U>` of `<U extends @nullable Object> U first(Box<? extends U> b)` infers `U` non-null, so `first(b).toString()` is not reported. Constraining `U` to be nullable there would substitute `@Nullable T` where javac inferred `T` and report `List<T> copy = copyOf(in)`, since the solver infers one nullness per variable. - A captured actual's bare type variable is not reported: `return get();` with `get()` returning `Box<? extends T>` still passes as `Box<? extends Object>`. Such a capture cannot be told from one whose written `@NonNull` capture conversion dropped, as in Caffeine's `CacheLoader.asyncLoadAll`, and checking it reported that method. - `Box<? extends Object> x = idBox(box)` with `<U extends @nullable Object> Box<U> idBox(Box<U> b)` and a `Box<T>` whose `T` admits null is not reported: the solver infers `U` non-null when nothing constrains it, and that substituted `@NonNull` counts as unspecified here. `new HashSet<>(list)` and `copyOf(list)` with a `List<T>` keep compiling for the same reason. - A `Class<T>` with `T extends @nullable Object` still fits a `Class<?>` parameter such as the one of `Class.isAssignableFrom`; the declaration `Class<T>` is not reported either. - Dereferencing a `T` whose bound admits null, and passing a `@Nullable T` where a `T` is required, are still not reported; that is apache#1727. An invariant type argument is still compared by annotation only, so a `Box<@nonnull T>` is accepted where a `Box<T>` is required. Neither has a test in this change. Assisted-by: Claude Code (claude-fable-5-1)
…ard is required Previously, a `Box<T>` declared with `T extends @nullable Object` was accepted where a `Box<? extends Object>` was required, and a `Box<Object>` was accepted where a `Box<? super T>` with that `T` was required, so a null could reach a position that excludes one. The containment check judged a type argument by the annotation on its use site, which a bare type variable has none of, so every such assignment, argument, and return passed. No diagnostic marks the defect; the symptom is the missing error. `CheckIdenticalNullabilityVisitor` now decides the top-level nullness of every wildcard containment check, for `? extends`, `?`, and `? super`, with the JSpecify nullness subtyping rules: a type is a nullness subtype of another where the supertype is null-inclusive, where the subtype is null-exclusive, or where the supertype is a type variable, not written `@NonNull`, that the subtype reaches through declared upper bounds none of which is nullable. A type variable is null-exclusive only through its bounds, so `T` with `T extends @nullable Object` fits neither `? extends Object` nor the lower bound of `? super T` from a `Box<Object>`, while `S` with `S extends T` still fits `? extends T` and `@Nullable T` does not. Each type usage carries one of the specification's four nullness operators, read in the specification's lenient "some world" sense, so the places where NullAway stays optimistic are the usages that get `UNSPECIFIED`: a type variable that inference left unconstrained, a bare class-type bound declared in unannotated code, a bare type variable left by capture conversion, which drops a written `@NonNull`, and a type variable actual against an unbounded `?`, whose implicit bound is the formal type parameter's own and already constrains the actual's declaration. A `@Nullable` written in unannotated code still counts, as JSpecify gives annotations in null-unmarked code their meaning, and so does an annotation on an element of an intersection bound declared there: the unannotated default applies to each bare element, not to the bound as a whole. A rule written by hand for the type-variable case of `? extends` was tried first; every boundary with the rest of the checker needed an exception of its own, and the `? super` case was missed. The override check renames the overriding method's own type variables to the overridden method's before comparing, as javac does to decide override equivalence, so a `Box<V>` returned over a `Box<? extends V>` is judged with one `V`; without the renaming the path from one `V` to the other does not exist and every such override would be reported. At a generic method call, the constraint solver constrains the inference variable `U` of a `? extends U` to be `@Nullable` where the actual is an uncaptured, unannotated type variable whose bound is explicitly nullable and `U`'s own bound excludes null, which no solution satisfies, so the call reports an inference failure rather than an incompatible type after a `@NonNull` substitution. The solver also returns a third result, `UNCONSTRAINED`, for a variable that no constraint fixed and that a type variable which may be null flowed into, as `T` does into the `U` of `copyOf(Collection<? extends U>)` for a `List<T>`; a variable that no constraint fixed and no such type variable reached stays non-null, the least solution, as before. The substitution marks an unconstrained variable with a synthetic annotation that no check reads by name, so a check that reads annotations by name sees the type as unannotated, and the containment check reads the marker by identity and declines to judge the type, since its nullness is a type variable's that the substitution no longer names. A synthetic `@NonNull` therefore means what a written one means: a constraint, a library model, or stream propagation fixed the variable. Reading every synthetic `@NonNull` as unconstrained was tried in an earlier revision of this change; it let a `Box<? super @nullable String>` take the `Box<E>` that `id(boxOfObject)` returns, which the parent reports, and lost the modeled `? extends @NonNull` bound that `CustomLibraryModelsTests.nestedWildcards` pins. Caller sees: - `incompatible types: Box<T> cannot be converted to Box<? extends Object>` where `T`'s declared bound admits null; accepted before. - `incompatible types: Box<Object> cannot be converted to Box<? super T>` where `T`'s declared bound admits null, and the same for a `Box<? super Object>`; accepted before. - `incompatible types: Box<S> cannot be converted to Box<? extends T>` for `<S extends @nullable T>`, whatever `T`'s own bound, and `Box<T>` against `Box<? extends @nonnull T>` where `T`'s bound admits null; accepted before. - `incompatible types: Box<T> cannot be converted to Box<? extends Object>` for `<T extends @nullable Object & @nullable Serializable>`, since the elements of an intersection bound are read one by one and a bound with one non-null element excludes null, in null-marked and in unannotated code alike; accepted before, which apache#1836 reports for the other places that read a bound. - `mismatched type parameter nullability` on an override that returns `List<V>` where the overridden method returns `List<? extends @nonnull V>` and `V` may be null; accepted before. Caffeine's `AsyncCacheLoader.bulk` is such an override. - `incompatible types: List<T> cannot be converted to Collection<? extends Object>` on `List<Object> copy = new ArrayList<>(in)` with a `List<T>` whose `T` admits null, since the target fixes the constructor's `E` to be non-null; accepted before. `return first(b);` into an `Object` return type, with `<U extends @nullable Object> U first(Box<? extends U> b)`, is reported the same way, as `Box<T> cannot be converted to Box<? extends T>`, which is the message apache#1828 rewrites. - `inference failure: type variable U is constrained to be @nullable, but its upper bound requires it to be @NonNull` where a `Box<T>` whose `T` admits null is passed as the `Box<? extends U>` of a generic method whose `U` excludes null, such as `List.copyOf` with a `List<T>`; accepted before. Not changed: - A type variable declared in unannotated code with no `@Nullable` on its bound and no library model is not reported; NullAway stays optimistic about unannotated code. The library-model branch has no test: the modeled bounds belong to type parameters of library classes and methods, and no modeled generic method has a wildcard bounded by its own type variable for an override to meet. - A `Box<T>` whose `T` admits null passed as the `Box<? extends U>` of `<U extends @nullable Object> U first(Box<? extends U> b)` leaves `U` unconstrained where no target fixes it, and the dereference check reads that as non-null, so `first(b).toString()` is not reported. Constraining `U` to be nullable there would substitute `@Nullable T` where javac inferred `T` and report `List<T> copy = copyOf(in)`, since the solver infers one nullness per variable. - A captured actual's bare type variable is not reported: `return get();` with `get()` returning `Box<? extends T>` still passes as `Box<? extends Object>`. Such a capture cannot be told from one whose written `@NonNull` capture conversion dropped, as in Caffeine's `CacheLoader.asyncLoadAll`, and checking it reported that method. - `Box<? extends Object> x = idBox(box)` with `<U extends @nullable Object> Box<U> idBox(Box<U> b)` and a `Box<T>` whose `T` admits null is not reported: nothing fixes `U`, so the result is unconstrained and is not judged (apache#1945). `new HashSet<>(list)` and `copyOf(list)` into a `List<T>` keep compiling for the same reason. - A `Class<T>` with `T extends @nullable Object` still fits a `Class<?>` parameter such as the one of `Class.isAssignableFrom`; the declaration `Class<T>` is not reported either. - Dereferencing a `T` whose bound admits null, and passing a `@Nullable T` where a `T` is required, are still not reported; that is apache#1727. An invariant type argument is still compared by annotation only, so a `Box<@nonnull T>` is accepted where a `Box<T>` is required. Neither has a test in this change. Assisted-by: Claude Code (claude-fable-5-1)
|
#1972 now implements the same rule at the same call sites: I'm moving this PR to draft and will close it once #1972 lands, so the work stays on the #1932 track. One part has no counterpart in #1972: the bytecode fixtures in |
I'd prefer to include it in 1972, let me see |
Included in 765d255, you can review |
A type variable whose upper bound is an intersection, such as
<T extends @Nullable Object & @Nullable Serializable>, was read throughupperBound.getAnnotationMirrors(). javac stores type annotations on the elements of an intersection, and anIntersectionClassTypecannot carry one at all —cloneWithMetadatathrows — so that read answered "not@Nullable" for every intersection, whatever its elements said.Both directions were wrong. An all-
@Nullablebound came out null-exclusive, so NullAway stayed silent where it should report: a value read throughBox<?>for such aBox, and an override that narrows or widens the bound. At a generic method call the same read produced a false positive instead, rejecting a legal type argument withA bound read from a class file went the other way. Before JDK 22 javac drops these annotations from the symbol (JDK-8225377), and NullAway falls back on
Symbol.getRawTypeAttributes(). That fallback ignoredbound_indexand marked the whole type parameter@Nullablewhen any one bound carried the annotation, so<T extends @Nullable Object & Serializable>accepted a@Nullabletype argument it should refuse.What
JSpecify assigns a nullness operator to each element of an intersection separately and fixes the operator of the whole to
NO_CHANGE; a type variable has nullness-subtype-establishing edges to all the elements, so one non-null element makes the bound null-exclusive.GenericsUtils.isNullableAnnotatedstates that rule once, and the sites that decide a bound's nullability call it:upperBoundIsNullable, the class and generic-method type-argument checks, the override comparison, andgetTypeNullness.It reads
getExplicitComponents()rather thangetComponents(): an intersection whose bounds are all interfaces carries an implicitObjectsupertype the declaration did not write, and counting that one would make every such bound non-null.The raw-attributes fallback now collects the annotated bounds per type parameter and requires all of them. An annotation's type path is relative to the one bound its
bound_indexnames, so the index is resolved to that element beforeNullabilityUtil.isDirectTypeUseAnnotationreads the path — otherwise an annotation on a type argument inside the bound, or on the enclosing type of a nested-class bound (@Nullable Outer.Innerrather thanOuter.@Nullable Inner), counts as annotating the bound. That helper gained an overload taking the annotatedType; the existingSymbolform delegates to it.GenericsChecks.isNullableAnnotated(Type), which had the same name and the old intersection-blind body, is gone; its eight call sites use the shared helper.Verification
IntersectionBoundTestsis new: 19 tests over one rule, split so that each name carries its outcome. They cover an all-@Nullablebound, a non-null first element, a non-null later element, and no@Nullableelement at all, at each site that reads a bound — the class and generic-method type-argument checks, a read through a wildcard, and an override in both directions plus one that preserves the bound. Bytecode bounds come from three newtest-java-libfixtures and add the shapes only a class file has: an interface-only intersection, wherebound_indexstarts at 1; a three-element bound; a@Nullabletype argument nested inside a bound; and an annotation on the enclosing type of a nested-class bound.Seventeen of the nineteen were observed failing against a change that breaks the rule they guard: eleven with the four main-source files reverted to
master, and six more under one of three mutations —allMatchtoanyMatchover the intersection's elements, an intersection reported nullable unconditionally, and the annotation-mirror and raw-attributes paths disabled together. Two are guarded only against a compound regression, because the mirror path and the fallback cover each other:innerClassBoundFromBytecodeAcceptsNullableTypeArgumentneeds both broken at once, andoverrideRepeatingIntersectionBoundIsAcceptedis the equality control for the override check, which no single-site mutation makes disagree with itself.Two narrower mutations were cut for the parts of the fix the broad ones do not reach: neutering the
bound_indexresolution reddensannotationOnEnclosingTypeOfIntersectionElementFromBytecodeRefusesNullableTypeArgumentand nothing else, and replacinggetExplicitComponents()withgetComponents()reddensallNullableInterfaceOnlyBoundAcceptsNullableTypeArgumentOnGenericMethodCalland nothing else.:nullaway:testis green (1117 tests) and so is:nullaway:buildWithNullAway;IntersectionBoundTestsis also green under:nullaway:testErrorProneOldeston JDK 17.One converted site is not distinguished by any test: in
substitutedMethodTypeVarUpperBoundIsNullable, reverting the read of the substituted bound to the plain mirror read changes no verdict, because the method falls through to the original declaration's bound, which the same helper reads. The two reads would have to be reverted together to show a difference.One limit is worth stating, because it is not visible from the test names. These tests always pass
-XDaddTypeAnnotationsToSymbol=true, which is JDK-8225377's own workaround, so the annotation mirrors answer on every JDK the suite runs on and the raw-attributes fallback can only add to their verdict. What the bytecode tests pin is that the fallback adds nothing it should not; removing the fallback entirely fails no test in the suite, on either JDK.Scope
Two pre-existing gaps are untouched:
GenericsUtils.wildcardUpperBoundstamps a synthetic@Nullableon a formal type variable's bound when a library model or unannotated code makes it nullable, andtypeWithAnnotthrows on an intersection. This change narrows the reachable set — an all-@Nullableintersection is no longer stamped — but does not close it. I could not construct an input that reaches it.Summary by CodeRabbit
@Nullable.