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

JSpecify mode: inference substitutes a synthetic @NonNull for an unconstrained type variable, so a List<T> with T extends @Nullable Object flows into a List<Object> #1945

Open

Description

@vlsi

With JSpecify mode on, new ArrayList<>(in) with in a List<T> and T extends @Nullable Object is accepted where a List<Object> is required, so the caller below throws NullPointerException at copy(in).get(0).toString() with no diagnostic anywhere. The same List<T> passed directly to a Collection<? extends Object> parameter is reported once #1834 is applied; the diamond, and any generic method whose type variable is instantiated by that argument, are not.

Versions: NullAway master at 2b25d0f (0.14.3-SNAPSHOT), the 0.14.2 release, and the head of #1834 (b84ffe7). Error Prone 2.50.0, JDK 21.0.9. Flags: -XepOpt:NullAway:JSpecifyMode=true -XDaddTypeAnnotationsToSymbol=true -XepOpt:NullAway:JSpecifyExperimental=true -XepOpt:NullAway:OnlyNullMarked=true.

Code sample. I ran it as a test in nullaway/src/test/java/com/uber/nullaway/jspecify/, with the helper WildcardTests uses: makeTestHelperWithArgs(JSpecifyJavacConfig.withJSpecifyModeArgs(Arrays.asList("-XepOpt:NullAway:OnlyNullMarked=true"))).

import java.util.ArrayList;
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 extends @Nullable Object> Box<U> idBox(Box<U> b) {
    return b;
  }
  static <T extends @Nullable Object> List<Object> copy(List<T> in) {
    return new ArrayList<>(in);
  }
  static <T extends @Nullable Object> void boxed(Box<T> box) {
    Box<? extends Object> x = idBox(box);
  }
  static void caller() {
    List<@Nullable String> in = new ArrayList<>();
    in.add(null);
    copy(in).get(0).toString();
  }
}

Expected: a diagnostic on the return new ArrayList<>(in); line (line 12) and on the Box<? extends Object> x = idBox(box); line (line 15), of the form #1834 gives a Box<T> passed straight to a Box<? extends Object> parameter:

incompatible types: Box<T> cannot be converted to Box<? extends Object>

Under JSpecify subtyping a bare T whose declared bound is @Nullable Object is not null-exclusive, so List<T> is contained by Collection<? extends E> only where E admits null; ArrayList<Object> has E as Object, and Box<? extends Object> requires the same of the U that idBox returns. The caller method shows the consequence: T is instantiated as @Nullable String, which NullAway accepts, and the List<Object> that comes back holds a null.

Actual: no diagnostic on any line, on master, on 0.14.2, and with #1834 applied. With a // BUG: Diagnostic contains: incompatible types marker inserted above line 12, which shifts it to line 13, the test fails with:

Did not see an error on line 13 matching incompatible types. There were no errors.

Analysis, a guess; the report stands without it. For the diamond and for idBox, javac infers the type variable as T and NullAway's ConstraintSolverImpl infers its nullness separately. Nothing in the call constrains it: T is neither @Nullable nor known non-null, so the variable stays UNKNOWN, and solve() maps UNKNOWN to NONNULL ("we infer NONNULL arbitrarily", ConstraintSolverImpl.solve). TypeSubstitutionUtils.substituteInferredNullabilityForTypeVariables then stamps a synthetic @NonNull on the variable, so the checks see ? extends @NonNull E on the formal and Box<@NonNull U> on the result, and a bare T passes against both. #1834 has to read that synthetic @NonNull as "no decision" to keep List<T> copy = copyOf(in) and new HashSet<>(in) compiling, which is correct for those and is what hides this case.

A result with two values cannot express what javac inferred here: the nullness of E is the nullness of T, whatever T is instantiated as. A third result, "same as the javac-inferred type" (the specification's NO_CHANGE), would substitute no annotation at all, and the containment check would then judge T against ? extends Object by its declared bound, as it does for the direct call. The dereference first(b).toString() with <U extends @Nullable Object> U first(Box<? extends U> b) goes unreported for the same reason, but it also meets the bare-T dereference gap of #1727, so it is not the reproducer here.

Related: #1834 reports the direct, non-inferred shape and names this gap in its description. #1551 asks for inference failures to be reported by default, where the default @NonNull hides an error; this report is the case where inference does not fail and the default still hides one. #1585 proposes mapping type variables to full nullability-enhanced types, which is where a NO_CHANGE result would live. #1727 is the NO_CHANGE operator on the dereference and sink side.

Assisted-by: Claude Code (claude-fable-5-1)

Activity

  1. vlsi commented on Oct 9, 2026

    @vlsi
    ContributorAuthor

    The current revision of #1834 changes what this report describes. The solver returns UNCONSTRAINED only for a variable that no constraint fixed and that a type variable which may be null flowed into; a variable that only a non-null argument reached stays non-null, the least solution. The containment check declines to judge an unconstrained variable and judges a fixed one.

    With that, line 12 of the sample, return new ArrayList<>(in); into List<Object>, is reported: the target fixes the constructor's E to be non-null, and List<T> is then judged against Collection<? extends Object>. Line 15, Box<? extends Object> x = idBox(box);, is not: nothing fixes U, so the result is unconstrained, and the check cannot tell U := T from U := Object after javac's substitution. The dereference variant first(b).toString() stays unreported for the same reason, and meets the bare-T dereference gap of #1727 as well.

    So what remains of this report is the idBox line: a result the solver could only express as "the nullness of T" needs the mapping from type variables to full types that #1585 proposes.

    Assisted-by: Claude Code (claude-fable-5-1)

  2. added
    jspecify
    on Oct 22, 2026
  3. vlsi commented on Nov 25, 2026

    @vlsi
    ContributorAuthor

    The same gap with a generic method in place of the diamond: the target fixes U as non-null, and a Box<T> whose T admits null passes through ? extends U unreported.

    @NullMarked
    class Test<T extends @Nullable Object> {
      static class Box<E extends @Nullable Object> {}
      static <U extends @Nullable Object> List<U> copy(Box<? extends U> b) {
        throw new UnsupportedOperationException();
      }
      static void takeObjects(List<Object> objects) {}
      void contextFixesU(Box<T> b) {
        List<Object> c = copy(b); // no report
        takeObjects(copy(b));     // no report
      }
    }

    No diagnostic on master at 594cf3d, on the head of #1955 (4a8cd89), or on the head of #2101 (f9f883e), with the helper WildcardTests uses. #1955 reports the form where U's own bound excludes null (<U> List<U> copy(Box<? extends U> b)), and #2101 the forms where T reaches U through another call, but neither covers a U that only the target makes non-null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    false negativejspecify

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions