Skip to content

Substitute inferred @NonNull types for generic method inference#1445

Merged
msridhar merged 7 commits into
masterfrom
issue-1444
Jan 25, 2026
Merged

Substitute inferred @NonNull types for generic method inference#1445
msridhar merged 7 commits into
masterfrom
issue-1444

Conversation

@msridhar

@msridhar msridhar commented Jan 24, 2026

Copy link
Copy Markdown
Collaborator

Fixes #1444

In certain cases, like the test from #1444 added here, javac adds a @Nullable annotation to a type incorrectly as part of its inference. Here, for a call from the test:

@NullMarked
public class Test {
    static class DefaultConfiguration {
        final @Nullable Stream<? extends String> children;
    }
    final Stream<? extends String> children;
    Test(DefaultConfiguration configuration) {
        // relevant call
        this.children = notNull(configuration.children, "children must not be null");
    }
    public static <T> T notNull(@Nullable T object, String message) {
        throw new RuntimeException("todo");
    }
}

javacs inferred type for the return of the notNull call matches the inferred parameter type exactly, @Nullable Stream<...>. But, when applying JSpecify rules, the return cannot be @Nullable. To fix this, add inferred @NonNull qualifiers along with @Nullable when doing a substitution inside a type.

Summary by CodeRabbit

  • Bug Fixes

    • Simplified printing of error/unknown generic types to show only the simple type name.
    • Improved type-variable substitution so inferred nullability is applied for both nullable and non-null cases.
  • Tests

    • Added/updated tests covering generic method type inference and JSpecify/Nullability behaviors (includes a new scenario for issue1444).

✏️ Tip: You can customize this high-level summary in your review settings.

@msridhar msridhar changed the title [draft] fix issue 1444 Substitute inferred @NonNull types for generic method inference Jan 24, 2026
@msridhar msridhar added the jspecify Related to support for jspecify standard (see jspecify.dev) label Jan 24, 2026
@msridhar msridhar marked this pull request as ready for review January 24, 2026 21:17
Comment on lines +92 to +97
@Override
public String visitErrorType(Type.ErrorType t, @Nullable Void unused) {
// this arises for our synthetic @Nullable and @NonNull annotations; we just return the simple
// name
return t.tsym.getSimpleName().toString();
}

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.

Drive-by fix to make printed errors more readable, by printing e.g. @NonNull instead of @nullaway.synthetic.NonNull

@codecov

codecov Bot commented Jan 24, 2026

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #1445   +/-   ##
=========================================
  Coverage     88.40%   88.40%           
- Complexity     2711     2712    +1     
=========================================
  Files            99       99           
  Lines          9004     9007    +3     
  Branches       1799     1799           
=========================================
+ Hits           7960     7963    +3     
  Misses          517      517           
  Partials        527      527           

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

@coderabbitai

coderabbitai Bot commented Jan 24, 2026

Copy link
Copy Markdown
Contributor

Walkthrough

Adds an override to GenericTypePrettyPrintingVisitor to print Type.ErrorType as the simple symbol name, and changes TypeSubstitutionUtils.substituteInferredNullabilityForTypeVariables to substitute inferred nullability for all matching type variables (emitting synthetic nullable or non-null annotations based on the inferred value). Tests were updated for expected diagnostic messages and two new tests (issue1444) were added covering contract/nonnull behavior.

Possibly related PRs

  • PR 1348: Modifies substituteInferredNullabilityForTypeVariables in TypeSubstitutionUtils with related inferred-nullability propagation changes.
  • PR 1371: Updates nullability annotation restoration/substitution for generics when computing method return/field types.
  • PR 1248: Refactors generic type-argument substitution and inferred-nullability application used when substituting type args in generic method types.

Suggested reviewers

  • yuxincs
  • lazaroclapp
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.50% 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: adding support for substituting inferred @NonNull types during generic method type inference to fix nullable return type inference issues.
Linked Issues check ✅ Passed The changes directly address issue #1444 by adding @NonNull substitutions in generic inference, handling error types, and adding a test case that reproduces the reported failure scenario.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing generic method type inference for nullable return types as described in the linked issue; no out-of-scope modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

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.

@msridhar msridhar requested a review from yuxincs January 24, 2026 21:20
@msridhar msridhar enabled auto-merge (squash) January 24, 2026 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jspecify Related to support for jspecify standard (see jspecify.dev)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unexpected errors from contract checking with !null return value

2 participants