Skip to content

Jspecify mode of 2d object arrays seems broken #1150

@agrieve

Description

@agrieve

Syntax of 2d arrays is tricky, so I might be holding it wrong...

import org.jspecify.annotations.Nullable;
import org.jspecify.annotations.NullMarked;
import static org.chromium.build.NullUtil.assumeNonNull;

@NullMarked
class Foo {
  // nullable array of non-null arrays of non-null objects.
  Object @Nullable [][] arr1 = new Object[3][4];
  // nullable array of non-nullable arrays of nullable objects.
  @Nullable Object @Nullable [][] arr2 = new Object[3][4];
  @Nullable Object[][] arr3 = new Object[3][4];

  public void bug() {
    assumeNonNull(arr1);

    for (int i = 0; i < arr1.length; ++i) {
      for (int j = 0; j < arr1[i].length; j++) {
        System.out.println(arr1[i][j]);
      }
    }

    assumeNonNull(arr2);

    for (int i = 0; i < arr2.length; ++i) {
      for (int j = 0; j < arr2[i].length; j++) {
        System.out.println(arr2[i][j]);
      }
    }

    for (int i = 0; i < arr3.length; ++i) {
      for (int j = 0; j < arr3[i].length; j++) {
        System.out.println(arr3[i][j]);
      }
    }
  }
}
Foo.java:11: warning: [NullAway] Cannot assign from type Object[][] to type @Nullable Object[][] due to mismatched nullability of type parameters
  @Nullable Object @Nullable [][] arr2 = new Object[3][4];
                                  ^
    (see http://t.uber.com/nullaway )
Foo.java:12: warning: [NullAway] Cannot assign from type Object[][] to type @Nullable Object[][] due to mismatched nullability of type parameters
  @Nullable Object[][] arr3 = new Object[3][4];
                       ^
    (see http://t.uber.com/nullaway )
Foo.java:26: warning: [NullAway] dereferenced expression arr2[i] is @Nullable
      for (int j = 0; j < arr2[i].length; j++) {
                                 ^
    (see http://t.uber.com/nullaway )
Foo.java:32: warning: [NullAway] dereferenced expression arr3[i] is @Nullable
      for (int j = 0; j < arr3[i].length; j++) {
                                 ^
    (see http://t.uber.com/nullaway )
4 warnings

The only difference between arr1 and arr2 is whether the element type is nullable, but changing that triggers warnings about the nullness of the array (no warnings exist for arr1). arr3 has only a nullable element type, but triggers the same warnings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    false positivejspecifyRelated to support for jspecify standard (see jspecify.dev)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions