Skip to content

Improve handling of wildcard upper bounds in generic method inference#1454

Merged
msridhar merged 27 commits intouber:masterfrom
dhruv-agr:generic-method-lambda-arg-wildcard
Feb 1, 2026
Merged

Improve handling of wildcard upper bounds in generic method inference#1454
msridhar merged 27 commits intouber:masterfrom
dhruv-agr:generic-method-lambda-arg-wildcard

Conversation

@dhruv-agr
Copy link
Contributor

@dhruv-agr dhruv-agr commented Jan 27, 2026

Fixes #1350
TypeSubstitutionUtils.updateTypeWithInferredNullability was treating the wildcard as a mismatch for the concrete type and failed to copy the @Nullable annotation from the bound (R) to the concrete type (Object). Modified TypeSubstitutionUtils.java to explicitly handle Type.WildcardType in the RestoreNullnessAnnotationsVisitor. Specifically, when the "other" type is an extends wildcard, we should recurse on its upper bound

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of wildcard types with extends bounds so nullability restoration for generics behaves correctly.
  • Tests

    • Added tests covering generic method inference involving wildcards in lambda expressions and method references.

✏️ Tip: You can customize this high-level summary in your review settings.

@codecov
Copy link

codecov bot commented Jan 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.41%. Comparing base (4038162) to head (596dcf8).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #1454   +/-   ##
=========================================
  Coverage     88.41%   88.41%           
  Complexity     2715     2715           
=========================================
  Files            99       99           
  Lines          9019     9022    +3     
  Branches       1803     1805    +2     
=========================================
+ Hits           7974     7977    +3     
  Misses          517      517           
  Partials        528      528           

☔ 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 February 1, 2026 22:30
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 1, 2026

Walkthrough

This change adds special-case handling in TypeSubstitutionUtils.RestoreNullnessAnnotationsVisitor.visitClassType to detect BoundKind.EXTENDS and, for wildcard class types with an extends bound, recurse into the wildcard's extends bound instead of applying nullability updates directly to the wildcard. BoundKind is imported to support the check. The change also adds two tests in GenericMethodLambdaOrMethodRefArgTests: genericMethodLambdaArgWildCard and genericMethodMethodRefWildCard, exercising generic inference with Function<? super T, ? extends @nullable R> mapper scenarios.

Possibly related PRs

Suggested labels

jspecify

Suggested reviewers

  • yuxincs
  • lazaroclapp
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.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 accurately describes the main change: improving handling of wildcard upper bounds in generic method inference, which directly reflects the code modifications to TypeSubstitutionUtils and the new test cases.
Linked Issues check ✅ Passed The PR successfully implements the objective from #1350 by adding explicit handling for Type.WildcardType with EXTENDS bounds in RestoreNullnessAnnotationsVisitor, enabling nullability propagation from wildcard bounds to concrete types, and includes test cases for lambda and method reference scenarios.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the stated objective: modifications to TypeSubstitutionUtils for wildcard handling and addition of targeted test cases for generic method inference with wildcards.

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

Copy link
Collaborator

@msridhar msridhar left a comment

Choose a reason for hiding this comment

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

Nice find @dhruv-agr!

I'm a bit uncomfortable with this change; I feel like it should break some case. But, I can't come up with a test that fails with it. So, let's go ahead and land it, but re-visit whether we need this when we implement more full support for wildcards.

@msridhar msridhar enabled auto-merge (squash) February 1, 2026 22:45
Copy link
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.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In
`@nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodLambdaOrMethodRefArgTests.java`:
- Line 448: Remove the unused inner class Foo declared as "class Foo<T extends
`@Nullable` Object> {}" inside the GenericMethodLambdaOrMethodRefArgTests test;
locate the declaration of the inner class Foo and delete it so the test class no
longer contains this unused leftover type.

@msridhar msridhar disabled auto-merge February 1, 2026 22:48
Copy link
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.

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In
`@nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java`:
- Around line 296-303: Replace the enum comparison
wt.kind.equals(BoundKind.EXTENDS) with the idiomatic and null-safe reference
equality check wt.kind == BoundKind.EXTENDS inside the TypeSubstitutionUtils
logic that handles other instanceof Type.WildcardType (the branch using wt and
BoundKind.EXTENDS) so the code compares enum values by == rather than .equals().

@msridhar msridhar enabled auto-merge (squash) February 1, 2026 23:03
@msridhar msridhar disabled auto-merge February 1, 2026 23:03
@msridhar msridhar merged commit 94f8725 into uber:master Feb 1, 2026
12 checks passed
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.

Improve handling of wildcard upper bounds in generic method inference

2 participants

Comments