Skip to content

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

@msridhar

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:

private final Map<Element, VarState> vars = new HashMap<>();

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    jspecifyRelated to support for jspecify standard (see jspecify.dev)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions