Skip to content

Fixes related to captured types and inference - #1655

Merged
msridhar merged 1 commit into
masterfrom
var-generic-method-inference-bug
Jul 26, 2026
Merged

Fixes related to captured types and inference#1655
msridhar merged 1 commit into
masterfrom
var-generic-method-inference-bug

Conversation

@msridhar

@msridhar msridhar commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator

Some initial fixes related to handling of captured types; see #1656 for more issues to handle later.

Here's the relevant example for this one:

final class Test {
    static void reproduce() {
        var future = future();
        run(() -> future.join());
    }
    private static CompletableFuture<?> future() {
        return new CompletableFuture<>();
    }
    private static <T extends @Nullable Object> T run(Action<T> action) {
        return action.run();
    }
    private interface Action<T extends @Nullable Object> {
        T run();
    }
}

The future local variable gets a type CompletableFuture<CAP#1>, where CAP#1 is a captured type variable whose upper bound is @Nullable. Hence, the lambda () -> future.join() may return @Nullable, which is ok given the upper bound of T for run. Before we would report a spurious warning for this case. We also add a test that when T's upper bound is @NonNull we still get an error.

The fix here is to handle captured types exactly like type variables when generating inference constraints and restoring nullness annotations.

Summary by CodeRabbit

  • Bug Fixes
    • Improved nullability constraint propagation for captured generic types.
    • Ensured direct nullability annotations are restored correctly when captured types are involved.
    • Fixed related generic inference behavior in lambda scenarios with nullable upper/lower bounds.
  • Tests
    • Added new generic lambda tests covering CompletableFuture.join(), including verification of expected diagnostics for conflicting nullability constraints.

@msridhar

msridhar commented Jul 25, 2026

Copy link
Copy Markdown
Collaborator Author

@codecov

codecov Bot commented Jul 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.06%. Comparing base (5ba2284) to head (b01c318).

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #1655   +/-   ##
=========================================
  Coverage     88.06%   88.06%           
  Complexity     3076     3076           
=========================================
  Files           105      105           
  Lines         10354    10356    +2     
  Branches       2095     2095           
=========================================
+ Hits           9118     9120    +2     
  Misses          581      581           
  Partials        655      655           

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

@coderabbitai

coderabbitai Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Adds explicit CapturedType handling to subtype constraint propagation and nullness annotation restoration. Adds generic lambda tests covering inference from CompletableFuture.join(), including nullable and conflicting non-null upper bounds.

Possibly related issues

Possibly related PRs

  • uber/NullAway#1454 — Updates nullness restoration for another wildcard-related type shape.
  • uber/NullAway#1549 — Extends generic constraint handling for wildcard-captured types.
  • uber/NullAway#1575 — Addresses lambda inference involving wildcards and captured types.

Suggested labels: jspecify

Suggested reviewers: yuxincs

🚥 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 matches the main theme of the changes: captured-type handling and inference-related fixes.
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 var-generic-method-inference-bug

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.

@msridhar
msridhar force-pushed the var-generic-method-inference-bug branch from a525370 to 6d57adf Compare July 25, 2026 23:53
@msridhar
msridhar force-pushed the inference-wildcard-bug branch from 34ffd0a to 4da9aa3 Compare July 26, 2026 00:10
@msridhar
msridhar force-pushed the var-generic-method-inference-bug branch from 6d57adf to fe86b32 Compare July 26, 2026 00:10

@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/test/java/com/uber/nullaway/jspecify/GenericLambdaTests.java`:
- Around line 188-192: Update both generated Java source strings in
GenericLambdaTests, including the snippets near makeHelper(), to declare package
com.uber before their imports. Keep the existing imports and test logic
unchanged so AnnotatedPackages=com.uber applies to both 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 Plus

Run ID: 1bafe23e-8204-4cd6-b309-bf347a785089

📥 Commits

Reviewing files that changed from the base of the PR and between 6d57adf and fe86b32.

📒 Files selected for processing (3)
  • nullaway/src/main/java/com/uber/nullaway/generics/ConstraintSolverImpl.java
  • nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java
  • nullaway/src/test/java/com/uber/nullaway/jspecify/GenericLambdaTests.java

@msridhar
msridhar force-pushed the inference-wildcard-bug branch from 4da9aa3 to 39d5b75 Compare July 26, 2026 20:59
Base automatically changed from inference-wildcard-bug to master July 26, 2026 21:13
@msridhar
msridhar force-pushed the var-generic-method-inference-bug branch from fe86b32 to b01c318 Compare July 26, 2026 21:40
@msridhar
msridhar enabled auto-merge (squash) July 26, 2026 21:40

@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/test/java/com/uber/nullaway/jspecify/GenericLambdaTests.java`:
- Around line 183-210: Add Javadoc describing the nullable-upper-bound inference
scenario for varAndGenericInference in
nullaway/src/test/java/com/uber/nullaway/jspecify/GenericLambdaTests.java lines
183-210. Also add Javadoc describing the conflicting non-null upper-bound
diagnostic scenario for the test method in lines 212-240; no other changes are
needed.
🪄 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 Plus

Run ID: 881d0b62-6755-4159-b4d0-fd732082badb

📥 Commits

Reviewing files that changed from the base of the PR and between fe86b32 and b01c318.

📒 Files selected for processing (3)
  • nullaway/src/main/java/com/uber/nullaway/generics/ConstraintSolverImpl.java
  • nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java
  • nullaway/src/test/java/com/uber/nullaway/jspecify/GenericLambdaTests.java

Comment on lines +183 to +210
@Test
public void varAndGenericInference() {
makeHelper()
.addSourceLines(
"Test.java",
"""
import java.util.concurrent.CompletableFuture;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
final class Test {
static void reproduce() {
var future = future();
run(() -> future.join());
}
private static CompletableFuture<?> future() {
return new CompletableFuture<>();
}
private static <T extends @Nullable Object> T run(Action<T> action) {
return action.run();
}
private interface Action<T extends @Nullable Object> {
T run();
}
}
""")
.doTest();
}

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.

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Add Javadoc for the new non-trivial test methods.

  • nullaway/src/test/java/com/uber/nullaway/jspecify/GenericLambdaTests.java#L183-L210: Document the nullable-upper-bound inference scenario.
  • nullaway/src/test/java/com/uber/nullaway/jspecify/GenericLambdaTests.java#L212-L240: Document the conflicting non-null upper-bound diagnostic scenario.

As per coding guidelines, “Add Javadoc to every non-trivial method, including private methods.”

📍 Affects 1 file
  • nullaway/src/test/java/com/uber/nullaway/jspecify/GenericLambdaTests.java#L183-L210 (this comment)
  • nullaway/src/test/java/com/uber/nullaway/jspecify/GenericLambdaTests.java#L212-L240
🤖 Prompt for 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.

In `@nullaway/src/test/java/com/uber/nullaway/jspecify/GenericLambdaTests.java`
around lines 183 - 210, Add Javadoc describing the nullable-upper-bound
inference scenario for varAndGenericInference in
nullaway/src/test/java/com/uber/nullaway/jspecify/GenericLambdaTests.java lines
183-210. Also add Javadoc describing the conflicting non-null upper-bound
diagnostic scenario for the test method in lines 212-240; no other changes are
needed.

Source: Coding guidelines

@msridhar
msridhar merged commit 53f5dc0 into master Jul 26, 2026
14 checks passed
@msridhar
msridhar deleted the var-generic-method-inference-bug branch July 26, 2026 21:56
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