Skip to content

Check explicitly typed lambda parameters against unmarked interface methods - #2003

Merged
msridhar merged 10 commits into
apache:masterfrom
vlsi:fix-1994-typed-lambda-unmarked
Oct 21, 2026
Merged

msridhar merged 10 commits into
apache:masterfrom
vlsi:fix-1994-typed-lambda-unmarked

Conversation

@vlsi

@vlsi vlsi commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

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

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.

vlsi added 9 commits October 20, 2026 19:19
…thods

Assisted-by: Claude Code (claude-opus-5-5)
Assisted-by: Claude Code (claude-opus-5-5)
Assisted-by: Claude Code (claude-opus-5-5)
…ethods

Assisted-by: Claude Code (claude-opus-5-5)
…rmal

Assisted-by: Claude Code (claude-opus-5-5)
Assisted-by: Claude Code (claude-opus-5-5)
Assisted-by: Claude Code (claude-opus-5-5)
Assisted-by: Claude Code (claude-opus-5-5)
@vlsi

vlsi commented Oct 20, 2026

Copy link
Copy Markdown
Contributor Author

Verification notes (kept out of the description, which becomes the squash commit body):

  • The regression tests sit in their own commits ahead of the fix; on the base commit the Explicitly typed lambda skips the @Nullable parameter check when the functional interface method is @NullUnmarked #1994 tests in NullMarkednessTests and AcknowledgeRestrictiveAnnotationsTests and the varargs lambda test fail with "Did not see an error … There were no errors". varargsMethodRefExpandingTrailingParametersChecksElementNullness and unboundVarargsMethodRefSkipsReceiverPosition throw IndexOutOfBoundsException without the index mapping.
  • ./gradlew :nullaway:test and :nullaway:buildWithNullAway pass; diff coverage is 32/32 changed lines and 34/34 branches.
  • pitestDiff: 25 of 26 mutants killed. The survivor replaces the unannotated-method guard in matchLambdaExpression (now guarding only the expression-lambda return check) with false. Killing it would mean pinning the expression-versus-block lambda inconsistency listed as left out, so I did not add a test for it.
  • The library-model consequence (varargs array models reaching override checks) has no test: it needs an overridable interface in test-java-lib and a model entry in test-library-models.

@coderabbitai

coderabbitai Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: uber/NullAway/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: f7e219b0-8083-4008-b0fa-f5d12ba78297
📥 Commits

Reviewing files that changed from the base of the PR and between dd66bf4 and 709e67f.

📒 Files selected for processing: 6
  • nullaway/src/main/java/com/uber/nullaway/NullAway.java
  • nullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.java
  • nullaway/src/test/java/com/uber/nullaway/AcknowledgeRestrictiveAnnotationsTests.java
  • nullaway/src/test/java/com/uber/nullaway/VarargsTests.java
  • nullaway/src/test/java/com/uber/nullaway/jspecify/JSpecifyVarargsTests.java
  • nullaway/src/test/java/com/uber/nullaway/jspecify/NullMarkednessTests.java

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 3 remain after this review.

Walkthrough

NullAway now distinguishes varargs array nullness from element nullness when checking overrides and method references, including adapted and unbound method references. Restrictive annotation handling can assign nullable or non-null nullness to the varargs array. Lambda parameter checks now run before the early return for unannotated functional interface methods. New tests cover these cases in regular and JSpecify modes.

Suggested reviewers: msridhar

Priority: ➖ Normal

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 709e6

The change extends nullness checks to explicitly typed lambdas and varargs method references. Its tests cover the main cases, and no merge-blocking issue was found.

Pre-merge checks: Passed 6 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 24 functions across 6 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 6 passed
Check name Status Explanation
Title check Passed The title clearly describes the primary change: checking explicitly typed lambda parameters against unmarked functional-interface methods.
Description check Passed The description directly explains the lambda-checking fix, related varargs corrections, test coverage, validation, and known limitations.
Linked Issues check Passed The changes directly address linked issue #1994 and match its stated scope for explicitly typed lambdas over unmarked or restrictively annotated methods.
Out of Scope Changes check Passed The varargs and method-reference fixes are directly related to the lambda override-checking change. The description clearly identifies separate limitations that remain out of scope.
Linked Issues check Passed Issue #1994 requires explicitly typed lambda parameters to be checked against nullable parameters on unmarked functional-interface methods. matchLambdaExpression now calls checkParamOverriding bef…
Out of Scope Changes check Passed The varargs changes support the linked objective. They correct array-versus-element nullness, map expanded method-reference parameters, and prevent the exposed index error in checkParamOverriding. R…
Full details: Linked Issues check

Explanation

Issue #1994 requires explicitly typed lambda parameters to be checked against nullable parameters on unmarked functional-interface methods. matchLambdaExpression now calls checkParamOverriding before the unannotated-method return. Restrictive-annotation handling can therefore provide the nullable parameter. Tests cover JSpecify mode and AcknowledgeRestrictiveAnnotations=true. Existing behavior for named implementations remains covered. Nested nullness remains assigned to #1739 and is not required by #1994.

Full details: Out of Scope Changes check

Explanation

The varargs changes support the linked objective. They correct array-versus-element nullness, map expanded method-reference parameters, and prevent the exposed index error in checkParamOverriding. Restrictive annotation handling and regression tests cover these paths. No unrelated change is demonstrated.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.26%. Comparing base (1d00cfd) to head (709e67f).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #2003      +/-   ##
============================================
+ Coverage     88.20%   88.26%   +0.05%     
  Complexity       38       38              
============================================
  Files           117      117              
  Lines         11951    11968      +17     
  Branches       2451     2458       +7     
============================================
+ Hits          10541    10563      +22     
+ Misses          640      639       -1     
+ Partials        770      766       -4     

☔ 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 enabled auto-merge (squash) October 21, 2026 02:36
@msridhar
msridhar merged commit 3688fa2 into apache:master Oct 21, 2026
20 checks passed
msridhar pushed a commit that referenced this pull request Oct 22, 2026
In JSpecify mode, an explicitly typed lambda parameter was checked against the functional interface method only at the top level. The nullness of its array elements and type arguments was never compared, while an anonymous class overriding the same method gets that comparison. So `(Object[] inputs) -> {}` against `void apply(@nullable Object[] inputs)` was accepted, while `new Fi() { public void apply(Object[] inputs) {} }` was reported, and the same held for `(List<String> list) -> {}` against `apply(List<@nullable String> list)`. This is a variation of #1994, which #2003 fixed for the top-level parameter check only.

`matchLambdaExpression` now gives each explicitly typed parameter the check an overriding method's parameters get: the declared type may be a supertype of the interface method's parameter type, and type arguments must have identical nullness. The interface method type is taken as a member of the lambda's target type, with library models applied as for an override. An implicitly typed parameter takes the interface method's type and is skipped.

For an explicitly typed lambda passed to a generic method, call inference now takes constraints from the declared parameter types, nested types only. Without them, a type variable that only a declared parameter type mentions, such as `T` in `take((List<@nullable String> list) -> {})` against `<T extends @nullable Object> void take(Consumer<List<T>> c)`, defaulted to non-null, and the new check reported correct code. Top-level nullness stays out of inference, so a top-level mismatch is still reported at the parameter as `parameter s is @nonnull, but parameter in functional interface method ... is @Nullable`.

Caller sees: `Parameter has type List<String>, but overridden method has parameter type List<@nullable String>, which has mismatched type parameter nullability` at an explicitly typed lambda parameter whose array element or type argument nullness differs from the functional interface method's parameter.

`JSpecifyVarargsTests#restrictiveAnnotationsVarargsLambdaUsesArrayNullness` asserted that `(Object[] inputs) -> {}` against `@NullUnmarked void apply(@nullable Object @nonnull... inputs)` is clean. It now expects this warning, which the anonymous-class form of the same code already gets.

The constraint solver ignores the type arguments of an enclosing type, so `take((Outer<@nullable String>.Inner x) -> {})` against `Consumer<Outer<T>.Inner>` is still reported; a plain argument and a method reference with the same types are reported the same way before this change. No test covers a library model that adds nested annotations to a functional interface method parameter, since the test library models have none.

The open #1953 changes `NullAway.checkParamOverriding` for method references to varargs methods. This change does not touch that method; both add tests to `JSpecifyVarargsTests`, in different methods.

Assisted-by: Claude Code (claude-opus-5-5)

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


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

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

- **Bug Fixes**
  - Improved JSpecify nullness analysis for explicitly typed lambda parameters.
  - Detects mismatches between nullable and non-null generic, array, and varargs parameter types.
  - Applies correct type checking when assigning lambdas to functional interfaces or inferring generic method arguments.

- **Tests**
  - Added coverage for generic lambdas, arrays, wildcards, raw types, `BiConsumer`, and code outside JSpecify mode.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
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.

Explicitly typed lambda skips the @Nullable parameter check when the functional interface method is @NullUnmarked

2 participants