Repository navigation
Description
A library model provider that gives one parameter both a nullable and a non-null fixed model, plus a top-level PolyNull model for that parameter, makes NullAway crash at every call of the method unless -XepOpt:NullAway:JSpecifyExperimental=true is set. With the flag on, the same provider is accepted: #1813 drops fixed parameter models at PolyNull locations before the conflict check runs, and polyNullOverridesConflictingFixedParameterModels tests exactly this. So whether NullAway accepts the provider depends on a flag the provider cannot see.
Versions: NullAway master at 9555b20 (0.14.3-SNAPSHOT), Error Prone 2.50.0, JDK 21.0.9. Flags: -XepOpt:NullAway:OnlyNullMarked=true -XepOpt:NullAway:JSpecifyMode=true -XDaddTypeAnnotationsToSymbol=true, without JSpecifyExperimental.
Code sample. TestLibraryModels in test-library-models is such a provider: parameter 0 of com.uber.lib.unannotated.PolyNullOverrides.NullableOverride.top is in both explicitlyNullableParameters() and nonNullParameters(), and it is a top-level PolyNull location. I ran this as a test in PolyNullLibraryModelsTests, with makeTestHelperWithArgs(List.of("-XepOpt:NullAway:OnlyNullMarked=true", JSpecifyJavacConfig.JSPECIFY_MODE_FLAG, JSpecifyJavacConfig.ADD_TYPE_ANNOTATIONS_FLAG)):
import com.uber.lib.unannotated.PolyNullOverrides.NullableOverride;
import org.jspecify.annotations.NullMarked;
@NullMarked
class Test {
Object test(NullableOverride library, Object value) {
return library.top(value, value, value, () -> value);
}
}Actual:
java.lang.AssertionError: /Test.java:7: error: An unhandled exception was thrown by the Error Prone static analysis plugin.
return library.top(value, value, value, () -> value);
^
Please report this at https://github.com/google/error-prone/issues/new and include the following, as well as a reproducing code sample (if possible):
error-prone version: 2.50.0
BugPattern: NullAway
Stack Trace:
java.lang.IllegalStateException: Library models give conflicting nullability for the following parameter of method top: 0
at com.uber.nullaway.handlers.LibraryModelsHandler.onOverrideMethodInvocationParametersNullability(LibraryModelsHandler.java:177)
at com.uber.nullaway.handlers.CompositeHandler.onOverrideMethodInvocationParametersNullability(CompositeHandler.java:157)
at com.uber.nullaway.NullAway.handleInvocation(NullAway.java:2439)
at com.uber.nullaway.NullAway.matchMethodInvocation(NullAway.java:450)
polyNullOverridesConflictingFixedParameterModels calls the same method with JSpecifyJavacConfig.withJSpecifyModeArgs(...), which adds JSpecifyExperimental=true, and passes on the same commit.
Expected: NullAway treats this provider the same way with and without JSpecifyExperimental: either both configurations compile the call above without an IllegalStateException, or neither accepts the provider. Which of the two is the question below.
Context. The conflicting pair was added in fec142b during the review of #1813 (#1813 (comment)), as the regression test for the filter in OptimizedLibraryModels.parameterModelsWithoutPolyNull. That filter changes the outcome only when fixed models conflict: for any other provider, the PolyNull loop in onOverrideMethodInvocationParametersNullability and GenericsChecks.applyPolyNullModel overwrite the parameter's nullness later anyway. With the flag off, CombinedLibraryModels does not read PolyNull locations at all, so the filter removes nothing and the conflict check, which predates #1813, throws. The built-in Optional.orElseGet and Map.computeIfAbsent models carry PolyNull locations and no fixed parameter models, so no built-in model takes this path. The conflict is checked at each call of the method, not when the provider loads, so it fails only where a call to NullableOverride.top meets a configuration without the flag. No test has that combination: every test that calls PolyNullOverrides enables JSpecifyExperimental, and the two tests without the flag call PolyNullMethods.first, Optional.orElseGet, and Map.computeIfAbsent. I found the crash with the crash oracle from #2009, which compiles NullAway's own test sources without JSpecifyExperimental.
Question. Which behavior should hold?
- Conflicting fixed parameter models are a provider error in every configuration. Remove the filter in
parameterModelsWithoutPolyNull, the conflicting pair inTestLibraryModels, andpolyNullOverridesConflictingFixedParameterModels. A provider like this one then fails with the flag on as well, and the test fixtures no longer contain one. - PolyNull locations take precedence over fixed parameter models in every configuration.
CombinedLibraryModelskeeps reading PolyNull locations when PolyNull is disabled, and uses them only to drop fixed models at those locations. With the flag off, the parameter is then left with no fixed model, so the experimental option changes what the fixed models say even while it is off. - Keep the current behavior, and move the conflicting provider out of
test-library-modelsinto a provider that only the experimental-mode test loads. Providers are found throughServiceLoaderon the classpath, so this needs a separate module on that test's classpath.
I would pick option 1: the filter exists for one provider shape, and that shape is already an error whenever the flag is off.
Activity
Reject conflicting fixed parameter models regardless of PolyNull (apa…