Skip to content

Fix wildcard inference bug with method references#1553

Merged
msridhar merged 3 commits into
masterfrom
wildcard-inference-2
May 3, 2026
Merged

Fix wildcard inference bug with method references#1553
msridhar merged 3 commits into
masterfrom
wildcard-inference-2

Conversation

@msridhar
Copy link
Copy Markdown
Collaborator

@msridhar msridhar commented Apr 27, 2026

Fixes #1552

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Improved accuracy of null-checking for generic types and method references by ensuring proper type resolution.
  • Tests

    • Enhanced test coverage for generic type nullability scenarios.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.45%. Comparing base (29a896a) to head (f379612).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #1553   +/-   ##
=========================================
  Coverage     88.45%   88.45%           
  Complexity     2869     2869           
=========================================
  Files           103      103           
  Lines          9586     9587    +1     
  Branches       1930     1930           
=========================================
+ Hits           8479     8480    +1     
  Misses          532      532           
  Partials        575      575           

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

@msridhar msridhar marked this pull request as ready for review April 27, 2026 21:14
@msridhar msridhar force-pushed the wildcard-inference-experiment branch from 8a2f2aa to 2ebfce6 Compare April 28, 2026 19:46
@msridhar msridhar force-pushed the wildcard-inference-2 branch 2 times, most recently from 2fe233b to ed48bd5 Compare April 28, 2026 19:56
@msridhar msridhar force-pushed the wildcard-inference-experiment branch from ea94de5 to 2f392ee Compare April 29, 2026 21:36
@msridhar msridhar force-pushed the wildcard-inference-2 branch from ed48bd5 to 6ec1a79 Compare April 29, 2026 21:36
@msridhar msridhar force-pushed the wildcard-inference-experiment branch from 2f392ee to adb858c Compare May 1, 2026 15:09
@msridhar msridhar force-pushed the wildcard-inference-2 branch from 6ec1a79 to fc48e50 Compare May 1, 2026 15:10
@msridhar
Copy link
Copy Markdown
Collaborator Author

msridhar commented May 1, 2026

list.stream().map(Test::mapToNull).forEach(s -> {
if (s != null) { s.hashCode(); }
});
list.stream().map(Test::mapToNull).forEach(Test::doNothing);
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.

Probably also worth checking that, without the map to null, a Stream<String> works just fine with .forEach(Test::callHashCode);, no?

@msridhar msridhar force-pushed the wildcard-inference-2 branch from fc48e50 to b3118a6 Compare May 3, 2026 01:18
Base automatically changed from wildcard-inference-experiment to master May 3, 2026 19:10
@msridhar msridhar force-pushed the wildcard-inference-2 branch from f027fde to f379612 Compare May 3, 2026 19:11
@msridhar msridhar enabled auto-merge (squash) May 3, 2026 19:11
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 3, 2026

Walkthrough

The PR modifies GenericsChecks.java to call maybeStorePolyExpressionTypeFromTarget for MemberReferenceTree before processing method-reference type relations, ensuring subsequent type lookups use target-derived inferred poly-expression types. Additionally, WildcardTests.java is updated to expand the mapStreamValuesToNullable test with separate positive and negative test methods, introducing helper functions and adjusting BUG diagnostic expectations to cover nullability mismatches in mapped values and functional-interface arguments.

Possibly related issues

  • Issue #1552: This PR directly addresses the missing error with wildcard inference when passing a method reference instead of a lambda by implementing the poly-expression type storage for MemberReferenceTree that was referenced in the TODO comment.

Possibly related PRs

  • PR #1430: Both PRs modify GenericsChecks to treat MemberReferenceTree like poly-expressions by storing and using inferred poly-expression types for method references.
  • PR #1438: Both PRs improve handling of MemberReferenceTree in generic and method-reference inference within GenericsChecks.
  • PR #1556: Both changes address nullability inference for method references in call-type checking by storing target-derived poly-expression types for MemberReferenceTree.

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 50.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 'Fix wildcard inference bug with method references' directly summarizes the main change: fixing a wildcard inference bug that occurs when method references are used instead of lambdas.
Linked Issues check ✅ Passed The code changes in GenericsChecks.java and WildcardTests.java directly address issue #1552 by implementing the fix for missing error detection with wildcard inference when method references are passed.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the wildcard inference bug with method references as specified in issue #1552; no out-of-scope modifications detected.

✏️ 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 wildcard-inference-2

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
Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.

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

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

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/main/java/com/uber/nullaway/generics/GenericsChecks.java (1)

1601-1622: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Store the target-derived poly type before relation checks.

At Line 1621, maybeStorePolyExpressionTypeFromTarget(...) runs after processMethodRefTypeRelations(...) and this branch then returns. That ordering can miss target-annotated type info during the relation check itself.

Suggested fix
               if (currentActualParam instanceof MemberReferenceTree memberReferenceTree) {
+                maybeStorePolyExpressionTypeFromTarget(currentActualParam, formalParameter);
                 // the type of the method reference tree provided by javac may not capture
                 // nullability of nested types. So, do explicit type checks based on the return and
                 // parameter types of the referenced method
                 GenericsUtils.processMethodRefTypeRelations(
                     this,
@@
                             reportInvalidMethodReferenceParameterTypeError(
                                 memberReferenceTree, subtype, supertype, state);
                           }
                         }
                     });
-                maybeStorePolyExpressionTypeFromTarget(currentActualParam, formalParameter);
                 return;
               }
🤖 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/generics/GenericsChecks.java` around
lines 1601 - 1622, The poly-expression target-derived type is being stored too
late: call maybeStorePolyExpressionTypeFromTarget(currentActualParam,
formalParameter) before invoking
GenericsUtils.processMethodRefTypeRelations(...) so that the relation checks
(inside processMethodRefTypeRelations involving
reportInvalidMethodReferenceReturnTypeError and
reportInvalidMethodReferenceParameterTypeError) see any target-provided
annotations; move the maybeStorePolyExpressionTypeFromTarget call to immediately
prior to the processMethodRefTypeRelations(...) invocation in the branch
handling currentActualParam instanceof MemberReferenceTree.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java`:
- Around line 1601-1622: The poly-expression target-derived type is being stored
too late: call maybeStorePolyExpressionTypeFromTarget(currentActualParam,
formalParameter) before invoking
GenericsUtils.processMethodRefTypeRelations(...) so that the relation checks
(inside processMethodRefTypeRelations involving
reportInvalidMethodReferenceReturnTypeError and
reportInvalidMethodReferenceParameterTypeError) see any target-provided
annotations; move the maybeStorePolyExpressionTypeFromTarget call to immediately
prior to the processMethodRefTypeRelations(...) invocation in the branch
handling currentActualParam instanceof MemberReferenceTree.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 2b83059c-dcbe-4ccc-a131-2ea10ae66901

📥 Commits

Reviewing files that changed from the base of the PR and between 29a896a and f379612.

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

@msridhar msridhar merged commit 8f7c7e5 into master May 3, 2026
14 of 22 checks passed
@msridhar msridhar deleted the wildcard-inference-2 branch May 3, 2026 19:22
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.

Missing error with wildcard inference when passing a method reference instead of a lambda

2 participants