You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Not-null varargs argument with Nullable elements - considered as Nullable #674
Yes, NullAway doesn't properly parse annotations on arrays (or varargs); it interprets the @Nullable annotation in your example as pertaining to the reference to the array itself, not to the references within the array. Unfortunately fixing this will introduce compatibility issues, so we need to be careful. We plan to do this as part of our efforts to support JSpecify. Thanks for the report!
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:
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 @Nullable annotation in this example includes PARAMETER (as well as TYPE_USE) in its @Target, it does get applied to the entire parameter, too. We can see in the .class file that the parameter has @Nullable on it. That's in addition to the @NonNull annotation on the parameter type and to the @Nullable annotation on the parameter's element type. That means that the array is annotated as both nullable and non-null. I don't know how NullAway normally handles such conflicts. But you could avoid that entirely by removing PARAMETER from the @Target.
(If you still have tools that require PARAMETER in the @Target, you might be able to use a separate @Nullable annotation for them. Unfortunately, whenever you have annotations that include both TYPE_USE and other target locations, you may encounter cases in which they're applied to multiple locations.)
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:
Annotation Type
Annotation Position
Mode
@Nullable varargs array
@Nullable array elements
@Nullable args at calls
declaration
-
standard
❌
❌
✅
declaration
-
legacy
✅
❌
✅
declaration
-
JSpecify
❌
❌
✅
type use
before ...
standard
✅
❌
❌
type use
before ...
legacy
✅
❌
✅
type use
before ...
JSpecify
✅
❌
❌
type use
elements
standard
❌
❌
✅
type use
elements
legacy
✅
❌
✅
type use
elements
JSpecify
❌
✅
✅
type use
both
standard
✅
❌
✅
type use
both
legacy
✅
❌
✅
type use
both
JSpecify
✅
✅
✅
both
before ...
standard
✅
❌
❌
both
before ...
legacy
✅
❌
✅
both
before ...
JSpecify
✅
❌
❌
both
elements
standard
❌
❌
✅
both
elements
legacy
✅
❌
✅
both
elements
JSpecify
❌
✅
✅
both
both
standard
✅
❌
✅
both
both
legacy
✅
❌
✅
both
both
JSpecify
✅
✅
✅
A subtlety here is that a declaration annotation should allow @Nullable arguments to be passed at call sites outside of JSpecify mode, even though we cannot check within the method that they are used correctly. This is because in almost all cases, one wants to write @Nullable on the varargs formal parameter to indicate that individual varargs actual parameters may be @Nullable.
Activity
msridhar commentedon Oct 27, 2022
Yes, NullAway doesn't properly parse annotations on arrays (or varargs); it interprets the
@Nullableannotation in your example as pertaining to the reference to the array itself, not to the references within the array. Unfortunately fixing this will introduce compatibility issues, so we need to be careful. We plan to do this as part of our efforts to support JSpecify. Thanks for the report!cpovirk commentedon Dec 22, 2022
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:
msridhar commentedon Dec 22, 2022
Wow @cpovirk that was not expected. I am going to pull that out as a separate issue.
msridhar commentedon Dec 23, 2022
@cpovirk the issue you observed was fixed in #702
Ignore incompatibly annotated var args from Kotlin code. (#721)
cpovirk commentedon Mar 21, 2024
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
@Nullableannotation in this example includesPARAMETER(as well asTYPE_USE) in its@Target, it does get applied to the entire parameter, too. We can see in the .class file that the parameter has@Nullableon it. That's in addition to the@NonNullannotation on the parameter type and to the@Nullableannotation on the parameter's element type. That means that the array is annotated as both nullable and non-null. I don't know how NullAway normally handles such conflicts. But you could avoid that entirely by removingPARAMETERfrom the@Target.(If you still have tools that require
PARAMETERin the@Target, you might be able to use a separate@Nullableannotation for them. Unfortunately, whenever you have annotations that include bothTYPE_USEand other target locations, you may encounter cases in which they're applied to multiple locations.)msridhar commentedon Mar 26, 2024
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.
msridhar commentedon Aug 18, 2024
Similar to #1010 (comment) here is a table of expected behaviors with varargs annotations:
@Nullablevarargs array@Nullablearray elements@Nullableargs at calls..................A subtlety here is that a declaration annotation should allow
@Nullablearguments to be passed at call sites outside of JSpecify mode, even though we cannot check within the method that they are used correctly. This is because in almost all cases, one wants to write@Nullableon the varargs formal parameter to indicate that individual varargs actual parameters may be@Nullable.msridhar commentedon Oct 18, 2024
This is finally fixed in NullAway 0.12.0!