Repository navigation
Add PolyNull library models and apply them to Optional.orElseGet and Map.computeIfAbsent - #1813
Conversation
|
This change is part of the following stack: Change managed by git-spice. |
Codecov Report❌ Patch coverage is 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. 🚀 New features to boost your workflow:
|
c7b6872 to
107b239
Compare
a63ed66 to
1f52bb7
Compare
2811af1 to
ae70300
Compare
5d13c7e to
100dbd1
Compare
|
The inference engine in this PR is more general than its API. A PolyNull group is a synthetic type variable bounded by // 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 Could 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:
In both cases, the new source would feed the existing |
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?
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.
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 I'm also open to unchecked (trusted)
👍 |
|
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 That search turned up a different gap: the receiver. @PolyNull @PolySigned Object[] toArray(List<@PolyNull @PolySigned E> this);and the same pattern appears in more than 30 On merging: the same union also happens one level down. |
👍
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.
This is a good point. I think we should probably assert in both places for now. One could hypothetically imagine user-provided |
|
Note to self: we should probably also model |
aefee7d to
b7a2ecd
Compare
99c1222 to
8779ff3
Compare
bfb3f98 to
231d696
Compare
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)
vlsi
left a comment
There was a problem hiding this comment.
Optional
- Check
polyNullInferenceFailureDoesNotAddOrdinaryNullnessErrorswith expected-diagnostic markers instead of the text of theAssertionError(inline).
Nit
- Remove the out-of-range parameter guard in
PolyNullInference.createContext, since the stale-location test passes without it (inline atPolyNullInference.java:139-143). - Two nits from 2026-10-18 have no reply yet: #1813 (comment) (the
isPolyNullLibraryModelsEnabledJavadoc) and #1813 (comment) (the TODO inCoreTests). 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
left a comment
There was a problem hiding this comment.
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.
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
@Nullableor@NonNullqualifier 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.orElseGetandMap.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,
varresults, assignment targets, inherited models, explicit and inferred generic type arguments, and custom library model providers.Assisted-by: Codex (GPT-5)
Summary by CodeRabbit
Optional.orElseGetandMap.computeIfAbsent, with return nullness inferred from relevant inputs.Optional.orElsebased on the fallback value.