Skip to content

Fix varargs element nullability check in JSpecify mode (Fixes #1002) - #1607

Closed
dyrpsf wants to merge 1 commit into
apache:masterfrom
dyrpsf:feature-broaden-jspecify-annotations
Closed

dyrpsf wants to merge 1 commit into
apache:masterfrom
dyrpsf:feature-broaden-jspecify-annotations

Conversation

@dyrpsf

@dyrpsf dyrpsf commented Jun 14, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR addresses Issue #1002 by fixing false positive warnings that occur when passing @Nullable arguments to varargs methods (like Caffeine's requireState) in JSpecify mode.

Root Cause & Fix

When JSpecify mode is enabled, NullAway correctly parses a varargs parameter like @Nullable Object... args as an array with nullable elements, rather than a nullable array itself.

However, during handleInvocation, NullAway was still using Nullness.paramHasNullableAnnotation(...) to check the varargs parameter. Because that method checks the top-level array type, it failed to see the @Nullable element annotation, resulting in NullAway strictly enforcing @NonNull arguments for the varargs positions.

I updated the logic inside NullAway.java's handleInvocation method. When JSpecify mode is active and the parameter being evaluated is the final varargs parameter, it now explicitly delegates to NullabilityUtil.nullableVarargsElementsForSourceOrBytecode(param, config) to check the element's nullability. Legacy behavior is preserved when JSpecify mode is off.

The test suite is fully green.

Summary by CodeRabbit

  • Bug Fixes
    • Improved null-safety checking accuracy for varargs parameters in JSpecify mode.

@coderabbitai

coderabbitai Bot commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 0cc8bdbe-a572-4c29-a8a6-9c339be959ad

📥 Commits

Reviewing files that changed from the base of the PR and between f4d8951 and b393e4d.

📒 Files selected for processing (1)
  • nullaway/src/main/java/com/uber/nullaway/NullAway.java

Walkthrough

In NullAway.matchInvocation, the per-parameter nullability loop was updated to handle JSpecify mode varargs correctly. When iterating over method parameters and the invoked method is varargs, the current index is the last formal parameter, and JSpecify mode is active, the code now derives hasNullableAnno by calling NullabilityUtil.nullableVarargsElementsForSourceOrBytecode(...) rather than using the result of Nullness.paramHasNullableAnnotation. This derived value is then used to set the MethodParameterNullness entry for that argument position.

Possibly related PRs

  • uber/NullAway#1484: Also modifies NullAway.matchInvocation's varargs nullability handling, extending per-argument MethodParameterNullness checks for @NonNull and varargs-array nullability in restrictive annotation mode.
  • uber/NullAway#1518: Introduced the MethodParameterNullness/handler consolidation refactor in matchInvocation that this PR builds on for the JSpecify varargs nullable element path.
  • uber/NullAway#1419: Adjusts nullness inference for array element type-use annotations via checkArrayElementAnnotations, targeting the same array/varargs element annotation inference area.

Suggested labels

run-benchmarks

Suggested reviewers

  • lazaroclapp
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: fixing varargs element nullability checking in JSpecify mode, with a reference to the issue being fixed.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

@msridhar msridhar closed this Jun 14, 2026
@dyrpsf
dyrpsf deleted the feature-broaden-jspecify-annotations branch June 15, 2026 03:04
@dyrpsf

dyrpsf commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

Closing the loop on this: after digging into the handleInvocation logic, I found a historical gap in how varargs were evaluated in JSpecify mode and opened this PR.

However, as @msridhar noted, testing against the current master branch shows that requireState and similar varargs methods are now analyzed correctly without triggering the false positives. The engine's architecture has naturally resolved this edge case since the issue was originally opened.

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.

2 participants