Skip to content

Nullability inference ignores T extends U when both type variables are inferred #2199

Backlog

Description

@msridhar

Kind of problem

False negative. NullAway accepts a nullable T with a non-null U when it infers both type arguments for a method that declares T extends U. A direct call and an inline method reference passed to a generic method already missed the error before #2175. With standalone inference for inline method references in #2175, an inline reference passed to a non-generic method also misses the error. The accepted reference below passes null to a Consumer<String>.

Minimal reproducer

Compile InlineBound.java with NullAway using the options below, then run InlineBound. The sample uses JSpecify annotations 1.0.1.

import java.util.function.BiFunction;
import java.util.function.Consumer;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
public class InlineBound {
  static <U extends @Nullable Object, T extends U> T forward(T value, Consumer<U> consumer) {
    consumer.accept(value);
    return value;
  }

  static void rejectsNull(BiFunction<@Nullable String, Consumer<String>, @Nullable String> fn) {
    fn.apply(null, InlineBound::length);
  }

  static void acceptsNull(
      BiFunction<@Nullable String, Consumer<@Nullable String>, @Nullable String> fn) {
    fn.apply(null, value -> {});
  }

  static void length(String value) {
    value.length();
  }

  public static void main(String[] args) {
    acceptsNull(InlineBound::forward);
    rejectsNull(InlineBound::forward);
  }
}

To check the direct call and the method reference passed to a generic method, add these methods inside InlineBound and compile again.

static void directCall(Consumer<String> c) {
  forward(null, c);
}

static <X extends @Nullable Object> void rejectsNullGeneric(
    BiFunction<@Nullable String, Consumer<String>, X> fn) {}

static void viaGeneric() {
  rejectsNullGeneric(InlineBound::forward);
}

Actual behavior

No error reported on rejectsNull(InlineBound::forward) at the #2175 head, with WarnOnGenericInferenceFailure either enabled or disabled. The base reports this diagnostic on the same reference:

InlineBound.java:28: [NullAway] parameter value of referenced method is @NonNull, but parameter in functional interface method java.util.function.BiFunction.apply(@Nullable String,Consumer<String>) is @Nullable

The #2175 review also reports no diagnostic on forward(null, c) or rejectsNullGeneric(InlineBound::forward) on either the base or the PR branch. Reproducer and results from the review

Running the original program accepted by #2175 throws:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "<parameter1>" is null
	at InlineBound.length(InlineBound.java:23)
	at InlineBound.forward(InlineBound.java:9)
	at InlineBound.rejectsNull(InlineBound.java:14)
	at InlineBound.main(InlineBound.java:28)

Expected behavior

Report a nullness error on forward(null, c) and on the references in rejectsNull(InlineBound::forward) and rejectsNullGeneric(InlineBound::forward). For rejectsNull, report the error with either warning setting. Each call requires a nullable T and a non-null U, which violates T extends U. Accept the reference in acceptsNull(InlineBound::forward), where both type arguments can be nullable. JSpecify nullness subtyping

NullAway configuration

-XepOpt:NullAway:OnlyNullMarked=true
-XepOpt:NullAway:JSpecifyMode=true
-XepOpt:NullAway:JSpecifyExperimental=true
-XDaddTypeAnnotationsToSymbol=true

Ran the same sample with -XepOpt:NullAway:WarnOnGenericInferenceFailure=false and -XepOpt:NullAway:WarnOnGenericInferenceFailure=true.

NullAway version

PR #2175 head 899008c74a4274a7e06fdcc8f309863e8fb0cd7e, compared with base af5ba086ef4859e4d660ac61bc5de336b8cbb89a.

Error Prone version

2.53.0

JDK version

Temurin 25.0.5.1+2

Additional context

The solver rebinds the declared upper bound when it registers fresh inference variables. It needs to enforce T <: U so a nullable T forces U to be nullable. One place to add this constraint is ConstraintSolverImpl.registerInferenceVariables, after both variables are registered.

Related to #2181, which covers bounds that depend on a type argument fixed by the receiver. This case uses a static method and a bound between two type variables inferred together.

Constraint solver stack

  • #2084 still misses the direct call and the method reference passed to a generic method.
  • #2086 adds the T <: U edge during constraint preparation. It catches the direct call and the joint-inference case, but still rejects the valid standalone reference in acceptsNull(InlineBound::forward).
  • #2098 routes standalone method references through the solver. At its head (f7f01eaea) and at the stack tip #2102 (a2af5caaa), NullAway reports a diagnostic on all three unsafe calls and accepts the valid reference. The repro gives that result with WarnOnGenericInferenceFailure enabled and disabled.

So this should be fixed by #2098.

Activity

  1. added
    jspecify
    on Nov 28, 2026
  2. added this to the v0.14.5 milestone on Nov 28, 2026
  3. changed the title [-]Inline generic method reference inference accepts a nullable type argument that violates T extends U[/-] [+]Nullability inference ignores T extends U when both type variables are inferred[/+] on Dec 2, 2026
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

    bugfalse negativejspecify

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions