Skip to content

Enforce receiver-substituted bounds in generic method-reference inference #2181

Open

Description

@coderabbitai

Summary

Generic method-reference inference may accept an inferred nullable type argument that violates a bound after substituting the receiver's type arguments. The review reported this case; it has not been independently reproduced for this issue.

For example, in JSpecify mode:

import java.util.function.Supplier;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
abstract class Example {
  static class Box<T extends @Nullable Object> {}

  abstract static class Factory<X extends @Nullable Object> {
    abstract <T extends X> Box<T> box();
  }

  static void accept(Supplier<Box<@Nullable String>> supplier) {}

  static void check(Factory<String> nonNull, Factory<@Nullable String> nullable) {
    accept(nonNull::box);  // The receiver's bound for T is non-null.
    accept(nullable::box); // The receiver's bound for T allows null.
  }
}

The reported concern is that inference registers the declared method type parameter without applying the receiver substitution to its bound. It may then infer T as nullable for the first reference, despite the non-null receiver-substituted bound.

Required change

In nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java and the associated constraint-solving code, enforce the method type parameter's receiver-substituted upper bound during omitted-type-argument inference for method references. Preserve the behavior for bounds that allow nullable type arguments.

Acceptance criteria

  • Add a regression test for an inline generic method reference whose receiver makes the method type parameter's upper bound non-null. When the target requires a nullable type argument, report an inference failure with WarnOnGenericInferenceFailure enabled.
  • Add a control using a nullable receiver type argument. Accept the reference when the target and substituted bound permit nullable inference.
  • Check that inference and compatibility behavior remains appropriate when inference warnings are disabled.

Requested by @msridhar as follow-up to #2175.

Review comment: #2175 (comment)

Activity

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

    @msridhar
    Contributor

    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

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions