Skip to content

Report inference failures as errors by default #1551

Open

Description

@msridhar

After #1549, there are cases where inference fails but the default type inferred by javac does not lead to any NullAway errors being reported, for the case of captures, etc. See the test com.uber.nullaway.jspecify.WildcardTests#superWildcardToConcreteTypeVariable.

This exposes a deeper issue with our error reporting, which is that we do not report inference failures by default as an error. In most cases, we get away with this, as some other error gets reported by NullAway given the default @NonNull nullability for the type variables. But, this error message is a bit misleading (since we did not successfully infer @NonNull), and, as shown by the case above, it's a hack that doesn't always work.

The right solution here is to report inference failures as errors by default and to suppress warnings that are subsumed by the inference failure as best we can. We also need to improve our inference failure warning messages as right now they completely lack information.

Activity

  1. added a commit that references this issue on May 3, 2026
  2. changed the title [-]Explicitly default all type variables to `@NonNull` in the case of an inference failure[/-] [+]Report inference failures as errors by default[/+] on May 7, 2026
  3. msridhar commented on May 7, 2026

    @msridhar
    ContributorAuthor

    This comment is outdated and incorrect. We had an incorrect idea of how to fix this issue before, as seen below. Keeping it for the record.

    We should explicitly create a substitution and map all type variables to @NonNull to ensure errors get reported for such cases.

    Plan from Codex:

      1. Introduce a fallback substitution for InferenceFailure.
    
         Instead of returning javac’s call-site method type unchanged in substituteTypeArgsInGenericMethodType(...), build a conservative NullAway fallback where
         each method type variable is treated as @NonNull.
      2. Use that fallback only for downstream checking, not for reporting inference success.
    
         Keep InferenceFailure as a failure result, but when later code needs a method type for parameter compatibility or return/assignment checks, substitute
         failed method type vars with synthetic @NonNull type vars or otherwise annotate the call-site substitutions as non-null.
      3. Be careful with nullable upper bounds.
    
         Even if a method type variable has T extends @Nullable Object, the fallback should still probably choose @NonNull T for unknown inference, matching the
         existing successful-inference behavior for unconstrained variables. But this should be explicitly documented because it is a policy choice.
      4. Avoid duplicate diagnostics when warnings are enabled.
    
         If warnOnGenericInferenceFailure is true, we probably do not want both:
          - Failed to infer type argument nullability
          - plus a downstream parameter/assignment mismatch caused by fallback defaults
    
         We can either suppress downstream fallback diagnostics when the inference warning is emitted, or decide the concrete mismatch is better and stop reporting
         the generic inference warning for that call.
      5. Add tests for both modes.
    
         For the new wildcard test:
          - With inference-failure warnings enabled, assert the current inference-failure diagnostic or the chosen replacement diagnostic.
          - With warnings disabled, assert a concrete parameter/assignment nullability error still appears.
      6. Keep fallback local to failed inference.
    
         Don’t change the successful inference path. The existing putIfAbsent(..., NONNULL) default for unconstrained variables should remain as-is.
    
  4. vlsi commented on Oct 22, 2026

    @vlsi
    Contributor

    Status on master (5023d67):

    So what remains here is enabling the reporting by default, presumably once wildcard support and JDK models make the benchmarks quiet enough. Keeping this open to track that.

    Reproducer (NullAway 5023d67, JDK 21)

    Flags: -XepOpt:NullAway:OnlyNullMarked=true -XepOpt:NullAway:JSpecifyMode=true -XDaddTypeAnnotationsToSymbol=true

    import org.jspecify.annotations.NullMarked;
    import org.jspecify.annotations.Nullable;
    import java.util.List;
    @NullMarked
    class Test {
      static <T extends @Nullable Object> void pair(List<T> a, List<T> b) {}
      void test(List<@Nullable String> nullableList, List<String> nonNullList) {
        pair(nullableList, nonNullList);
      }
    }

    Observed: incompatible types: List<@Nullable String> cannot be converted to List<String> on the pair(...) call. Expected: inference failure: type variable T constrained to be both @NonNull and @Nullable, which is what NullAway reports with -XepOpt:NullAway:JSpecifyExperimental=true.

    The wildcard case from WildcardTests#superWildcardToConcreteTypeVariable behaves the same way with -XepOpt:NullAway:HandleWildcardGenerics=true and no experimental mode: field = get(nullableBox) gets assigning @Nullable expression to @NonNull field rather than the inference failure.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions