Skip to content

WarnOnGenericInferenceFailure reports an inference failure for a @Nullable argument passed to a generic method of @NullUnmarked code #2125

Open

Description

@Stephan202

(This bug report was generated by Claude Sonnet 5.5 with "high" effort. See also PicnicSupermarket/error-prone-support#2299 (comment).)

With JSpecifyExperimental, passing a @Nullable argument to a generic method of unmarked code, with the result flowing into a @NonNull location (a return, a field, or a method argument), reports an inference failure. Unmarked code is otherwise treated optimistically: it accepts null, and its return values are assumed to be non-null. A real-world example is Refaster.emitCommentBefore("REPLACEME", null) (Error Prone, <T> T emitCommentBefore(String, T)), whose result is passed to a @NullMarked method.

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

@NullMarked
class Repro2 {
  @NullUnmarked
  static class Lib {
    static <T> T id(T t) {
      throw new RuntimeException();
    }
  }

  String test(@Nullable String s) {
    return Lib.id(s);
  }
}

Options: -XepOpt:NullAway:OnlyNullMarked=true -XepOpt:NullAway:JSpecifyMode=true -XepOpt:NullAway:JSpecifyExperimental=true

Repro2.java:15: warning: [NullAway] inference failure: type variable T constrained to be both @NonNull and @Nullable
    return Lib.id(s);
                 ^

Reproduced with NullAway 0.14.3 and with master (002c08d), Error Prone 2.51.0, JDK 25. No diagnostic is reported with -XepOpt:NullAway:WarnOnGenericInferenceFailure=false.

Observations:

  • The same failure is reported for fld = Lib.id(s); (a String field) and take(Lib.id(s)) (for static String take(String s)).
  • String result = Lib.id(s); is not reported, nor is later returning, passing or dereferencing result, nor is Lib.id(s).trim(). I presume this is because the assignment does not constrain the top-level type of a local variable (see ConstraintSolver#addSubtypeConstraint, localVariableType).
  • Nested types are still constrained for locals, so there the local is no workaround: with static <T> List<T> listOf(T t) added to Lib, List<String> result = Lib.listOf(s); is reported with the same message.
  • Passing null rather than a @Nullable variable behaves the same, as does a callee in a class outside of AnnotatedPackages rather than a @NullUnmarked one.
  • If Lib is @NullMarked, the call is reported with both this inference failure and the regular "passing @nullable parameter" error, which seems right. For unmarked code I'd expect no diagnostic, or perhaps that inference is not attempted at all; is the current behavior intended?

Activity

  1. added
    in-the-wild
    on Nov 18, 2026
  2. added this to the v0.14.5 milestone on Nov 21, 2026
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

    in-the-wild

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions