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
Summary
Add support for nullability inference of type variables declared by generic constructors when a constructor call uses the diamond operator.
Rationale
GenericsChecks.generateConstraintsForCallcurrently registers type variables returned bygetCallTypeParameters(callTree). For aNewClassTree, 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.javaNewClassTreediamond callsRequired changes
NewClassTreecalls, register the constructor type variables fromgetMethodSymbolForCall(callTree).getTypeParameters()withConstraintSolver.Acceptance criteria
Backlinks
Requested by: @msridhar