Repository navigation
Check explicitly typed lambda parameters against unmarked interface methods - #2003
Conversation
…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)
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)
|
Verification notes (kept out of the description, which becomes the squash commit body):
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
WalkthroughNullAway 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: Priority: ➖ Normal Change: Bug fix · Severity of issue fixed: Medium Merge Risk: ⚪ Minimal · up to
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
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 -->
An explicitly typed lambda could declare a non-null parameter where the functional interface method,
@NullUnmarkedor otherwise unannotated, has a@Nullableparameter that NullAway acknowledges (JSpecify mode, orAcknowledgeRestrictiveAnnotations=true), and NullAway reported nothing. An anonymous class, a method reference, and an implicitly typed lambda implementing the same method were reported.matchLambdaExpressionreturned beforecheckParamOverridingwhenever the interface method was unannotated, soRestrictiveAnnotationHandlernever 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
@Nullableunless a custom library model does, so default behavior there is unchanged.Sending lambdas through
checkParamOverridingexposed a varargs defect it already had for anonymous classes and method references. It read the varargs position ofMethodParameterNullness, which for handler-provided values holds the nullness of the elements; the array's nullness is ingetVarargsArrayNullness(). Over an unmarkedapply(@Nullable Object @NonNull ... inputs), an override takingObject...was reported, and overapply(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, andRestrictiveAnnotationHandlernow records a type-use@Nullablevarargs array.JSpecifyVarargsTests#restrictiveAnnotationsVarargsLambdaUsesArrayNullnesskeeps 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)totake(Object...), threwIndexOutOfBoundsExceptionfromcheckParamOverriding. 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 @Nullableon 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 @Nullablefor a method reference against a type-use@Nullablevarargs array when the referenced varargs array is non-null, including a legacy@javax.annotation.Nullable Object..., whose annotation applies to the elements.@NonNullbut 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:@NonNullis not checked, while a block-bodied lambda and an anonymous class are.hasNullableAnnotation, so an overrideapply(@javax.annotation.Nullable Object... p)ofapply(Object @Nullable ... p)is not reported, while a method reference with the same signature now is.CoreNullnessStoreInitializergives 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