Skip to content

Argument order suppresses an independent method-reference error after generic inference fails #2198

Backlog

Description

@msridhar

Kind of problem

False negative after #2175 lands. With generic inference failure warnings enabled, NullAway misses a method-reference parameter mismatch when the enclosing nested call precedes the argument that causes the contradiction. The same inference failure also produces false positives on a valid nested box(null) call.

Note that this false negative only appears when there is an inference error on an enclosing call, so some error is still always reported.

Minimal reproducer

Compile Test.java with NullAway and the configuration below (with the changes with #2175).

import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
class Test {
  interface Fn<A extends @Nullable Object, B extends @Nullable Object> {
    B apply(A a);
  }

  static class Box<T extends @Nullable Object> {}
  static <U> Box<U> wrapF(Fn<@Nullable String, U> f) { return new Box<>(); }
  static String needsNonNull(String s) { return s; }
  static <T> void takeTwo(Box<T> first, Box<String> second) {}
  static <T> void takeTwoRev(Box<String> first, Box<T> second) {}

  void after(Box<@Nullable String> first) {
    takeTwo(first, wrapF(Test::needsNonNull));
  }

  void before(Box<@Nullable String> first) {
    takeTwoRev(wrapF(Test::needsNonNull), first);
  }

  static <U extends @Nullable Object> Box<U> box(U value) { return new Box<>(); }
  static <T> void takeBoxRev(Box<@Nullable String> first, Box<T> second) {}

  void boxBefore(Box<@Nullable String> first) {
    takeBoxRev(box(null), first);
  }
}

Actual behavior

In before, no error is reported on Test::needsNonNull. The outer takeTwoRev call still reports an inference failure and a type mismatch for first. In after, the reference also gets its own parameter mismatch. Before #2175, NullAway reports that mismatch in both orders.

Test.java:17: error: [NullAway] inference failure: type variable T is constrained to be @Nullable, but its upper bound requires it to be @NonNull
    takeTwo(first, wrapF(Test::needsNonNull));
           ^
    (see http://t.uber.com/nullaway )
Test.java:17: error: [NullAway] incompatible types: Box<@Nullable String> cannot be converted to Box<String>
    takeTwo(first, wrapF(Test::needsNonNull));
            ^
    (see http://t.uber.com/nullaway )
Test.java:17: error: [NullAway] parameter s of referenced method is @NonNull, but parameter in functional interface method Test.Fn.apply(@Nullable String) is @Nullable
    takeTwo(first, wrapF(Test::needsNonNull));
                         ^
    (see http://t.uber.com/nullaway )
Test.java:21: error: [NullAway] inference failure: type variable T is constrained to be @Nullable, but its upper bound requires it to be @NonNull
    takeTwoRev(wrapF(Test::needsNonNull), first);
              ^
    (see http://t.uber.com/nullaway )
Test.java:21: error: [NullAway] incompatible types: Box<@Nullable String> cannot be converted to Box<String>
    takeTwoRev(wrapF(Test::needsNonNull), first);
                                          ^
    (see http://t.uber.com/nullaway )
Test.java:28: error: [NullAway] inference failure: type variable T is constrained to be @Nullable, but its upper bound requires it to be @NonNull
    takeBoxRev(box(null), first);
              ^
    (see http://t.uber.com/nullaway )
Test.java:28: error: [NullAway] incompatible types: Box<String> cannot be converted to Box<@Nullable String>
    takeBoxRev(box(null), first);
                  ^
    (see http://t.uber.com/nullaway )
Test.java:28: error: [NullAway] incompatible types: Box<@Nullable String> cannot be converted to Box<String>
    takeBoxRev(box(null), first);
                          ^
    (see http://t.uber.com/nullaway )
Test.java:28: error: [NullAway] passing @Nullable parameter 'null' where @NonNull is required
    takeBoxRev(box(null), first);
                   ^
    (see http://t.uber.com/nullaway )

Expected behavior

Report the parameter nullness mismatch at both occurrences of Test::needsNonNull. The functional interface can pass null, and the referenced method requires a non-null argument. The unrelated inference failure involving first should still be reported.

In boxBefore, accept box(null) as Box<@Nullable String>. Its type parameter permits null, and the formal parameter of takeBoxRev supplies a matching target type. Continue diagnosing the mismatch involving first.

NullAway configuration

-Xep:NullAway:ERROR
-XepOpt:NullAway:OnlyNullMarked=true
-XepOpt:NullAway:JSpecifyMode=true
-XepOpt:NullAway:JSpecifyExperimental=true
-XepOpt:NullAway:WarnOnGenericInferenceFailure=true
-XDaddTypeAnnotationsToSymbol=true

Additional context

Deferred from PR #2175, finding F27. The review proposes having the solver identify the inference variables connected to the contradiction, then sharing the failure only with sites that depend on those variables and solving the remaining constraints separately. That would let independent calls and references receive their own results in either argument order.

Related to #2132, which tracks duplicate diagnostics for the same inference failure. The missing method-reference diagnostic here concerns a separate nullness mismatch.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions