Skip to content

Inline generic method reference inference accepts a nullable type argument that violates T extends U #2199

Backlog

Description

@msridhar

Kind of problem

False negative. #2175 introduces standalone inference for inline generic method references. With that change, NullAway accepts a reference that passes null to a Consumer<String> through a method with T extends U.

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);
  }
}

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

Running the accepted program 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 the method reference in rejectsNull(InlineBound::forward) with either warning setting. That target 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

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.

Activity

  1. added
    jspecify
    on Nov 28, 2026
  2. added this to the v0.14.5 milestone on Nov 28, 2026
  3. msridhar commented on Nov 29, 2026

    @msridhar
    ContributorAuthor

    TODO: check if the solver rewrite fixes this one

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