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

Improved varargs support. #291

Merged
merged 3 commits into from Mar 29, 2019
Merged

Conversation

lazaroclapp
Copy link
Collaborator

Resolves #290 by supporting vararg nullability.

Previously, we ignored the default nullability info of vararg arguments,
but would error out if the vararg formal was deemed non-null by a handler.

Instead, we now correctly acknowledge the nullability of vararg arguments
whether they are non-null due to default code assumptions or due to handlers
(restrictive annotations, @Contract, etc).

This PR also adds tests and support for javax.annotations.Nonnull which
we were ignoring before and is needed for those tests.

Copy link
Collaborator

@msridhar msridhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test for the case where the varargs are @Nullable? I think we won't get any safety for this case within the body of the function unfortunately (i.e., it will be able to deref the args without a null check). But we should make sure that there is no warning at the call site.

With that change I approve

@lazaroclapp
Copy link
Collaborator Author

@msridhar There is an issue with the @Nullable case above and beyond not getting any safety for the body of the called function, actually. Namely that NullAway interprets @Nullable Object... others to be a nullable array of non-nullable objects. Thus, this fails:

for (Object other : others) { ... }

With the for loop taken as de-referencing others.

Given that the varargs parameter can be, say, assigned to a field inside the method, I am not sure how to fully support this without support for @Nullable generics.

I see two options:

  1. Undo most of this change and just make NullAway ignore annotations on the var args
  2. Support T1[@Nullable T2] through NullAway
  3. Some limited support for nullable var args where you: a) treat the array ref as non-null, b) but set the elements to @Nullable whenever you load from the array or loop over it, c) forbid assigning the var args array to any other field/local

(1) is easy but unsound, (2) is significant work, (3) is... sort of inconsistent?

@msridhar
Copy link
Collaborator

msridhar commented Mar 29, 2019 via email

@lazaroclapp
Copy link
Collaborator Author

Fine by me, but let me add an error explaining that when we see @Nullable in a var args in the method declaration, then. Otherwise the message is pretty confusing.

Resolves #290 by supporting vararg nullability.

Previously, we ignored the default nullability info of vararg arguments,
but would error out if the vararg formal was deemed non-null by a handler.

Instead, we now correctly acknowledge the nullability of vararg arguments
whether they are non-null due to default code assumptions or due to handlers
(restrictive annotations, `@Contract`, etc).

This PR also adds tests and support for `javax.annotations.Nonnull` which
we were ignoring before and is needed for those tests.
@lazaroclapp lazaroclapp force-pushed the lazaro_handle_nonnull_varargs branch from 8251203 to fee2c25 Compare March 29, 2019 18:01
@lazaroclapp lazaroclapp merged commit 401d048 into master Mar 29, 2019
@lazaroclapp lazaroclapp deleted the lazaro_handle_nonnull_varargs branch March 29, 2019 18:32
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

Successfully merging this pull request may close these issues.

None yet

2 participants