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

Figure out how JDK 21 record patterns interact with nullability annotations #840

Open
msridhar opened this issue Oct 3, 2023 · 0 comments

Comments

@msridhar
Copy link
Collaborator

msridhar commented Oct 3, 2023

Consider the following code using a record pattern:

class Records {
  record Rec(Object fst, @Nullable Object snd) {}
  void ex(Object obj) {
    if (obj instanceof Rec(Object f, @Nullable Object s)) {
      s.toString(); // possible NPE
    }
  }
}

It seems clear NullAway should report an error on the dereference above. What if the code is written as follows?

  void ex(Object obj) {
    if (obj instanceof Rec(Object f, Object s)) {
      s.toString(); // possible NPE
    }
  }

Here I think the error should be reported in the pattern itself: if the code explicitly writes Object s, the top-level nullability annotation should match the record declaration. Finally, if var is used:

  void ex(Object obj) {
    if (obj instanceof Rec(var f, var s)) {
      s.toString(); // possible NPE
    }
  }

Here I think we should infer the type of s to be @Nullable Object. I think all of the above also holds for uses of record patterns in switch cases.

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