Skip to content

Support nullability inference for generic constructors in diamond calls #1825

Description

@coderabbitai

Summary

Add support for nullability inference of type variables declared by generic constructors when a constructor call uses the diamond operator.

Rationale

GenericsChecks.generateConstraintsForCall currently registers type variables returned by getCallTypeParameters(callTree). For a NewClassTree, these are class type variables. A constructor can also declare its own type variables. The constraint solver treats unregistered type variables as fixed types, so constructor type variables in formal parameter types do not participate in nullability inference.

Affected area

  • nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java
  • Generic nullability inference for NewClassTree diamond calls

Required changes

  • For NewClassTree calls, register the constructor type variables from getMethodSymbolForCall(callTree).getTypeParameters() with ConstraintSolver.
  • Keep registration of class type variables. These variables remain necessary for constructed-result substitution.
  • Add regression coverage for a diamond constructor call where the constructor declares its own type variables and nullability inference requires those variables.

Acceptance criteria

  • Constructor-declared type variables participate in constraint generation and solving for diamond constructor calls.
  • Existing class type-variable inference for diamond constructor calls remains unchanged.
  • A regression test demonstrates the expected result for a generic constructor with constructor-declared type variables.

Backlinks

Requested by: @msridhar

Activity

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

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions