Skip to content

Add PolyNull library models and apply them to Optional.orElseGet and Map.computeIfAbsent - #1813

Merged
msridhar merged 80 commits into
masterfrom
polynull-explicit
Oct 21, 2026
Merged

msridhar merged 80 commits into
masterfrom
polynull-explicit

Conversation

@msridhar

@msridhar msridhar commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1616.

Add JSpecify-mode library model support for polymorphic nullness in method signatures. Library model providers can identify top-level or nested parameter and return locations whose nullness must resolve together at each invocation.

Represent the linked nullness with a fresh, nullable-bounded synthetic inference variable for each call. Generate constraints from named arguments, lambdas, method references, and available result target types, and solve these constraints alongside ordinary generic method type-variable inference. Apply the resolved @Nullable or @NonNull qualifier back to every modeled location in the call-site method type. Report a dedicated inference diagnostic when the linked locations impose incompatible constraints.

Initially model Optional.orElseGet and Map.computeIfAbsent, including calls through overriding methods. Extend nested type-path updates to replace types during inference and to preserve wildcard and captured-type structure when applying the resolved qualifier.

Add tests covering named functional-interface arguments, lambdas, method references, var results, assignment targets, inherited models, explicit and inferred generic type arguments, and custom library model providers.

Assisted-by: Codex (GPT-5)

Summary by CodeRabbit

  • New Features
    • Added support for polymorphic nullness models when JSpecify experimental features are enabled, inferring nullness across modeled method inputs and outputs, including nested generic types.
    • Added default models for Optional.orElseGet and Map.computeIfAbsent, with return nullness inferred from relevant inputs.
    • Added support for inherited polymorphic nullness models and their interaction with existing fixed and conditional nullness models.
    • Improved nullness handling for Optional.orElse based on the fallback value.
    • Custom library models can define polymorphic nullness for method parameters and return values.

@msridhar

msridhar commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

This change is part of the following stack:

Change managed by git-spice.

@codecov

codecov Bot commented Sep 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.96875% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.73%. Comparing base (2431401) to head (cf2b7e3).

Files with missing lines Patch % Lines
...ava/com/uber/nullaway/generics/GenericsChecks.java 96.66% 2 Missing and 4 partials ⚠️
.../com/uber/nullaway/generics/PolyNullInference.java 96.15% 1 Missing and 3 partials ⚠️
.../nullaway/testlibrarymodels/TestLibraryModels.java 98.91% 1 Missing and 1 partial ⚠️
...r/nullaway/librarymodel/NestedTypePathUpdater.java 97.43% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1813      +/-   ##
============================================
+ Coverage     88.26%   88.73%   +0.47%     
  Complexity       38       38              
============================================
  Files           117      118       +1     
  Lines         11968    12504     +536     
  Branches       2458     2532      +74     
============================================
+ Hits          10563    11095     +532     
- Misses          639      642       +3     
- Partials        766      767       +1     

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

@vlsi

vlsi commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

The inference engine in this PR is more general than its API. A PolyNull group is a synthetic type variable bounded by @Nullable Object that affects only nullness: the base type stays fixed, and the solver picks @Nullable or @NonNull together with the ordinary method type variables. Read that way, the two models in this PR are signatures with one extra nullness-only type parameter:

// Optional<T>
<S extends @Nullable T> S orElseGet(Supplier<? extends S> supplier)
// Map<K, V>
<S extends @Nullable V> S computeIfAbsent(K key, Function<? super K, ? extends S> mappingFunction)

Plain Java cannot declare these, because a real S extends T would also narrow the base type (Optional<Number>.orElseGet(() -> 1) would infer S = Integer). So the concept needs its own representation, and PolyNullLocation is that representation. I am not asking to change the implementation, but I have one request for the API before it ships.

Could PolyNullLocation carry a group id? Right now every location of a method belongs to one implicit group: createInferenceVariable takes a group argument, but every call passes 0. LibraryModels.polyNullLocations() becomes public API with the next release, and adding a group to the record later would break every provider that constructs it. With a group id, each group gets its own synthetic variable, and the rest of the engine stays as it is. A related question is how CompositeHandler.onGetPolyNullLocations should merge locations once more than one handler provides them. Today it takes the union, which would merge groups from different sources.

If you agree with this reading, the javadoc could describe a location as a position of a nullness-only method type variable, rather than as PolyNull. That would leave room for the following, none of which needs to be in this PR:

  • An annotation for first-party code, for example @PolyNull("A") in the annotations module next to @Contract and @EnsuresNonNull, with a handler that turns annotated positions into locations. The Checker Framework @PolyNull has no argument, so it could be recognized as a single group. This part needs body and override checks that the library-model-only approach avoids.
  • Stubs instead of Java code for library models. NullnessAnnotationSerializer already reads annotated Java sources for jspecify-jdk.astubx, so if the serialized format carried the group, a model such as orElseGet or List.toArray (mentioned in Add some kind of @PolyNull-like support within library models? #1616) would be one annotated line in a stub.

In both cases, the new source would feed the existing onGetPolyNullLocations hook, and the inference code in this PR would not change. Does that match how you see the feature evolving?

@msridhar

Copy link
Copy Markdown
Contributor Author

Could PolyNullLocation carry a group id? Right now every location of a method belongs to one implicit group: createInferenceVariable takes a group argument, but every call passes 0. LibraryModels.polyNullLocations() becomes public API with the next release, and adding a group to the record later would break every provider that constructs it. With a group id, each group gets its own synthetic variable, and the rest of the engine stays as it is.

To be clear, the ask is both for a group id and that each group is handled with a separate constraint variable during solving, is that correct?

I'm a bit unclear on the motivation. Do you have a concrete example?

A related question is how CompositeHandler.onGetPolyNullLocations should merge locations once more than one handler provides them. Today it takes the union, which would merge groups from different sources.

Yeah, that is weird. Probably the safe thing to do here is assert fail if more then one handler tries to add polynull locations for the same method. Alternately, maybe this doesn't belong in handlers, and instead we should "hard code" the fact that only library models provide polynull locations; but that might be messier.

  • An annotation for first-party code, for example @PolyNull("A") in the annotations module next to @Contract and @EnsuresNonNull, with a handler that turns annotated positions into locations. The Checker Framework @PolyNull has no argument, so it could be recognized as a single group. This part needs body and override checks that the library-model-only approach avoids.

In terms of priorities, the reason I am working on PolyNull for library models is that without it, we get a very big number of false positives with JDK library models enabled for calls to methods like orElseGet. Potentially down the road, we could add support for first-party @PolyNull annotations. But in that case, it'd be really good if they could be checked, and I haven't thought through the complexity of body and override checks. To me first-party @PolyNull annotations should probably be lower priority compared to more complete JSpecify checking support, though if someone is very passionate I'm open to them working on it.

I'm also open to unchecked (trusted) @PolyNull first-party annotations as an intermediate step, though it's not ideal.

  • Stubs instead of Java code for library models. NullnessAnnotationSerializer already reads annotated Java sources for jspecify-jdk.astubx, so if the serialized format carried the group, a model such as orElseGet or List.toArray (mentioned in Add some kind of @PolyNull-like support within library models? #1616) would be one annotated line in a stub.

👍

@vlsi

vlsi commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Thanks, that clears up the priorities.

On group ids: yes, I meant a separate solver variable per group, but I withdraw the request. I looked for a method that needs two independent groups and found none. The Checker Framework manual defines every occurrence of a polymorphic qualifier in a method as the same qualifier, so CF-annotated code cannot have two groups: all 171 @PolyNull lines in Apache Calcite and all 186 in java.util and java.lang of the typetools JDK use one group. A group could also be added later without breaking providers, through a secondary constructor that defaults it to 0.

That search turned up a different gap: the receiver. List.toArray(), which #1616 mentions, is annotated in the typetools JDK as

@PolyNull @PolySigned Object[] toArray(List<@PolyNull @PolySigned E> this);

and the same pattern appears in more than 30 toArray() declarations across java.util, java.util.concurrent, and Stream. PolyNullLocation accepts only a parameter index or -1 for the return type, so it cannot put the variable on the receiver's type argument, and substitution cannot cover it because the return type is Object[] rather than E[]. Adding the receiver later would need another magic index such as -2. Would you replace int parameterIndex with an explicit position kind (receiver, parameter, return) before this API ships?

On merging: the same union also happens one level down. CombinedLibraryModels merges polyNullLocations() from every LibraryModels provider, so a user provider that adds a location for Optional.orElseGet would silently join the built-in group. An assertion in CompositeHandler would not catch that, since all of these arrive through LibraryModelsHandler. Would it work to reject a method that gets locations from more than one provider while CombinedLibraryModels is built, with an error that names the method?

@msridhar

msridhar commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor Author

On group ids: yes, I meant a separate solver variable per group, but I withdraw the request.

👍

That search turned up a different gap: the receiver. List.toArray(), which #1616 mentions, is annotated in the typetools JDK as

@PolyNull @PolySigned Object[] toArray(List<@PolyNull @PolySigned E> this);

and the same pattern appears in more than 30 toArray() declarations across java.util, java.util.concurrent, and Stream. PolyNullLocation accepts only a parameter index or -1 for the return type, so it cannot put the variable on the receiver's type argument, and substitution cannot cover it because the return type is Object[] rather than E[]. Adding the receiver later would need another magic index such as -2. Would you replace int parameterIndex with an explicit position kind (receiver, parameter, return) before this API ships?

Absolutely, we should fix this. Thanks for finding this issue!! I'll at least try to fix the API in this PR, though perhaps will add full support in a follow-up.

On merging: the same union also happens one level down. CombinedLibraryModels merges polyNullLocations() from every LibraryModels provider, so a user provider that adds a location for Optional.orElseGet would silently join the built-in group. An assertion in CompositeHandler would not catch that, since all of these arrive through LibraryModelsHandler. Would it work to reject a method that gets locations from more than one provider while CombinedLibraryModels is built, with an error that names the method?

This is a good point. I think we should probably assert in both places for now. One could hypothetically imagine user-provided LibraryModels wanting to "override" built-in polynull models, but I can't imagine a realistic scenario for that; I think in almost all cases it would be just a mistake that we should fail fast on.

@msridhar

Copy link
Copy Markdown
Contributor Author

Note to self: we should probably also model Optional.orElse as part of this PR, low hanging fruit.

Fall back to nullable PolyNull when separate inference fails with
WarnOnGenericInferenceFailure disabled. Allow generic modeled calls to
perform separate PolyNull inference after generic inference fails in that
configuration, preserving ordinary field-assignment and return errors.

Add regression coverage for inferred and explicit generic calls and verify
that enabling inference warnings does not add ordinary nullness errors.

Validation: :nullaway:test and :nullaway:buildWithNullAway pass. All changed
lines and branches are covered, and :nullaway:pitestDiff kills all 6 mutants.

Assisted-by: Codex (gpt-6)
Cover non-null results for wildcard and captured receivers, parenthesized
result targets, and computeIfAbsent diagnostics with inference warnings
disabled. Use throwing callbacks for wildcard controls because String
literals are not assignable to an unknown captured element type.

Check failed generic inference with diagnostics and dereferences on separate
lines, and name the supplier and function tests after their named values.

Assisted-by: Codex (gpt-6)
Route both inference paths through reportInferenceFailure so the warning
flag, duplicate suppression, and diagnostic type stay consistent.

Assisted-by: Codex (gpt-6)
Keep receiver and nested-location checks at both handler and provider
boundaries while sharing their predicates and diagnostic messages.

Remove the unused inference-variable group parameter and the impossible
negative parameter-index check. Memoize the synthetic variable name.

Assisted-by: Codex (gpt-6)
Keep the parameter-model filter: conflicting nullable and non-null fixed
models must yield to PolyNull before the ordinary conflict check runs.
Add a regression that fails with a model-conflict exception without the
filter, and document its purpose.

Cover an input-only model's ordinary nullable-return assignment diagnostic
and ignore a stale nested parameter location on a zero-argument method.

Assisted-by: Codex (gpt-6)
@msridhar
msridhar requested a review from vlsi October 21, 2026 18:01

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

Optional

  1. Check polyNullInferenceFailureDoesNotAddOrdinaryNullnessErrors with expected-diagnostic markers instead of the text of the AssertionError (inline).

Nit

  1. Remove the out-of-range parameter guard in PolyNullInference.createContext, since the stale-location test passes without it (inline at PolyNullInference.java:139-143).
  2. Two nits from 2026-10-18 have no reply yet: #1813 (comment) (the isPolyNullLibraryModelsEnabled Javadoc) and #1813 (comment) (the TODO in CoreTests). Please apply them or say why not.
Coverage and mutation results at fec142b

./gradlew :nullaway:test -PdiffBase=2f728073e passes. 455 of 458 changed lines are executed, and 218 of 226 changed branches are covered. The lines that remain uncovered are the parentPath == null returns at GenericsChecks.java:3757 and :3763, and the : methodType branch at PolyNullInference.java:81.

./gradlew :nullaway:pitestDiff -PdiffBase=2f728073e reports live mutants on 15 lines. Apart from the guard in item 2, I have no request for them.

A correction to my last round: I wrote that the survivor at PolyNullInference.java:48 (now :53, hasReturnLocations replaced with true) is not equivalent because no test uses a model without a Return() location. inputOnlyPolyNullPreservesNullableReturnAssignmentError now uses such a model, and the mutant still survives, as does the one at :206. Both look equivalent to me, so no test is needed.

Put modeled calls on separate lines from assignments and returns so extra ordinary nullness diagnostics fail the test directly. Remove assertions on CompilationTestHelper failure text and their unused imports.

Assisted-by: Codex (gpt-6)
Collect locations without checking the argument count: applying them already walks only the actual parameters. Keep the stale-location regression coverage.

Assisted-by: Codex (gpt-6)
Document that both JSpecifyMode and JSpecifyExperimental must be enabled.

Assisted-by: Codex (gpt-6)
Keep the missing diagnostic marked as a TODO and point to the existing JSpecify-mode regression test.

Assisted-by: Codex (gpt-6)
Restore the Config Javadoc and CoreTests comment so those review threads can receive a different response.

Assisted-by: Codex (gpt-6)

@msridhar msridhar left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed the diagnostic-test cleanup and redundant parameter-index guard from the latest review. Each reply references its own commit. Also replied on the earlier nits. The full :nullaway:test suite and :nullaway:buildWithNullAway passed for the fixes. :nullaway:pitestDiff against fec142b generated no mutants on those changed lines. A manual mutation forcing a nullable fallback caused the rewritten diagnostic test to fail on the extra assignment and return diagnostics, as expected.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add some kind of @PolyNull-like support within library models?

2 participants