Skip to content

Suggest a declaration change for a generic nullability mismatch only where applying it removes the error #2216

Backlog

Description

@vlsi

Problem or use case

When NullAway reports a generic nullability mismatch, the reader has to decide which declaration to change, and to what type. Usually one of two changes is right: the source's wildcard loses its implicit @Nullable bound, or the target accepts @Nullable. Writing either type out by hand is error-prone, for example List<? extends Object> versus Collection<? extends @Nullable Object>. Which of the two is right depends on whether the source can hold null, and NullAway does not know that.

A prototype in #1828 printed both candidates and attached an Error Prone edit for the source side of a local variable:

incompatible nullability in type argument for E of Collection -> upper bound of wildcard: source has @Nullable Object where target requires Object
    source: List<?>
                 ^
    target: Collection<? extends Object>
                                 ^^^^^^
    note: the unbounded wildcard ? in List<?> has the upper bound of type parameter E of List, which is @Nullable Object (JLS 5.1.10)
    possible fixes:
      if list never holds null: declare list as List<? extends Object>
      if it may: declare c as Collection<? extends @Nullable Object>
  Did you mean 'List<? extends Object> list = new ArrayList<String>();'?

The fix lines and the edit were removed from #1828, and the rest of the message stays there. The reason: each review round found a program where applying the offered fix compiles but moves the error to another line, or does not compile. The prototype checks the rewritten declaration against the reported pseudo-assignment, the local's initializer, and its assignments. A rewrite is safe only if every use of the variable still accepts the new type, and each round found a use the check did not cover:

  • the initializer or a later assignment rejects the rewritten type (List<?> l = make(); with make() returning List<?>);
  • the value's type is not known without inference that has not run yet: a conditional, a lambda, a method reference, a generic call;
  • a pattern variable, which can be reassigned;
  • a lambda parameter or a parameter of an overriding method: the rewrite compiles, and NullAway then reports the override (Parameter has type ..., but overridden method has parameter type ..., which has mismatched type parameter nullability);
  • a read through ? super that accepted the old type and rejects the new one (keep(List<? super List<?>>) after l becomes List<List<? extends Object>>);
  • a fix text that names a shadowed type (Object with a nested class Object {} in scope), or drops an annotation it cannot print (a custom type-use nullable, @Nullable("reason"), an annotated wildcard).

The tag has tests for the first four kinds and for the annotation cases; they pass there. The ? super read and the shadowed name were found by review and have no test at the tag. For the overriding method, the tag withholds the fix only for a lambda parameter, and the method-parameter case was found by review and not run.

Example and expected behavior

For each generic mismatch where NullAway offers a declaration change, applying the change exactly as printed yields a program that javac compiles and that NullAway accepts, at the reported line and at every other use of the rewritten variable. Where NullAway cannot show that, it offers no change.

The tag carries a round-trip test, FixRoundTripTests, that states this as an executable check. For each program in its corpus, the test:

  1. applies every offered line and edit;
  2. recompiles;
  3. asserts that the result compiles and that NullAway reports no generic mismatch or override error on the lines it checks.

A fix that meets the criterion passes this test over a corpus that also contains the cases listed above.

Proposed solution

The prototype is the annotated tag nullaway-1828-possible-fix in vlsi/NullAway, commit 1759a84. The fix code is in NullabilityMismatchMessage (sourceTypeFix, targetTypeFix, valuesAccept, addSourceFixEdit) and in LocalVariableDeclarations. Its tests are FixTests, SourceFixTests, TargetTypeFixTests, and FixRoundTripTests under nullaway/src/test/java/com/uber/nullaway/jspecify/mismatchmessage/.

One possible shape, untested: offer a change only for a local whose every use NullAway can recheck against the rewritten type. That means rerunning the generic checks of the local's method with the declaration replaced, rather than predicting the result from a list of value kinds. The cost is a second check of the method per offered fix, and it has to stay within the rule in CLAUDE.md against repeating work per lookup.

Additional context

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions