-
Notifications
You must be signed in to change notification settings - Fork 299
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
Not-null varargs argument with Nullable elements - considered as Nullable #674
Comments
Yes, NullAway doesn't properly parse annotations on arrays (or varargs); it interprets the |
This is probably not a surprise, but I notice the same thing with annotations on type arguments: They're interpreted as if they're on the "top-level" type. For example: package p;
import java.util.List;
import org.checkerframework.checker.nullness.qual.Nullable;
class TypeArgumentAnnotation {
void use(List<@Nullable String> list) {
list.hashCode();
}
}
|
Wow @cpovirk that was not expected. I am going to pull that out as a separate issue. |
See #720 for a detailed explanation. Long story short: 1. `kotlinc` will add `@org.jetbrains.annotations.NotNull` as a declaration annotation for any argument that is non-null, which we pick up from jars when running with `AcknowledgeRestrictiveAnnotations=true`. 2. Unfortunately, it will mark code such as `open fun w(vararg args: Any?)` as having `@NotNull args` (meaning the array itself). 3. This is indistinguishable at the bytecode level from Java code such as `w(@NotNull Object... args)`, which we believe should be interpreted as marking the elements of `args` as being `@NotNull`. 4. This PR hacks `RestrictiveAnnotationHandler` to skip `@org.jetbrains.annotations.NotNull` on var args only. Additionally, our basic handling of var args is pretty broken. I went over our test cases and commented where I think our current behavior is different from the desired behavior as documented in #720 and #674. Foregoing fixing it for now, as I am worried about the breaking change and I think we want to avoid changing that before 0.10.9.
The main fix here does still need to be in NullAway, but I did notice one other thing when I was looking back at this issue: Because the (If you still have tools that require |
Thanks a lot @cpovirk! We will keep an eye out for that issue. I would have been very confused without your comment. I hope to get back to this issue at some point soon. |
Similar to #1010 (comment) here is a table of expected behaviors with varargs annotations:
A subtlety here is that a declaration annotation should allow |
This is finally fixed in NullAway 0.12.0! |
result:
which points to line:
Key facts:
names
is vararg array, it's expected to be not-null and contain:Nice examples:
https://help.eclipse.org/latest/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-using_null_type_annotations.htm&cp=1_3_9_1_3&anchor=compatibility
There is also one answer I found helpful:
https://stackoverflow.com/questions/32327134/where-does-a-nullable-annotation-refer-to-in-case-of-a-varargs-parameter
The text was updated successfully, but these errors were encountered: