Skip to content

Remove onOverrideMethodInvocationVarargsArrayNullability handler method - #1518

Merged
msridhar merged 11 commits into
masterfrom
issue-1501
Apr 11, 2026
Merged

msridhar merged 11 commits into
masterfrom
issue-1501

Conversation

@msridhar

@msridhar msridhar commented Apr 11, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1501

Instead, we return varargs array nullability through the previous onOverrideMethodInvocationParametersNullability handler method. We introduce a new MethodParameterNullness type to encapsulate regular parameter nullability and nullability of the varargs array; MethodParameterNullness is mutable for efficiency (like the arrays we used before).

This PR should not change visible behavior. Still, I wasn't sure about some corner cases involving lambdas and method overriding, so there are some new tests. I confirmed those new tests passed even without these changes.

Summary by CodeRabbit

  • Refactor

    • Reworked how method-parameter nullness is represented and passed through the analysis pipeline, simplifying per-parameter and varargs nullness handling.
    • Consolidated varargs-array nullness into the parameter nullness model and removed a separate varargs hook.
  • Tests

    • Added JSpecify-mode tests exercising varargs and lambda parameter nullness inference.

@msridhar
msridhar requested a review from lazaroclapp April 11, 2026 14:52
@coderabbitai

coderabbitai Bot commented Apr 11, 2026 •

Copy link
Copy Markdown
Contributor

Walkthrough

Introduces a new public final class MethodParameterNullness that stores mutable nullness metadata for a method’s formal parameters and, when applicable, its varargs array. Replaces previous per-parameter @Nullable Nullness[] usage across invocation analysis and lambda/functional-interface initialization with the MethodParameterNullness object. Updates handler APIs and implementations to accept/return MethodParameterNullness instead of nullable Nullness[], and removes the separate varargs-array handler hook. Adds JSpecify varargs tests exercising the updated varargs nullness inference.

Possibly related PRs

Suggested labels

jspecify

Suggested reviewers

  • lazaroclapp
  • yuxincs
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: removing the onOverrideMethodInvocationVarargsArrayNullability handler method, which aligns with the primary objective documented in issue #1501.
Linked Issues check ✅ Passed The PR successfully addresses issue #1501 by removing onOverrideMethodInvocationVarargsArrayNullability and consolidating varargs array nullability handling into MethodParameterNullness and the existing onOverrideMethodInvocationParametersNullability handler method.
Out of Scope Changes check ✅ Passed All changes are directly related to the objective of removing onOverrideMethodInvocationVarargsArrayNullability and consolidating varargs handling; the new MethodParameterNullness class and related refactoring are necessary supporting changes for achieving this goal.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch issue-1501

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In
`@nullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.java`:
- Around line 135-140: When handling varargs, the code currently only sets
array-level nullness via Nullness.varargsArrayIsNonNull(...), which misses cases
where the parameter is explicitly annotated `@Nullable`; update the
RestrictiveAnnotationHandler logic so that when isVarargsParam is true and
Nullness.paramHasNullableAnnotation(methodSymbol, i, config) is true you also
call argumentNullness.setVarargsArrayNullness(Nullness.NULLABLE) (in addition to
existing per-parameter handling), ensuring varargs-array nullness is preserved
for explicitly nullable varargs parameters; use the existing symbols
isVarargsParam, varargsParamSymbol, paramHasNullableAnnotation,
varargsArrayIsNonNull, and setVarargsArrayNullness to locate and implement the
fix.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 5ffa8aa7-0504-46a9-817f-83c350687612

📥 Commits

Reviewing files that changed from the base of the PR and between 164d8f6 and 97ec233.

📒 Files selected for processing (8)
  • nullaway/src/main/java/com/uber/nullaway/MethodParameterNullness.java
  • nullaway/src/main/java/com/uber/nullaway/NullAway.java
  • nullaway/src/main/java/com/uber/nullaway/dataflow/CoreNullnessStoreInitializer.java
  • nullaway/src/main/java/com/uber/nullaway/handlers/CompositeHandler.java
  • nullaway/src/main/java/com/uber/nullaway/handlers/Handler.java
  • nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java
  • nullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.java
  • nullaway/src/test/java/com/uber/nullaway/jspecify/JSpecifyVarargsTests.java

Comment on lines 135 to 140
} else if (Nullness.paramHasNullableAnnotation(methodSymbol, i, config)) {
argumentPositionNullness[i] = Nullness.NULLABLE;
argumentNullness.setParameterNullness(i, Nullness.NULLABLE);
}
}
return argumentPositionNullness;
}

@Override
public @Nullable Nullness onOverrideMethodInvocationVarargsArrayNullability(
Context context,
Symbol.MethodSymbol methodSymbol,
boolean isAnnotated,
@Nullable Nullness varargsArrayNullness) {
if (methodSymbol.isVarArgs()) {
Symbol.VarSymbol varargsParamSymbol =
methodSymbol.getParameters().get(methodSymbol.getParameters().size() - 1);
if (Nullness.varargsArrayIsNonNull(varargsParamSymbol, config)) {
return Nullness.NONNULL;
if (isVarargsParam && Nullness.varargsArrayIsNonNull(varargsParamSymbol, config)) {
argumentNullness.setVarargsArrayNullness(Nullness.NONNULL);
}

@coderabbitai coderabbitai Bot Apr 11, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Preserve nullable varargs-array nullness.

After removing the dedicated varargs hook, this path only writes array-level nullness through Nullness.varargsArrayIsNonNull(...), so a restrictively annotated @Nullable T... parameter never updates argumentNullness.setVarargsArrayNullness(...). That changes behavior for explicit-array varargs calls and related override checks when the default varargs-array nullness is non-null.

Proposed fix
       } else if (Nullness.paramHasNullableAnnotation(methodSymbol, i, config)) {
         argumentNullness.setParameterNullness(i, Nullness.NULLABLE);
+        if (isVarargsParam) {
+          argumentNullness.setVarargsArrayNullness(Nullness.NULLABLE);
+        }
       }
       if (isVarargsParam && Nullness.varargsArrayIsNonNull(varargsParamSymbol, config)) {
         argumentNullness.setVarargsArrayNullness(Nullness.NONNULL);
       }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@nullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.java`
around lines 135 - 140, When handling varargs, the code currently only sets
array-level nullness via Nullness.varargsArrayIsNonNull(...), which misses cases
where the parameter is explicitly annotated `@Nullable`; update the
RestrictiveAnnotationHandler logic so that when isVarargsParam is true and
Nullness.paramHasNullableAnnotation(methodSymbol, i, config) is true you also
call argumentNullness.setVarargsArrayNullness(Nullness.NULLABLE) (in addition to
existing per-parameter handling), ensuring varargs-array nullness is preserved
for explicitly nullable varargs parameters; use the existing symbols
isVarargsParam, varargsParamSymbol, paramHasNullableAnnotation,
varargsArrayIsNonNull, and setVarargsArrayNullness to locate and implement the
fix.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The restrictive annotation handler only handles @NonNull annotations in null-unmarked (unannotated) code. So I think this is fine.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msridhar, that's a good clarification — since RestrictiveAnnotationHandler only acts on null-unmarked code and its primary purpose is to enforce @NonNull restrictive annotations (overriding the default nullable assumption), the varargs-array nullable case isn't something this handler needs to address. My concern was unfounded. Thanks for the explanation!


✏️ Learnings added
Learnt from: msridhar
Repo: uber/NullAway PR: 1518
File: nullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.java:135-141
Timestamp: 2026-04-11T17:13:55.316Z
Learning: In NullAway's `RestrictiveAnnotationHandler` (`nullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.java`), the handler only applies to null-unmarked (unannotated) code (`isAnnotated == false`) and its primary purpose is to enforce `NonNull` restrictive annotations. The `Nullable` branch exists for completeness but the handler does NOT need to propagate `Nullable` to varargs-array nullness (`setVarargsArrayNullness`), since that is not within the scope of restrictive annotation handling for unannotated code.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.


🧠 Learnings used
Learnt from: CR
Repo: uber/NullAway PR: 0
File: AGENTS.md:0-0
Timestamp: 2026-04-08T19:11:30.754Z
Learning: Applies to **/*.java : Do not add top-level `Nullable` annotations on local variables in Java code. NullAway infers the nullability of local variables and ignores these explicit annotations.

Learnt from: msridhar
Repo: uber/NullAway PR: 1485
File: nullaway/src/main/java/com/uber/nullaway/NullAway.java:2119-2128
Timestamp: 2026-03-20T15:34:10.657Z
Learning: In this repo (uber/NullAway), don’t annotate local variables with `Nullable` or `NonNull`. NullAway infers nullability for local variables automatically, so adding these annotations to locals is incorrect style for this codebase.

Learnt from: msridhar
Repo: uber/NullAway PR: 1507
File: nullaway/src/main/java/com/uber/nullaway/generics/GenericTypePrettyPrintingVisitor.java:66-79
Timestamp: 2026-04-01T15:40:51.141Z
Learning: In NullAway's `GenericTypePrettyPrintingVisitor` (nullaway/src/main/java/com/uber/nullaway/generics/GenericTypePrettyPrintingVisitor.java), the `appendNullableAnnotationIfPresent` helper intentionally prints ONLY `Nullable` type-use annotations and silently suppresses all other type-use annotations (including `NonNull`). This is by design: non-null is the default in NullAway's model and other type-use qualifiers are not relevant to NullAway diagnostics, so only `Nullable` is meaningful to display in error messages.

Learnt from: msridhar
Repo: uber/NullAway PR: 1316
File: jdk-javac-plugin/src/main/java/com/uber/nullaway/javacplugin/NullnessAnnotationSerializer.java:261-293
Timestamp: 2025-10-29T23:56:18.236Z
Learning: In NullAway's jdk-javac-plugin NullnessAnnotationSerializer, type variable bounds with annotations (e.g., `T extends Nullable Object`) are checked at their declaration sites by the typeParamHasAnnotation method for both class-level and method-level type parameters. The hasJSpecifyAnnotationDeep method is designed to check type uses (return types, parameters, etc.) and does not need a TYPEVAR case because type variable declaration bounds are already handled separately.

Learnt from: msridhar
Repo: uber/NullAway PR: 1248
File: nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java:847-857
Timestamp: 2025-08-28T04:54:20.953Z
Learning: In NullAway's GenericsChecks.java, NewClassTree support for explicit type argument substitution requires more extensive changes beyond just modifying the conditional in compareGenericTypeParameterNullabilityForCall. The maintainers prefer to handle NewClassTree support in a separate follow-up rather than expanding the scope of PRs focused on specific issues like super constructor calls.

Learnt from: msridhar
Repo: uber/NullAway PR: 1245
File: guava-recent-unit-tests/src/test/java/com/uber/nullaway/guava/NullAwayGuavaParametricNullnessTests.java:101-102
Timestamp: 2025-08-14T18:50:06.159Z
Learning: In NullAway JSpecify tests, when JDK version requirements exist due to bytecode annotation reading capabilities, prefer failing tests over skipping them on unsupported versions to ensure CI catches regressions and enforces proper JDK version usage for developers.

Learnt from: msridhar
Repo: uber/NullAway PR: 1259
File: jdk-recent-unit-tests/src/test/java/com/uber/nullaway/jdk17/SwitchTests.java:318-321
Timestamp: 2025-08-29T18:41:43.584Z
Learning: Classes annotated with NullMarked are analyzed by NullAway even if they are not in packages specified by the AnnotatedPackages configuration. The NullMarked annotation guarantees NullAway analysis.

Learnt from: msridhar
Repo: uber/NullAway PR: 1464
File: nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java:610-621
Timestamp: 2026-02-15T00:29:58.053Z
Learning: In NullAway's codebase, the `InvocationArguments.forEach` method has special-case handling for varargs and other edge cases, so it should not be replaced with a simple indexed loop. Using `forEach` with an `AtomicReference` or similar pattern is appropriate when you need to find a specific argument and its corresponding formal parameter type.

Learnt from: msridhar
Repo: uber/NullAway PR: 1421
File: nullaway/src/main/java/com/uber/nullaway/NullAway.java:339-340
Timestamp: 2025-12-31T23:59:20.009Z
Learning: In Error Prone's ASTHelpers class, the getSymbol(MethodInvocationTree) overload is guaranteed to return a non-null Symbol.MethodSymbol. This differs from other overloads of getSymbol() that may return null. Therefore, no null check is required after calling ASTHelpers.getSymbol() with a MethodInvocationTree parameter.

Learnt from: haewiful
Repo: uber/NullAway PR: 1243
File: jdk-annotations/astubx-generator-cli/src/main/java/com/uber/nullaway/jdkannotations/AstubxGeneratorCLI.java:97-106
Timestamp: 2025-09-24T23:14:37.777Z
Learning: In the jdk-javac-plugin NullnessAnnotationSerializer, the plugin only creates one JSON file per compilation, so processing only the first JSON file is intentional behavior rather than a bug.

@github-actions

Copy link
Copy Markdown

Main Branch:

Benchmark                          Mode  Cnt   Score   Error  Units
AutodisposeBenchmark.compile      thrpt   25  10.456 ± 0.120  ops/s
CaffeineBenchmark.compile         thrpt   25   1.930 ± 0.035  ops/s
DFlowMicroBenchmark.compile       thrpt   25  41.369 ± 0.329  ops/s
NullawayReleaseBenchmark.compile  thrpt   25   1.645 ± 0.024  ops/s

With This PR:

Benchmark                          Mode  Cnt   Score   Error  Units
AutodisposeBenchmark.compile      thrpt   25  10.638 ± 0.088  ops/s
CaffeineBenchmark.compile         thrpt   25   1.947 ± 0.036  ops/s
DFlowMicroBenchmark.compile       thrpt   25  41.630 ± 0.235  ops/s
NullawayReleaseBenchmark.compile  thrpt   25   1.646 ± 0.027  ops/s

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (1)
nullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.java (1)

135-141: ⚠️ Potential issue | 🟠 Major

Preserve nullable varargs-array nullness for @Nullable T....

At Line 135-137, @Nullable on the varargs parameter updates only per-parameter nullness. But array-level nullness is only set in the non-null path at Line 138-140. This can drop nullable varargs-array semantics and change behavior for explicit-array varargs calls / override checks.

Proposed fix
       } else if (Nullness.paramHasNullableAnnotation(methodSymbol, i, config)) {
         argumentNullness.setParameterNullness(i, Nullness.NULLABLE);
+        if (varargsParamSymbol != null) {
+          argumentNullness.setVarargsArrayNullness(Nullness.NULLABLE);
+        }
       }
       if (varargsParamSymbol != null
           && Nullness.varargsArrayIsNonNull(varargsParamSymbol, config)) {
         argumentNullness.setVarargsArrayNullness(Nullness.NONNULL);
       }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In
`@nullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.java`
around lines 135 - 141, The code in RestrictiveAnnotationHandler sets
per-parameter nullness for a varargs parameter when
Nullness.paramHasNullableAnnotation(...) is true but fails to also set the
varargs array nullness, so add logic in that branch to preserve array-level
NULLABLE semantics for varargs: inside the block where you call
argumentNullness.setParameterNullness(i, Nullness.NULLABLE) (the branch using
Nullness.paramHasNullableAnnotation), detect if the current parameter is the
varargs parameter (varargsParamSymbol != null and i is the varargs index) and
call argumentNullness.setVarargsArrayNullness(Nullness.NULLABLE); leave the
existing non-null path that sets NONNULL unchanged so you don't overwrite
explicit non-null handling.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In
`@nullaway/src/main/java/com/uber/nullaway/handlers/RestrictiveAnnotationHandler.java`:
- Around line 135-141: The code in RestrictiveAnnotationHandler sets
per-parameter nullness for a varargs parameter when
Nullness.paramHasNullableAnnotation(...) is true but fails to also set the
varargs array nullness, so add logic in that branch to preserve array-level
NULLABLE semantics for varargs: inside the block where you call
argumentNullness.setParameterNullness(i, Nullness.NULLABLE) (the branch using
Nullness.paramHasNullableAnnotation), detect if the current parameter is the
varargs parameter (varargsParamSymbol != null and i is the varargs index) and
call argumentNullness.setVarargsArrayNullness(Nullness.NULLABLE); leave the
existing non-null path that sets NONNULL unchanged so you don't overwrite
explicit non-null handling.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 243ffd15-536a-4dd7-98bd-5e031a171e72

📥 Commits

Reviewing files that changed from the base of the PR and between 97ec233 and 6f02cdc.

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

@codecov

codecov Bot commented Apr 11, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.69767% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.48%. Comparing base (7b5c924) to head (9ab011d).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...m/uber/nullaway/handlers/LibraryModelsHandler.java 68.42% 3 Missing and 3 partials ⚠️
...ava/com/uber/nullaway/MethodParameterNullness.java 88.23% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1518      +/-   ##
============================================
+ Coverage     88.46%   88.48%   +0.01%     
- Complexity     2840     2841       +1     
============================================
  Files           102      103       +1     
  Lines          9478     9488      +10     
  Branches       1901     1902       +1     
============================================
+ Hits           8385     8395      +10     
- Misses          530      531       +1     
+ Partials        563      562       -1     

☔ View full report in Codecov by Sentry.
📢 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.

@lazaroclapp lazaroclapp left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Comments below are just random related questions

}
void test() {
NullableVarargsElementsConsumer consumer =
inputs -> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the semantics here is that inputs is the full array? Can't be called as i1, i2, i3 -> { ... }, then? (I assume so, just double checking)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it looks like for a varargs functional-interface method, you can only assign a lambda that takes a single argument, which must be an array; this example doesn't compile https://godbolt.org/z/YsYP1G6KT

/**
* Mutable nullness information for a method's formal parameters and varargs array. For a varargs
* method, the nullability of individual arguments is represented in the last parameter position,
* while the nullability of the varargs array itself is represented separately.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we plan to allow using this class in the future to override deep nullability? E.g. allow stuff like overriding argument 1 of foo(List<Bar> bars) to be List<@Nullable Bar> or List<? extends @Nullable Bar> or whatever? Obviously outside of the scope of this PR, but I am curious...

We currently have a separate model from LibraryModels to set these kinds of annotations for third-party libraries / JDK method, right? (using astub(x)?)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now this is handled by the onOverrideMethodType handler method, see, e.g., here for library models:

It would be great to unify the two eventually, but it will also be painful given that we still need to handle declaration annotations and other legacy corner cases. Plus, all the original APIs didn't handle nested annotations, and that support has been tacked on for JSpecify mode, in a way that allows for JSpecify mode to be turned off. I think maybe when JSpecify mode is the default, or even better when there is only JSpecify mode for NullAway, we could think about cleaning things up into one handler method.

@msridhar
msridhar enabled auto-merge (squash) April 11, 2026 21:42
@msridhar
msridhar merged commit 68fc07e into master Apr 11, 2026
12 checks passed
@msridhar
msridhar deleted the issue-1501 branch April 11, 2026 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove new onOverrideMethodInvocationVarargsArrayNullability handler method

2 participants