Skip to content

Check nested nullness for functional interface implementations - #1739

Draft
msridhar wants to merge 2 commits into
masterfrom
more-nested-nullness-override-checks
Draft

msridhar wants to merge 2 commits into
masterfrom
more-nested-nullness-override-checks

Conversation

@msridhar

@msridhar msridhar commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

This closes an important gap in checking, but it causes new errors, and we have other high-priority issues to fix, so going to hold off on landing for a bit

@msridhar

msridhar commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor Author

@codecov

codecov Bot commented Aug 24, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.36364% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.42%. Comparing base (009d7cb) to head (c01b57f).
⚠️ Report is 14 commits behind head on master.

Files with missing lines Patch % Lines
...ava/com/uber/nullaway/generics/GenericsChecks.java 79.16% 0 Missing and 5 partials ⚠️
...java/com/uber/nullaway/generics/GenericsUtils.java 87.50% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1739      +/-   ##
============================================
- Coverage     89.44%   89.42%   -0.03%     
- Complexity     4009     4024      +15     
============================================
  Files           113      113              
  Lines         13148    13169      +21     
  Branches       2723     2734      +11     
============================================
+ Hits          11760    11776      +16     
  Misses          624      624              
- Partials        764      769       +5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@msridhar
msridhar force-pushed the more-nested-nullness-override-checks branch 7 times, most recently from 326ff97 to 4a15623 Compare August 29, 2026 20:48
@msridhar
msridhar force-pushed the more-nested-nullness-override-checks branch 2 times, most recently from 5bdd31d to 39c5b34 Compare September 9, 2026 21:33
@msridhar
msridhar force-pushed the more-nested-nullness-override-checks branch from 39c5b34 to dfe8279 Compare September 17, 2026 23:55
@msridhar
msridhar force-pushed the more-nested-nullness-override-checks branch from dfe8279 to d9b2857 Compare October 4, 2026 22:54
msridhar added a commit that referenced this pull request Oct 7, 2026
Fixes #1919 

This issue came up while working on #1739. For cases like the test of
#1919, we would sometimes fall back on the `javac`-computed type of an
implicitly-typed lambda parameter. E.g., for this code (from #1919):
```java
  static void implicitParameter(Box<Box<@nullable String>> nested) {
    var flat = nested.flatMap(box -> box);
    acceptsNullable(flat);
  }
```
The type of `box` should be `Box<@nullable String>` based on the
declaration of `nested`, but `javac` computes it as `Box<String>`,
causing a spurious warning.

To fix, store correct types of such parameters in a new map
`lambdaParameterTypesForInference` during inference for the lambda body,
and lookup types in that map as appropriate. We need to carefully update
and restore this map to handle nested lambdas and to not "leak" these
types to other parts of inference (since the types may have references
to unresolved type variables). (The alternative would have been to
thread these types as a parameter through many methods, but that would
have been a more disruptive change.) To handle lambdas like `box -> {
var copy = box; return copy; }`, we also update our visitor to discover
return statements to also discover and register `var`-declared locals.
We disable caching of inferred types whenever
`lambdaParameterTypesForInference` is non-empty to be conservative.

The new tests exercise a number of cases around expression lambdas vs
statement lambdas and nested lambdas.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## Summary

* **Bug Fixes**
* Improved nullness inference for implicit lambda parameters in nested
generic calls, local variables, and expression- or block-bodied lambdas.
* Preserved nullable-dereference diagnostics when generic method calls
appear in lambda-local variable initializers, keeping potentially unsafe
dereferences visible.
* Improved recognition of nested nullable types across lambda-body calls
and local assignments, including cases where inferred types are used in
subsequent generic operations.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
msridhar pushed a commit that referenced this pull request Oct 21, 2026
…ethods (#2003)

An explicitly typed lambda could declare a non-null parameter where the functional interface method, `@NullUnmarked` or otherwise unannotated, has a `@Nullable` parameter that NullAway acknowledges (JSpecify mode, or `AcknowledgeRestrictiveAnnotations=true`), and NullAway reported nothing. An anonymous class, a method reference, and an implicitly typed lambda implementing the same method were reported. `matchLambdaExpression` returned before `checkParamOverriding` whenever the interface method was unannotated, so `RestrictiveAnnotationHandler` never got to mark the parameter.

The lambda's parameters are now checked like those of the other implementations. In legacy mode without restrictive annotations no handler marks an unannotated parameter `@Nullable` unless a custom library model does, so default behavior there is unchanged.

Sending lambdas through `checkParamOverriding` exposed a varargs defect it already had for anonymous classes and method references. It read the varargs position of `MethodParameterNullness`, which for handler-provided values holds the nullness of the elements; the array's nullness is in `getVarargsArrayNullness()`. Over an unmarked `apply(@nullable Object @nonnull ... inputs)`, an override taking `Object...` was reported, and over `apply(Object @nullable ... inputs)` it was not. The check now uses the array's nullness at the varargs position, both for the overridden method and for a referenced method that receives the whole array, and `RestrictiveAnnotationHandler` now records a type-use `@Nullable` varargs array. `JSpecifyVarargsTests#restrictiveAnnotationsVarargsLambdaUsesArrayNullness` keeps its expectation of no warning: the array is `@NonNull`, and the test now reaches the varargs check it is named for instead of returning early. A library model that sets the varargs array nullness of an overridable method now applies to override checks too, as the handler contract states; no default model does.

A method reference whose trailing interface parameters expand into the referenced method's varargs, such as `apply(Object, @nullable Object)` to `take(Object...)`, threw `IndexOutOfBoundsException` from `checkParamOverriding`. The position is now mapped to the varargs parameter and checked by element nullness.

Caller sees:
- `parameter o is @nonnull, but parameter in functional interface method com.uber.Test.Fi.apply(@nullable Object) is @Nullable` on an explicitly typed lambda over an unmarked or restrictively annotated method.
- `parameter inputs of referenced method is @nonnull, but parameter in functional interface method ... is @Nullable` for a method reference against a type-use `@Nullable` varargs array when the referenced varargs array is non-null, including a legacy `@javax.annotation.Nullable Object...`, whose annotation applies to the elements.
- No warning for an override or method reference whose varargs elements are `@NonNull` but whose array may be null, where one was reported before.

Nested nullness of explicitly typed lambda parameters stays out of scope: #1739 adds that check inside `checkParamOverriding`, which lambdas over unmarked interface methods now reach as well. Also left out, each the same kind of inconsistency found while testing this change:
- An expression-bodied lambda over an unannotated method whose return is restrictively `@NonNull` is not checked, while a block-bodied lambda and an anonymous class are.
- A plain override or a lambda at the varargs position is still judged by `hasNullableAnnotation`, so an override `apply(@javax.annotation.Nullable Object... p)` of `apply(Object @nullable ... p)` is not reported, while a method reference with the same signature now is.
- `CoreNullnessStoreInitializer` gives an implicitly typed lambda parameter at the varargs position the elements' nullness rather than the array's.

Fixes #1994

🤖 Generated with [Claude Code](https://claude.com/claude-code)


<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

- **Bug Fixes**
  - Improved nullness analysis for varargs, distinguishing nullable arrays from nullable elements.
  - Added more accurate checks for varargs overrides, lambdas, and method references, including expanded and unbound references.
  - Restrictive annotations now correctly recognize both nullable and non-null varargs arrays.
  - JSpecify analysis now reports mismatches more reliably while accepting compatible nullable assignments.
- **Tests**
  - Added coverage for varargs, lambdas, method references, and restrictive-annotation scenarios.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
@msridhar
msridhar force-pushed the more-nested-nullness-override-checks branch from d9b2857 to c01b57f Compare November 14, 2026 00:35
@msridhar
msridhar added this pull request to stack #2161 November 14, 2026 00:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant