Skip to content

Record when generic method inference fails#1280

Merged
msridhar merged 3 commits intomasterfrom
record-inference-failures
Sep 11, 2025
Merged

Record when generic method inference fails#1280
msridhar merged 3 commits intomasterfrom
record-inference-failures

Conversation

@msridhar
Copy link
Copy Markdown
Collaborator

@msridhar msridhar commented Sep 7, 2025

This may be a mild performance optimization, as we will no longer repeatedly perform inference for a call if it failed the first time. More importantly, for future changes like fixing #1263, it may be useful to be able to assert that inference has not already run for certain calls, and this change enables such assertions.

Summary by CodeRabbit

  • Improvements
    • More reliable generic method inference with caching that records both successes and failures to avoid repeated work and improve consistency.
  • Bug Fixes
    • Varargs inference diagnostic now correctly attributes the issue to the null literal for clearer error messages.
  • Tests
    • Updated test expectations to reflect the refined varargs diagnostic behavior.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Sep 7, 2025

Walkthrough

Adds a MethodInferenceResult abstraction (success/failure) and updates the generic-method inference cache and lookup in GenericsChecks to store and consult success or failure results; updates substitution to apply inferred nullability only on success. Adjusts one test expectation for a varargs diagnostic.

Changes

Cohort / File(s) Summary
Generic inference result and caching
nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java
Introduces MethodInferenceResult with InferenceSuccess and InferenceFailure. Replaces inferredTypeVarNullabilityForGenericCalls map to store MethodInferenceResult. inferGenericMethodCallType reads/writes the new cache, caches failures, and preserves warning/error behavior. substituteTypeArgsInGenericMethodType applies inferred nullability only when InferenceSuccess is cached.
Test expectation update
nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java
Adjusts the varargsInference test expected diagnostic: third argument now expects diagnostic referencing the literal null (not makeStr(null)).

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant Caller
  participant GenericsChecks
  participant Cache as Inference Cache

  Caller->>GenericsChecks: inferGenericMethodCallType(invocation)
  GenericsChecks->>Cache: get(invocation)
  alt Cache hit: InferenceSuccess
    Cache-->>GenericsChecks: InferenceSuccess(typeVarNullability)
    GenericsChecks->>GenericsChecks: use inferred nullability
  else Cache hit: InferenceFailure
    Cache-->>GenericsChecks: InferenceFailure(msg)
    GenericsChecks->>GenericsChecks: emit warning/error (cached failure)
  else Cache miss
    GenericsChecks->>GenericsChecks: run constraint inference
    alt inference succeeds
      GenericsChecks->>Cache: put(invocation, InferenceSuccess(...))
      GenericsChecks->>GenericsChecks: use inferred nullability
    else inference fails (UnsatisfiableConstraintsException)
      GenericsChecks->>Cache: put(invocation, InferenceFailure(msg))
      GenericsChecks->>GenericsChecks: warnOnGenericInferenceFailure
    end
  end
  GenericsChecks-->>Caller: result
Loading
sequenceDiagram
  autonumber
  participant Caller
  participant GenericsChecks
  participant Cache as Inference Cache

  Caller->>GenericsChecks: substituteTypeArgsInGenericMethodType(methodType, invocation)
  alt explicit type args present
    GenericsChecks->>Caller: substitute explicit args
  else no explicit args
    GenericsChecks->>Cache: get(invocation)
    alt InferenceSuccess
      Cache-->>GenericsChecks: inferred nullability
      GenericsChecks->>Caller: apply inferred nullability
    else InferenceFailure or miss
      GenericsChecks->>Caller: fallback substitution (no inferred nullability)
    end
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • yuxincs
  • lazaroclapp
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch record-inference-failures

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.

" Foo<@Nullable String> foo4 = make(\"hello\", \"world\");",
" Foo<@Nullable String> foo5 = make(\"hello\", \"world\", makeStr(null));",
" // BUG: Diagnostic contains: passing @Nullable parameter 'makeStr(null)' where @NonNull is required",
" // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required",
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.

I'm not 100% sure why this error message changed. But the new message is as valid as the old one, and given all the other tests still pass, I'm not concerned.

Copy link
Copy Markdown
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: 0

🧹 Nitpick comments (3)
nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java (3)

90-96: Fix Javadoc: keys are method invocations only (constructors not cached here)

The comment mentions “method or constructor,” but the map key is MethodInvocationTree and inference here excludes NewClassTree. Tighten the wording to avoid confusion.

Apply:

-  /**
-   * Maps a Tree representing a call to a generic method or constructor to the result of inferring
-   * its type argument nullability. The call must not have any explicit type arguments. If a tree is
-   * not present as a key in this map, it means inference has not yet been attempted for that call.
-   */
+  /**
+   * Maps a MethodInvocationTree (a generic method call with no explicit type arguments)
+   * to the result of inferring its type-argument nullability. If a tree is not present
+   * as a key, inference has not yet been attempted for that call.
+   *
+   * Note: constructor/diamond inference is not cached here.
+   */

525-569: Failure caching assumptions: document the invariant

Caching InferenceFailure prevents re-attempts later for the same call. That relies on the invariant that a given invocation tree is analyzed under a single assignment/parameter context within a compilation unit. Add a brief comment to make this assumption explicit.

-        for (MethodInvocationTree invTree : allInvocations) {
+        // Cache success for the top-level invocation and any nested invocations that required
+        // inference. This assumes each invocation tree appears under a single assignment context
+        // within a compilation unit.
+        for (MethodInvocationTree invTree : allInvocations) {
           inferredTypeVarNullabilityForGenericCalls.put(
               invTree, new InferenceSuccess(typeVarNullability));
         }
       } catch (UnsatisfiableConstraintsException e) {
@@
-        for (MethodInvocationTree invTree : allInvocations) {
+        // Cache failures to avoid repeated, futile inference attempts for the same trees in this CU.
+        // See note above about the single-context invariant.
+        for (MethodInvocationTree invTree : allInvocations) {
           inferredTypeVarNullabilityForGenericCalls.put(
               invTree, new InferenceFailure(e.getMessage()));
         }

1163-1172: Apply cached inference only for methods: good; update method Javadoc accordingly

Using cached InferenceSuccess for calls without explicit type args is correct; skipping constructors is consistent with current scope. Consider updating the Javadoc of substituteTypeArgsInGenericMethodType to say it accepts a method invocation or constructor and only applies cached inference to method invocations.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8f40500 and 8593784.

📒 Files selected for processing (2)
  • nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java (5 hunks)
  • nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📚 Learning: 2025-08-28T04:54:20.953Z
Learnt from: msridhar
PR: uber/NullAway#1248
File: nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java:847-857
Timestamp: 2025-08-28T04:54:20.953Z
Learning: In NullAway's GenericsChecks.java, NewClassTree support for explicit type argument substitution requires more extensive changes beyond just modifying the conditional in compareGenericTypeParameterNullabilityForCall. The maintainers prefer to handle NewClassTree support in a separate follow-up rather than expanding the scope of PRs focused on specific issues like super constructor calls.

Applied to files:

  • nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java
  • nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java
📚 Learning: 2025-08-14T18:50:06.159Z
Learnt from: msridhar
PR: uber/NullAway#1245
File: guava-recent-unit-tests/src/test/java/com/uber/nullaway/guava/NullAwayGuavaParametricNullnessTests.java:101-102
Timestamp: 2025-08-14T18:50:06.159Z
Learning: In NullAway JSpecify tests, when JDK version requirements exist due to bytecode annotation reading capabilities, prefer failing tests over skipping them on unsupported versions to ensure CI catches regressions and enforces proper JDK version usage for developers.

Applied to files:

  • nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java
🧬 Code graph analysis (1)
nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java (1)
nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java (1)
  • SuppressWarnings (99-263)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Build and test on macos-latest
  • GitHub Check: Build and test on windows-latest
  • GitHub Check: Build caffeine with snapshot
  • GitHub Check: Build spring-framework with snapshot
🔇 Additional comments (2)
nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java (1)

823-823: Expectation update matches new diagnostic target

Switching the expected mention from the nested call to the literal null aligns with the new inference-result handling and where the error is now reported. LGTM.

nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java (1)

64-87: Result abstraction for inference outcomes looks solid

Clean separation of success vs. failure enables precise caching and future instrumentation. No issues.

Copy link
Copy Markdown
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: 0

🧹 Nitpick comments (2)
nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java (2)

90-94: Javadoc mentions constructors, but cache keys are MethodInvocationTree

Doc implies supporting constructor calls; cache type only admits method invocations. Given we’re intentionally punting on NewClass/diamond support (see retrieved learnings), tighten the Javadoc now to avoid confusion, and broaden later when constructor inference lands.

Apply:

- * Maps a Tree representing a call to a generic method or constructor to the result of inferring
- * its type argument nullability. The call must not have any explicit type arguments. If a tree is
+ * Maps a tree representing a call to a generic method to the result of inferring
+ * its type-argument nullability. The call must not have any explicit type arguments. If a tree is

563-566: LGTM: cache failures to prevent repeated inference attempts

Nice win for perf; message retention is fine. If desired, we could later dedupe with a singleton failure result.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8593784 and 399e758.

📒 Files selected for processing (1)
  • nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java (5 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-08-28T04:54:20.953Z
Learnt from: msridhar
PR: uber/NullAway#1248
File: nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java:847-857
Timestamp: 2025-08-28T04:54:20.953Z
Learning: In NullAway's GenericsChecks.java, NewClassTree support for explicit type argument substitution requires more extensive changes beyond just modifying the conditional in compareGenericTypeParameterNullabilityForCall. The maintainers prefer to handle NewClassTree support in a separate follow-up rather than expanding the scope of PRs focused on specific issues like super constructor calls.

Applied to files:

  • nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java
🧬 Code graph analysis (1)
nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java (1)
nullaway/src/main/java/com/uber/nullaway/generics/TypeSubstitutionUtils.java (1)
  • SuppressWarnings (99-263)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: Build and test on ubuntu-latest
  • GitHub Check: Build and test on macos-latest
  • GitHub Check: Build and test on windows-latest
  • GitHub Check: Build caffeine with snapshot
🔇 Additional comments (4)
nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java (4)

64-88: LGTM: clear success/failure result typing for the cache

Good encapsulation; keeps call sites simple and avoids null-sentinels.


525-529: LGTM: reuse success results; skip work when failure or not-attempted

Lookup logic clean; using null map to mean “no substitution” keeps downstream code simple.


547-549: LGTM: populate success for all involved invocations

Storing the same inferred map for nested calls is correct; substitution filters by occurrences.


1163-1169: LGTM: only apply inferred nullability on successful inference

Fallback to explicit substitution is preserved; constructor/diamond left untouched, as intended.

@codecov
Copy link
Copy Markdown

codecov bot commented Sep 7, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.45%. Comparing base (cb72c5a) to head (8c008e1).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1280      +/-   ##
============================================
+ Coverage     88.42%   88.45%   +0.02%     
- Complexity     2451     2454       +3     
============================================
  Files            92       92              
  Lines          8088     8100      +12     
  Branches       1608     1610       +2     
============================================
+ Hits           7152     7165      +13     
  Misses          472      472              
+ Partials        464      463       -1     

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

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