Skip to content

Fix inference bug with generic instance methods - #1654

Merged
msridhar merged 3 commits into
masterfrom
inference-wildcard-bug
Jul 26, 2026
Merged

msridhar merged 3 commits into
masterfrom
inference-wildcard-bug

Conversation

@msridhar

@msridhar msridhar commented Jul 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fix generic method inference to substitute type arguments from the invocation receiver before generating nullability constraints and reconstructing inferred types.

Previously, inference used the method’s declaration-site type. For a call such as a generic method returning V on a Box<@Nullable Object> receiver, NullAway treated the return as the unconstrained type variable V instead of @Nullable Object. The solver could consequently infer a non-null return and report a false-positive warning for a lambda returning that value.

Implementation

Introduce a shared helper that:

  • obtains the method’s enclosing receiver type;
  • computes the method type as a member of that receiver;
  • applies handler-provided method models.

Use this receiver-aware method type when:

  • generating generic inference constraints;
  • caching inferred lambda and method-reference types;
  • reconstructing generic invocation return types;
  • restoring method-invocation type annotations in getTreeType.

This also removes duplicated receiver-substitution logic.

Tests

Added regression coverage for:

  • a nullable generic instance-method return used within a Stream.map lambda returned as List<?>, which should produce no warning;
  • a var initialized from a generic instance method on an inferred nullable receiver, ensuring the nested nullable type argument is preserved.

Summary by CodeRabbit

Summary

  • Bug Fixes

    • Improved generic method type inference for invocations using parameterized receiver types, preserving nullness correctly.
    • Fixed inferred generic instance-method return type behavior in wildcard and lambda/stream flows.
  • Tests

    • Added regression coverage for receiver-based inferred generic instance method return types.
    • Added regression coverage ensuring lambda inference preserves generic instance method receiver typing.

@msridhar

msridhar commented Jul 25, 2026 •

Copy link
Copy Markdown
Collaborator Author

@codecov

codecov Bot commented Jul 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.06%. Comparing base (1767334) to head (39d5b75).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1654      +/-   ##
============================================
+ Coverage     88.04%   88.06%   +0.01%     
- Complexity     3074     3076       +2     
============================================
  Files           105      105              
  Lines         10351    10354       +3     
  Branches       2095     2095              
============================================
+ Hits           9114     9118       +4     
+ Misses          582      581       -1     
  Partials        655      655              

☔ 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 marked this pull request as ready for review July 25, 2026 05:27
@coderabbitai

coderabbitai Bot commented Jul 25, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

GenericsChecks centralizes invocation MethodType computation, including receiver substitution and handler overrides. Generic return inference, poly-expression caching, and constraint generation now use this shared method type. Two JSpecify tests cover receiver-dependent nullable return inference and lambda inference.

Possibly related PRs

  • uber/NullAway#1293: Both modify receiver-aware generic method inference in GenericsChecks.
  • uber/NullAway#1438: Both change modeled generic method types around invocation and lambda handling.
  • uber/NullAway#1571: Both address receiver-derived MethodType computation for generic instance methods.

Suggested labels: jspecify

Suggested reviewers: lazaroclapp, yuxincs

🚥 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 summarizes the main change: fixing a generic instance-method inference bug.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ 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 inference-wildcard-bug

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.

@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.

Don't understand the CI warnings on a random pre-existing comment, but modulo that, this LGTM!

@msridhar

Copy link
Copy Markdown
Collaborator Author

Don't understand the CI warnings on a random pre-existing comment, but modulo that, this LGTM!

Yeah, GitHub Actions is bringing in Javadoc warnings from the integration test jobs over to the code review tab now...should probably fix these warnings, but still.

@msridhar
msridhar force-pushed the inference-wildcard-bug branch from 34ffd0a to 4da9aa3 Compare July 26, 2026 00:10
Base automatically changed from inf-failure-warnings-in-experimental-mode to master July 26, 2026 20:41
@msridhar
msridhar force-pushed the inference-wildcard-bug branch from 4da9aa3 to 39d5b75 Compare July 26, 2026 20:59
@msridhar
msridhar enabled auto-merge (squash) July 26, 2026 20:59

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java (1)

890-890: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Preserve warning-enabled helpers for tests expecting inference failures.

These tests still contain // BUG: Diagnostic contains: inference failure..., but makeHelper() no longer enables WarnOnGenericInferenceFailure; runInferenceForCall() suppresses that diagnostic otherwise.

  • nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java#L890-L890: use a warning-enabled helper for localsWithTypesFromDataflow.
  • nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java#L935-L935: use it for nullableTypeVarToNonNullField.
  • nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java#L1239-L1239: use it for selfContainedOptional.
  • nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java#L1757-L1757: use it for nestedGenericMethodRepairPreservesTopLevelNullability.
  • nullaway/src/test/java/com/uber/nullaway/jspecify/WildcardTests.java#L505-L505: use it for superWildcardToConcreteTypeVariable.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java` at
line 890, Replace makeHelper() with the warning-enabled helper for the tests
localsWithTypesFromDataflow, nullableTypeVarToNonNullField,
selfContainedOptional, nestedGenericMethodRepairPreservesTopLevelNullability,
and superWildcardToConcreteTypeVariable. Apply this in GenericMethodTests.java
at lines 890-890, 935-935, 1239-1239, and 1757-1757, and WildcardTests.java at
line 505-505, preserving the existing inference-failure diagnostics.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java`:
- Line 890: Replace makeHelper() with the warning-enabled helper for the tests
localsWithTypesFromDataflow, nullableTypeVarToNonNullField,
selfContainedOptional, nestedGenericMethodRepairPreservesTopLevelNullability,
and superWildcardToConcreteTypeVariable. Apply this in GenericMethodTests.java
at lines 890-890, 935-935, 1239-1239, and 1757-1757, and WildcardTests.java at
line 505-505, preserving the existing inference-failure diagnostics.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: ae76f786-bb44-443d-bec0-83784f7a3b82

📥 Commits

Reviewing files that changed from the base of the PR and between 4da9aa3 and 39d5b75.

📒 Files selected for processing (3)
  • nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java
  • nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java
  • nullaway/src/test/java/com/uber/nullaway/jspecify/WildcardTests.java

@msridhar
msridhar merged commit 5ba2284 into master Jul 26, 2026
14 checks passed
@msridhar
msridhar deleted the inference-wildcard-bug branch July 26, 2026 21:13
msridhar pushed a commit that referenced this pull request Sep 28, 2026
Since #1654, the executable type used for inference is the method's type
as a member of its receiver, and computing it infers the receiver when
the receiver is itself a generic call. inferCallType computed it twice
for a call not yet inferred (constraints, then return type), and
runInferenceForCall a third time for lambda argument types. Inference
run from dataflow is never cached, so every generic call in a receiver
chain doubled the work below it: checking a fluent chain such as
`r.path(..).entity(..).isEqualTo(..)` repeated n times took O(4^n).

Compute the executable type once and pass it to runInferenceForCall and
generateConstraintsForCall. At 9 links, dataflow inference runs drop
from 524,259 to 315; a 96-link chain now checks in under 1s.

Adds GenericMethodTests.longChainOfGenericInstanceMethodCalls, which
times out on master and passes in about 0.1s with the fix. No existing
test's diagnostics change.

I used Claude Code to bisect the regression to #1654, find the cause and
write the fix and test. I have reviewed and understood all the changes.

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

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

## Summary by CodeRabbit

* **Bug Fixes**
* Improved generic method-call inference consistency, including for
nested calls and chained method calls.

* **Tests**
* Added a regression test covering nullable-argument detection in a long
chain of generic method calls.

<!-- 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.

2 participants