Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

JSpecify mode: a Box<? extends T> whose T admits null reaches a ? extends U with a non-null U unreported #2189

In progress

Description

@vlsi

With JSpecify mode on and #1955 applied, requireNonNull(box) with a Box<T> whose T is declared T extends @Nullable Object is reported as an inference failure against <U> void requireNonNull(Box<? extends U> b), but the same call with a Box<? extends T> is accepted, whether the wildcard is written in a parameter type or comes from a call that returns Box<? extends T>. List.copyOf(wildList(t)) with a List<? extends T> wildList(T t) is accepted too, although List.copyOf throws NullPointerException "if coll is null, or if it contains any nulls" (its Javadoc), and a List<? extends T> may hold a null T.

Versions: NullAway 0.14.5-SNAPSHOT with #1955 at ba2f5b721, which checks only a bare T, Error Prone 2.53.0, JDK 21.0.9. Flags: JSpecifyJavacConfig.withJSpecifyModeArgs plus -XepOpt:NullAway:OnlyNullMarked=true, the helper WildcardTests uses.

Code sample, run as a test in nullaway/src/test/java/com/uber/nullaway/jspecify/:

import java.util.List;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
class Test {
  static class Box<E extends @Nullable Object> {}
  static <U> void requireNonNull(Box<? extends U> b) {}
  static <T extends @Nullable Object> Box<? extends T> wildBox(T t) { throw new RuntimeException(); }
  static <T extends @Nullable Object> List<? extends T> wildList(T t) { throw new RuntimeException(); }
  static <T extends @Nullable Object> void f(Box<T> box, Box<? extends T> wild, T t) {
    requireNonNull(box);
    requireNonNull(wild);
    requireNonNull(wildBox(t));
    List.copyOf(wildList(t));
  }
}

The same holds for a capture of an unbounded ? whose declared bound reaches T: nonNullVariable(h.box()) with h of type BoundedByANullableVariable<T, ?>, class BoundedByANullableVariable<T extends @Nullable Object, X extends T> and Box<X> box().

Expected: the diagnostic that line 11 gets, also on lines 12, 13, and 14 (on line 14 it names E of List.copyOf):

inference failure: type variable U of Test.requireNonNull is constrained to be @Nullable, but its upper bound requires it to be @NonNull

A wildcard whose own type parameter excludes null holds no null and must stay unreported: Sub<? extends T> passed for Base<? extends U> with class Sub<X> extends Base<X>, List.copyOf of a NonNullList<? extends T> with class NonNullList<X> extends ArrayList<X>, and Pair<S, ? extends X> passed for Pair<U, ? extends U> with class Pair<A extends @Nullable Object, B extends A> and <S, X extends @Nullable S>. WildcardTests.inferenceCannotFitATypeVariableWhoseBoundAdmitsNullIntoANonNullVariable holds these as silent methods.

Actual: only line 11 is reported:

/Test.java:11: warning: [NullAway] inference failure: type variable U of Test.requireNonNull is constrained to be @Nullable, but its upper bound requires it to be @NonNull; the type argument T passed for ? extends U admits null, since T of Test.f has a @Nullable upper bound

On master none of the four lines is reported; line 11 comes from #1955.

Analysis, a guess; the report stands without it. #1955 checks only an unannotated, uncaptured type variable passed for ? extends U. An earlier revision of #1955 also checked wildcard and captured actuals by their bound, and reported the three silent cases above: it judged a wildcard by the type parameter of the formal's class after asSuper, and fell back to the declared bound where a sibling type argument was a type variable. Deciding it needs the type parameter the wildcard was written for, in the actual's own class with that class's arguments substituted. At the head of #2101 (f9f883e7d), a run of the same shapes reports lines 12–14, and also reports the three cases above that must stay unreported.

Related: #1955 (the check for a bare T), #2101 (the constraint-solver stack that checks wildcard obligations when U is resolved), #1947 (a different gap: T reaches List.copyOf through another generic call), #1932.

Assisted-by: Claude Code (claude-opus-5-5)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions