Skip to content

More generics inference fixes#1255

Merged
msridhar merged 21 commits intogenerics-type-constraint-solverfrom
more-generics-inference-fixes
Aug 24, 2025
Merged

More generics inference fixes#1255
msridhar merged 21 commits intogenerics-type-constraint-solverfrom
more-generics-inference-fixes

Conversation

@msridhar
Copy link
Copy Markdown
Collaborator

@msridhar msridhar commented Aug 23, 2025

Three fixes:

  1. There may be multiple TypeVariable objects representing the same underlying type variable due to differences in annotations (e.g., T vs @Nullable T). Switch our maps key'd on TypeVariable to use the underlying Element instead, which should uniquely identify the type variable.
  2. Previously when handling constraints, we would treat types like @Nullable T as if they were the same as just T; this was wrong. Now, only treat TypeVariable objects with no nullness annotations as actual type variables whose nullability must be inferred.
  3. Before we didn't properly determine whether an AssignmentTree was assigning to a local variable.

For testing, we also add a new config flag WarnOnGenericInferenceFailure to allow for a warning to be emitted when inference fails for a generic method call. This way, we can test the expectation that generic inference fails for certain calls. This is off by default; I'm still unsure how best to expose these inference-related errors to the user.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Aug 23, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch more-generics-inference-fixes

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@codecov
Copy link
Copy Markdown

codecov bot commented Aug 23, 2025

Codecov Report

❌ Patch coverage is 88.88889% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.19%. Comparing base (4d710ae) to head (4cf6381).
⚠️ Report is 1 commits behind head on generics-type-constraint-solver.

Files with missing lines Patch % Lines
...m/uber/nullaway/generics/ConstraintSolverImpl.java 88.00% 1 Missing and 2 partials ⚠️
...ava/com/uber/nullaway/generics/GenericsChecks.java 87.50% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@                        Coverage Diff                         @@
##             generics-type-constraint-solver    #1255   +/-   ##
==================================================================
  Coverage                              88.18%   88.19%           
- Complexity                              2417     2424    +7     
==================================================================
  Files                                     92       92           
  Lines                                   7981     8002   +21     
  Branches                                1587     1593    +6     
==================================================================
+ Hits                                    7038     7057   +19     
  Misses                                   491      491           
- Partials                                 452      454    +2     

☔ 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 requested a review from lazaroclapp August 23, 2025 18:42
@msridhar msridhar marked this pull request as ready for review August 23, 2025 18:42
@msridhar msridhar marked this pull request as draft August 24, 2025 15:01
@msridhar msridhar marked this pull request as ready for review August 24, 2025 15:48
Copy link
Copy Markdown
Collaborator

@lazaroclapp lazaroclapp left a comment

Choose a reason for hiding this comment

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

Very minor comments below. Would personally prefer a clearer title for the PR, but I know this will land squashed with the previous PR, so it's less of an issue :)

@@ -435,6 +436,8 @@ public void checkTypeParameterNullnessForAssignability(Tree tree, VisitorState s
} else {
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.

Maybe an else if (tree instanceof AssignmentTree) { with a error handling } else { branch? Or some precondition check? I know the contract is the function takes only those two types, but just in terms of defensive programming.

An argument type like AssignmentTree | VariableTree tree would be great, if Java supported that 😅

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.

Sure, I can do an else if here to give a cleaner error message if something weird happens 🙂

" }",
"}")
.doTest();
}
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.

Test case might benefit from a quick explanation of what's being tested, since no reports are expected. From looking at the test alone, the answer is that we are testing constraint resolution when method invocations are passed as parameters to other methods with some nested type variable on the signature of both methods. But given the PR description, it is a bit more specific than that, no? We are making sure we avoid conflating T and @Nullable T, right?

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.

Yeah, I'll add a comment. This code was reduced from some code in Spring that was giving weird inference failures, and yes, it was due to not properly distinguishing T and @Nullable T

" result = make();",
" }",
"}")
.doTest();
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.

Not that it needs docs or anything, but I am curious here... did this fail before this PR in some way?

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.

It did fail! Before, we didn't properly mark when AssignmentTrees like result = make() were assigning to local variables (we only handled initializers in VariableTrees). So, when inference ran, it thought that the result variable had to be @NonNull, which then caused inference to fail for the make() due to the constraint @Nullable U <: @NonNull Object. Now, we properly detect this is an assignment to a local variable, so we do not generate that constraint (since types of locals are inferred). I'll add a comment on this one too.

@msridhar msridhar merged commit ee2fdd6 into generics-type-constraint-solver Aug 24, 2025
12 checks passed
@msridhar msridhar deleted the more-generics-inference-fixes branch August 24, 2025 23:16
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