You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Report inference failures as errors by default #1551
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.
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
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.
In plain JSpecify mode the failure is still silent, and NullAway reports a downstream error assuming @NonNull for the type variable instead. Enable inference failure warnings by default #1568, which made reporting unconditional, was closed because of the number of new warnings on real-world code.
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.
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.
Activity
Initial inference for wildcards (#1549)
[-]Explicitly default all type variables to `@NonNull` in the case of an inference failure[/-][+]Report inference failures as errors by default[/+]msridhar commentedon May 7, 2026
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
@NonNullto ensure errors get reported for such cases.Plan from Codex:
vlsi commentedon Oct 22, 2026
Status on master (5023d67):
-XepOpt:NullAway:JSpecifyExperimental=trueis set;WarnOnGenericInferenceFailuredefaults to that flag.@NonNullfor the type variable instead. Enable inference failure warnings by default #1568, which made reporting unconditional, was closed because of the number of new warnings on real-world code.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=trueObserved:
incompatible types: List<@Nullable String> cannot be converted to List<String>on thepair(...)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#superWildcardToConcreteTypeVariablebehaves the same way with-XepOpt:NullAway:HandleWildcardGenerics=trueand no experimental mode:field = get(nullableBox)getsassigning @Nullable expression to @NonNull fieldrather than the inference failure.