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

JSpecify mode: @Nullable on a type argument that has type arguments is not checked against the bound #2227

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 @Nullable type argument is checked against the bound of its type parameter only when it has no type arguments itself.

import java.util.List;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
class Test {
  static class Hasher<T> {
    int hash(T value) { return value.hashCode(); }
  }

  static int plainArgument() {
    return new Hasher<@Nullable String>().hash(null); // reported, as expected
  }

  static int parameterizedArgument() {
    return new Hasher<@Nullable List<String>>().hash(null); // not reported
  }

  public static void main(String[] args) {
    parameterizedArgument(); // 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 type argument in plainArgument:

Test.java:12: error: [NullAway] Generic type parameter cannot be @Nullable, as type variable T of type Test.Hasher does not have a @Nullable upper bound

It should report the same error in parameterizedArgument, because T has no @Nullable bound. That method throws a NullPointerException at run time.

The same happens for a field, parameter, superclass, bound or explicit method type argument, and for qualified names such as java.util.@Nullable List<String>. An annotated array, List<String> @Nullable [], is reported.

#1946 is the same check for a type variable as the argument.

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

Activity

  1. msridhar commented on Dec 2, 2026

    @msridhar
    Contributor

    Thanks @aosen-xiong for this and #2228. We do still have many gaps in our JSpecify mode checking, as you can see from the number of open issues we have 😅 If you discover a lot more issues like this with your tools, it'd be best for us to sync separately on how best to track them. Submitting each of them as a separate issue might be overwhelming. But we do appreciate the reports!

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