Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jspecify mode of 2d object arrays seems broken #1150

Open
agrieve opened this issue Feb 24, 2025 · 0 comments
Open

Jspecify mode of 2d object arrays seems broken #1150

agrieve opened this issue Feb 24, 2025 · 0 comments

Comments

@agrieve
Copy link

agrieve commented Feb 24, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant