Skip to content

Fix type parameter nullability matching for covariant types#1533

Closed
utafrali wants to merge 1 commit intouber:masterfrom
utafrali:fix/issue-1528-possible-regression-in-type-parameter-nu
Closed

Fix type parameter nullability matching for covariant types#1533
utafrali wants to merge 1 commit intouber:masterfrom
utafrali:fix/issue-1528-possible-regression-in-type-parameter-nu

Conversation

@utafrali
Copy link
Copy Markdown

@utafrali utafrali commented Apr 14, 2026

Fixes #1528

There's a regression in type parameter nullability matching for covariant generics. CheckIdenticalNullabilityVisitor was being too strict, requiring identical nullability on both sides even when the type system allows for more flexibility.

Added a covariant mode to the visitor that handles this correctly. In covariant positions, RHS can be @nullable while LHS isn't (but not the reverse). Updated the comparison logic for both type arguments and array components.

Tests added to cover this scenario.

Summary by CodeRabbit

  • Bug Fixes

    • Improved nullability checking for generic method reference parameters to use covariant semantics. The checker now correctly handles cases where a method reference's parameter is more lenient (nullable) than the functional interface requirement, preventing false positives in valid code patterns.
  • Tests

    • Added test coverage for method reference generic parameter nullability covariance scenarios.

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@msridhar
Copy link
Copy Markdown
Collaborator

Hi @utafrali at a high level this seems wrong to me. Can you point to where in the JLS / JSpecify specification this covariant mode for type arguments is discussed?

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 14, 2026

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: 3cee3cf1-0bf5-4ba5-913a-93af6370a1af

📥 Commits

Reviewing files that changed from the base of the PR and between 334016d and 4707366.

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

Walkthrough

This pull request adds covariant nullability checking for generic type parameters in method references. It modifies CheckIdenticalNullabilityVisitor to accept a covariant flag that alters mismatch detection: when enabled, only RHS @Nullable vs LHS non-null is considered a failure, not the reverse. A new helper method covariantTypeParameterNullability in GenericsChecks delegates to this covariant mode. Method reference parameter type relations now use covariant checking instead of the previous symmetric approach. Tests are added to verify the new behavior for method reference argument nullability.

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 27.27% 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: introducing covariant type handling for type parameter nullability matching in the visitor.
Linked Issues check ✅ Passed Changes implement covariant nullability checking via a new mode in CheckIdenticalNullabilityVisitor and update GenericsChecks to use it for method reference parameters, directly addressing issue #1528's regression.
Out of Scope Changes check ✅ Passed All changes are narrowly focused on fixing covariant type parameter nullability handling in method reference contexts, with no unrelated modifications detected.

✏️ 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.

@utafrali
Copy link
Copy Markdown
Author

Good point, let me double-check this against the spec and revise if needed.

@msridhar
Copy link
Copy Markdown
Collaborator

Closing as this fix is invalid; see #1528 (comment)

@msridhar msridhar closed this Apr 29, 2026
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.

Possible regression in type parameter nullability matching in 0.13.2

3 participants