Skip to content

JSpecify mode: a type argument is not checked against the type arguments inside its bound #2228

Backlog

Description

@aosen-xiong

Found during differential testing, which uses the Lean framework and Specimen to generate well-typed programs and compares the verified Lean checker with NullAway.

A type argument is checked only against whether its bound is @Nullable, not against the type arguments inside the bound.

import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
class Test {
  static class Box<T extends @Nullable Object> {
    private T item;
    Box(T item) { this.item = item; }
    T get() { return item; }
    void set(T next) { item = next; }
  }

  static class Cleaner<B extends Box<@Nullable String>> {
    void clear(B box) { box.set(null); }
  }

  static void assign(Box<String> box) {
    Box<@Nullable String> nullableBox = box; // reported, as expected
  }

  static void clear(Box<String> box) {
    new Cleaner<Box<String>>().clear(box); // not reported
  }

  public static void main(String[] args) {
    Box<String> box = new Box<String>("text");
    clear(box);
    box.get().length(); // throws NullPointerException
  }
}
javac -XDaddTypeAnnotationsToSymbol=true -Xplugin:"ErrorProne -XepDisableAllChecks -Xep:NullAway:ERROR -XepOpt:NullAway:OnlyNullMarked=true -XepOpt:NullAway:JSpecifyMode=true" Test.java

NullAway reports only the assignment:

Test.java:18: error: [NullAway] incompatible types: Box<String> cannot be converted to Box<@Nullable String>

It should report an error on new Cleaner<Box<String>>(), because Box<String> is not within the bound Box<@Nullable String>. The program throws a NullPointerException at run time.

The same happens for a field, parameter, superclass, bound or explicit method type argument, and for the opposite mistake, Reader<Box<@Nullable String>> with B extends Box<String>. An inferred call such as clearStatic(box) for <B extends Box<@Nullable String>> void clearStatic(B box) is not reported either. The open pull requests #2086, #2098 and #2099 may cover that case, but not written type arguments.

NullAway 0.14.4 and master 1bf8910, Error Prone 2.53.0, JDK 26.0.1.

Activity

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions