Skip to content

Generic method inference does not support inferring different nullability for multiple calls to same generic method #1291

Closed

Description

@msridhar

Test:

import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
public class Test {
  static <T extends @Nullable Object> T id(T t) {
    return t;
  }
  static <T extends @Nullable Object, U extends @Nullable Object> T chooseFirst(T t, U u) {
    return t;
  }
  static void test(@Nullable String s, String t) {
    String u = chooseFirst(id(t), id(s));
    // this is safe but we currently get an error
    u.hashCode();
  }
}

The issue is that within ConstraintSolverImpl we track inference state for type variables on a per-Element basis:

https://github.com/uber/NullAway/blob/2e578d6314e224a83114d3ff85571a33ab4b6035/nullaway/src/main/java/com/uber/nullaway/generics/ConstraintSolverImpl.java#L73-L73

But as the above test shows, we may need to infer different nullability for different calls to the same generic method. Probably, we need to refactor our code to track separate inference state for each (element,invocation) pair.

Activity

  1. added
    jspecify
    on Sep 16, 2025
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

    jspecify

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions