You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JSpecify JDK models: list.toArray() on a List<String> is reported as @Nullable Object [] where Object[] is expected #1988
With JSpecify JDK models on, toArray() on a List<String> returns @Nullable Object[], so storing the result in an Object[] is reported as incompatible types: @Nullable Object [] cannot be converted to Object [], although no element of a List<String> is null.
Versions: NullAway master at a03b7bb (0.14.3-SNAPSHOT) and the head of #1813 (5f01b57); both report the same. Error Prone 2.50.0, JDK 21.0.9. Flags: -XepOpt:NullAway:JSpecifyMode=true -XDaddTypeAnnotationsToSymbol=true -XepOpt:NullAway:JSpecifyExperimental=true -XepOpt:NullAway:OnlyNullMarked=true; JSpecifyExperimental turns on JSpecifyJDKModels.
Code sample. I ran it as a test in nullaway/src/test/java/com/uber/nullaway/jspecify/, with makeTestHelperWithArgs(JSpecifyJavacConfig.withJSpecifyModeArgs(List.of("-XepOpt:NullAway:OnlyNullMarked=true"))).
Expected: no diagnostic on line 7. Collection.toArray() returns "an array containing all of the elements in this collection", and the elements of a List<String> exclude null, so the array component is @NonNull here. For the List<@Nullable String> on line 11 it stays @Nullable, which is what the JDK model says today.
Actual: the test fails on master and on the #1813 head with:
java.lang.AssertionError: Saw unexpected error on line 7. All errors:
/Test.java:7: warning: [NullAway] incompatible types: @Nullable Object [] cannot be converted to Object []
Object[] array = list.toArray();
^
(see http://t.uber.com/nullaway )
Context.#1728 reported this call-site error together with an override error for toArray(); #1722 fixed the override half, and this half remains. The nullness of the result follows the receiver's element type, which is what the typetools JDK writes as @PolyNull Object[] toArray(List<@PolyNull E> this). #1813 adds PolyNull library models, but a location on the receiver (PolyNullLocation.Receiver) is rejected with PolyNull receiver locations are not yet supported for ..., so this model cannot be written yet. Receiver support was left for a follow-up in #1813 (comment) and #1813 (comment); this issue tracks it. In the typetools JDK the same toArray() pattern appears in more than 30 declarations across java.util, java.util.concurrent, and Stream.
Activity
msridhar commentedon Nov 21, 2026
We should be able to handle this with a
@PolyNullmodel, like:https://github.com/typetools/jdk/blob/f5572a66561560e8d765754dc3ec6781d6342450/src/java.base/share/classes/java/util/Collection.java#L369
Remove NullAway opt-outs from GraphReachability (wala#2233)