NullAway icon indicating copy to clipboard operation
NullAway copied to clipboard

Jspecify mode of 2d object arrays seems broken

Open agrieve opened this issue 10 months ago • 0 comments

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.

agrieve avatar Feb 24 '25 18:02 agrieve