Skip to content

Better support for conditional expressions in JSpecify mode#1608

Open
msridhar wants to merge 13 commits into
masterfrom
jspecify-conditional-expr-wildcard
Open

Better support for conditional expressions in JSpecify mode#1608
msridhar wants to merge 13 commits into
masterfrom
jspecify-conditional-expr-wildcard

Conversation

@msridhar

@msridhar msridhar commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

We improve support for conditional expressions in JSpecify mode, by properly inferring types of conditional expressions where appropriate. We also propagate types from the assignment context through (nested) conditional expressions to correctly infer types of generic method calls nested within such expressions. See the new ConditionalExprTests for examples.

We extract out some common code for finding a type from the assignment context given a TreePath into a new method getTargetTypeFromParentContext, and now use it consistently to handle getting assignment contexts for conditional expressions, generic method calls, and diamond constructor calls. As part of this extraction, we make more consistent our handling of var-declared locals (ensuring we don't use their javac-inferred type during our own inference), and we add tests for this.

Apologies for the size of this PR; it would have been a bit awkward to make it smaller.

Summary by CodeRabbit

Release Notes

  • New Features

    • Improved handling of conditional (?:) expressions for nullability analysis and generic type inference.
    • Enhanced type inference across assignment, method calls, and constructor (including diamond) scenarios, including nested conditionals and dataflow-guarded branches.
    • More accurate results when conditional expressions are used with var (when no target type is available).
  • Tests

    • Added/expanded JUnit coverage for conditional-expression nullability, generic inference, diamond inference, and dataflow/loop interactions.

@msridhar

msridhar commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator Author

This change is part of the following stack:

Change managed by git-spice.

@msridhar msridhar marked this pull request as draft June 15, 2026 05:20
@coderabbitai

coderabbitai Bot commented Jun 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

GenericsChecks now treats conditional expressions as poly expressions with context-derived target types, cached inferred types, and dedicated inference paths for assignments, returns, calls, diamonds, and nullability checking. The conditional nullability check now resolves the surrounding target context before validating both branches. New tests add coverage for conditional-expression nullability, generic inference, dataflow, loops, and var-related diamond and generic method inference cases.

Possibly related PRs

  • uber/NullAway#1464: Changes diamond-constructor inference and parent-context target typing in the same area of GenericsChecks.
  • uber/NullAway#1499: Extends the poly-expression inferred-type caching machinery that this PR also uses for conditionals.
  • uber/NullAway#1430: Introduces the poly-expression type-inference pattern for lambdas and method references that this PR expands to conditionals.

Suggested labels: codex

Suggested reviewers: yuxincs, lazaroclapp

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 31.82% 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 clearly summarizes the main change: improved conditional-expression support in JSpecify mode.
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 jspecify-conditional-expr-wildcard

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.

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

Actionable comments posted: 1

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

Inline comments:
In `@nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java`:
- Around line 2088-2097: The `inferConditionalExpressionType` method returns a
nullable Type, but the result assigned to `actualParameterType` is used without
a null check. Add a null check immediately after the assignment from
`inferConditionalExpressionType` (within the conditional block for
ConditionalExpressionTree) to ensure `actualParameterType` is not null before it
is used at line 2098 in the `subtypeParameterNullability` call. If the result is
null, handle it appropriately by continuing to the next iteration or setting a
default value, similar to how other branches handle edge cases.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: b9f9ce7e-22a5-4370-9033-dd6605649ea9

📥 Commits

Reviewing files that changed from the base of the PR and between 0b3145b and 6b53e13.

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

@codecov

codecov Bot commented Jun 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.92857% with 27 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.17%. Comparing base (addfe41) to head (7e0c16b).

Files with missing lines Patch % Lines
...ava/com/uber/nullaway/generics/GenericsChecks.java 83.92% 9 Missing and 18 partials ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1608      +/-   ##
============================================
+ Coverage     88.10%   88.17%   +0.07%     
- Complexity     3046     3071      +25     
============================================
  Files           105      105              
  Lines         10181    10286     +105     
  Branches       2062     2083      +21     
============================================
+ Hits           8970     9070     +100     
- Misses          572      575       +3     
- Partials        639      641       +2     

☔ 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 force-pushed the jspecify-conditional-expr-wildcard branch 2 times, most recently from 77cd8fd to 464e44a Compare June 16, 2026 23:11
@msridhar msridhar force-pushed the jspecify-conditional-expr-wildcard branch 3 times, most recently from 7e7240a to 516ec37 Compare June 26, 2026 17:01
@msridhar msridhar force-pushed the jspecify-conditional-expr-wildcard branch from 516ec37 to 14ba0f3 Compare July 3, 2026 21:12
@msridhar msridhar changed the title [draft] Better support for conditional expressions in JSpecify mode Better support for conditional expressions in JSpecify mode Jul 4, 2026
@msridhar msridhar marked this pull request as ready for review July 4, 2026 01:32
@msridhar msridhar requested a review from lazaroclapp July 4, 2026 01:34

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

Actionable comments posted: 1

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

Inline comments:
In `@nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java`:
- Around line 1872-1888: In getConditionalExpressionType, the cached
assignment-context type can still be raw and should not be returned or stored
as-is. Update the logic around inferredPolyExpressionTypes and
getTargetTypeForConditionalExpression so that any typeFromAssignmentContext is
passed through typeOrNullIfRaw(...) before caching and before returning,
ensuring inferConditionalExpressionType never sees a raw conditional target.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: c5df7e9e-5c51-46eb-9730-a74f0e63eb9b

📥 Commits

Reviewing files that changed from the base of the PR and between 6b53e13 and 7e0c16b.

📒 Files selected for processing (4)
  • nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java
  • nullaway/src/test/java/com/uber/nullaway/jspecify/ConditionalExprTests.java
  • nullaway/src/test/java/com/uber/nullaway/jspecify/GenericDiamondTests.java
  • nullaway/src/test/java/com/uber/nullaway/jspecify/VarDeclaredLocalTests.java

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.

1 participant