Skip to content

JSpecify JDK models: list.toArray() on a List<String> is reported as @Nullable Object [] where Object[] is expected #1988

Open

Description

@vlsi

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"))).

import java.util.List;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
@NullMarked
class Test {
  Object[] nonNullElements(List<String> list) {
    Object[] array = list.toArray();
    return array;
  }
  @Nullable Object[] nullableElements(List<@Nullable String> list) {
    @Nullable Object[] array = list.toArray();
    return array;
  }
}

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

  1. added
    jspecify
    on Oct 22, 2026
  2. msridhar commented on Nov 21, 2026

    @msridhar
    Contributor
  3. added this to the v0.14.5 milestone on Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementjdk-modelsjspecify

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions