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
I presume that we'll follow Java's precedent, which would let "array of non-null strings" be assigned to "array of possibly null strings," even though that's unsound.
To be far, what made Java's precedent of array covariance tolerable (in the beginning) was that at the worst you'd reliably get an ASE at runtime.
Since we don't have that, then it's how generics was mixed into that that probably forms a more useful precedent for us. There, creating an array of a parameterized type causes a warning (but only a warning). That said, I'm not actually sure how that should translate to our world.
I would say that a method returning an array for the caller to write into, or accepting an array parameter for its own implementation to write into, is probably irregular/unusual/already-problematic enough that it is not worth fitting the design to that case. And that says that continuing to treat arrays as covariant for our purposes is a defensible choice for usability reasons.
But does that go as far as wanting our semantics to actually dictate the following to be incorrect?
class Super { @NotNull String[] foo(); }
class Sub extends Super { @Nullable String[] foo(); }
I think we are probably best to just embrace array covariance. Because arrays are exchanged in APIs far more often to be read than to be written to (I would bet > 20:1), and because Java already treats them as covariant, I think that the code snippet above should indeed be deemed incorrect. That seems like the cleanest thing to do.
I don't think the stakes are high on getting this decision perfect, because object arrays are already grossly inferior to collections (in every single way except as special-case optimizations), and type annotations already make that even worse (no one will ever find @A String @B [] as readable as @B List<@A String>).
GenericArrayCast2.java:10: warning: [unchecked] unchecked generic array creation for varargs parameter of type List<String>[]
List<String>[] a = makeArray(asList("foo"));
^
GenericArrayCast2.java:16: warning: [unchecked] Possible heap pollution from parameterized vararg type T
static <T> T[] makeArray(T... objects) {
^
where T is a type-variable:
T extends Object declared in method <T>makeArray(T...)
2 warnings
(And you can hide the one with @SafeVarargs -- not that it would be appropriate to do so on a method like this, which returns the array to the caller.)
But the more direct way of creating one is an error:
GenericArrayCast2.java:5: error: generic array creation
List<String>[] a = new List<String>[1];
^
So you have to use a wildcard and an unchecked cast:
List<String>[] a = (List<String>[]) newList<?>[1];
Or, if you don't mind having both an unchecked warning and a rawtypes warning, you can do it in less code:
List<String>[] a = newList[1];
Once you have a List<String>[], it's easy to set up a latent ClassCastException with no further warnings:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at GenericArrayCast2.main(GenericArrayCast2.java:10)
I, too, am not sure exactly how to apply all this to nullness yet. Certainly an attempt to create an array of non-null objects is at best "aspirational":
(If @NotNull is the default, then it could be omitted. Perhaps it would be surprising for a line with no nullness annotations and no preexisting objects to produce a warning or even error?)
(Another effect of the weird @Nullable String[] syntax is that it looks like you have to annotate the variable -- normally not required by flow-sensitive nullness analysis -- because in fact you're annotating the element type.)
Following the precedent of generics might mean requiring the following and issuing a warning:
(There's no "rawtypes workaround" in the case of String[].)
(Maybe it would be more honest for the code to store the array in a variable of type @Nullable String[], fill it in, and then perform the unchecked cast to @NotNull String[]. I suppose that libraries could even provide a casting helper that performs a runtime check of the contents, though that still can't protect against future mutation through the original variable (edit: or after assigning to a new variable of type Nullable String[]?).)
I was going to say that we could special-case creating an empty array, as in new Foo[0], but:
It's hard to imagine that that's worth a special case. (I guess a few people are stuck calling toArray(T[]), though, and maybe it helps them?)
I suspect that that can be turned into a latent NullPointerException by using Arrays.copyOf(empty, someNonZeroSize). (Not that we're trying to be 100% sound.) [edit: Is this solvable by making copyOf return a @Nullable T[] when given a T[]?]
[edit: another matter of implementation code: the type of Foo[].class: Class<Foo[]> or Class<@Nullable Foo[]>? See jspecify/jdk@71d8267]
Checker Framework also has special handling of Collection.toArray -- both overloads, I think. Certainly it's a weird API. Maybe we can take the easy way out (though I haven't even thought about this enough to say what that is :))
Following-up to my earlier comment at #33 (comment):
What about an unspecified library method that returns a @NullUnspec String[]? I would expect a warning when trying to assign that result to a @NotNull String[], but none when assigning to @nullable String[].
The assignment of @NullUnspec String[] to @Nullable String[] should also produce a warning: the legacy code might assume that no null value is contained in that array and this client could break that.
Certainly receiving an array across an API boundary and then writing to it is a thing that can happen, and therefore a soundness analyzer will want to generate the warning you describe (unless it wants to deem the assignment safe anyway from context). So we should not say anything that sounds like we are preventing it from doing that.
I think we should also not say anything that prevents the non-soundness-based checker from deciding to privilege the much more common use case over the other, because there are big advantages to most users for doing that.
Unfortunately we can't just leave this all up to checkers, because an API owner needs a single answer on whether they can safely make a method return @Nullable String[] if it already has overrides returning @NullUnspec String[] or @NotNull String[].
Wasn't our approach to handling sound tools vs bugfinders that we specify certain things as hard errors and other things as various kinds of warnings. Certain warnings, like these warnings for conversions to/from @NullUnspec could be ignored by default by certain tools. Other tools could elect to make them hard errors.
We know that we require the one kind of "warning" discussed in #33 (and the newly filed #73). I am not sure whether we want to get into "various kinds" of warnings at the level of this project or not. This one discussed here would be a different kind.
A related issue: How much do we plan to encourage "lying" about array nullability when we annotate APIs (directly, like for Guava, or in stub files, like for the JDK)?
(As noted above, Collection.toArray is arguably its own special thing, perhaps especially toArray(T[]). But let's pretend that Collection.toArray doesn't exist and that we're just defining our own method. If that's hard to do, then consider FluentIterable.toArray, which does define its own method, instead.)
The returned array will never contain null. However, as soon as we hand the user a @NotNull Object[] (an array of not-null objects; hopefully I got the syntax right), we've opened up a type-system hole. (Well, probably. It depends on the decisions discussed in this issue.)
The technically correct thing is going to be to return @Nullable Object[]. But are we willing to put users through that? Maybe: If we're putting users of Map.get through that (#71), then is this really any worse? (Maybe it is: At least with Map.get, you can call requireNonNull on the result. Here, you have to suppress or manually loop if you want to pass an array to a method like ImmutableList.copyOf.)
require a @NotNull E[] when there's really "no such thing" and someone has to suppress a warning to obtain a reference of such a type -- though maybe the suppression will be performed often by libraries, as discussed in my previous post?
accept any @Nullable E[] when we know that we would throw NPE if the array contained null? (This is somewhat like the Joiner case, in which appeared willing to use a signature that would permit NPE, but in that case, not all uses of null elements would NPE.)
(First a boilerplate reminder that using object arrays will be approximately equally painful no matter what we do here.)
I suspect that for most array writes in existence it is relatively easy to verify that the array reference can't have undergone a covariant assignment (usually because it was locally initialized to new SameType[]). All those writes are safe. All the trouble comes from the other writes, which I currently suspect to be a small minority.
I think we don't want to let those naughty writes become a tail that wags the dog here. So I think we can embrace array covariance, to the point of saying:
Anything that should be safe as long as those naughty writes aren't happening somewhere is safe enough for us; a checker can always choose to be more concerned about that risk.
Anything that tries to treat an array as contravariant though is right out.
so, re: the specific question just asked, I definitely very much want those APIs to be able to say @NotNull E[]. That's good documentation even if it were nothing else.
And, if all we're concerned about is nullness (as opposed to solving the larger problem of array covariance), then we can also permit any attempt to write a value into an array if we know that that value is not null. This probably covers a good fraction of the remaining cases.
A couple notes about the implications for toArray methods:
A nice thing about covariant arrays is that a type like ImmutableCollection can override the no-arg toArray() to return Object[] instead of the default of @Nullable Object[]. (I think this works out OK for Kotlin callers, but it does not work if you try to write a similar hierarchy in Kotlin, since Kotlin arrays are not covariant.)
Once arrays are covariant, we're stuck: Even if we were to declare these methods to require a nullable array, as in...
public <T> @Nullable T[] toArray(@Nullable T[] array) {
...callers would be able to pass a non-nullable array, since it would widen to a nullable array. The only difference is that we'd then force anyone who tries to use the returned array directly to deal with nullable elements. That's probably the worst of both worlds: The original array might still have arrays unsoundly inserted into it, while the returned array, which is likely not to have nulls in it in the common case (of a collection without nulls and an input array of new Foo[0]), is the array that forces callers to confront nullness.
I haven't thought about the "new" (Java 11) toArray(IntFunction<T[]>) overload.
Re: bullet 2, it's a weird method that is really two in one: (a) the one where you actually expect elements to be written in, and (b) the one where you're just using an empty array as a type token.
In case (a), it's the kind of method I was discussing way up top:
a method ... accepting an array parameter for its own implementation to write into, is probably irregular/unusual/already-problematic enough that it is not worth fitting the design to that case. And that says that continuing to treat arrays as covariant for our purposes is a defensible choice for usability reasons.
... and I still think it's fine to throw this case under the bus (and we really have no other choice).
In case (b), logically the same type token ought to work whether the caller wants to get a Foo![]! or a Foo?[]! back out. And that type token itself could be either a Foo![]! or a Foo?[]!, couldn't it? (aside: it seems like an analyzer should treat a literal new Foo[0] or new Foo[] {} as having the type Foo![]!, as there's just no call for bringing null into the picture, so to speak.) So in theory all four combinations are valid?
(What's more dodgy is trying to get a Foo![]! from a Collection!<Foo?>, but I believe we've already made our peace with this anomaly is that right? And that's an issue regardless of what gets passed in.)
(so to be more clear I'm possibly raising a very minor question over whether <T extends @Nullable Object> T[] toArray(T[]) is the right signature or not... but mostly I'm just confused, this stuff is hard to think about.)
I wrote a bunch of theoretical stuff and then deleted it :)
[edit: The continuation of the toArray discussion has moved to #481.]
As for the aside:
it seems like an analyzer should treat a literal new Foo[0] or new Foo[] {} as having the type Foo![]!, as there's just no call for bringing null into the picture, so to speak.
I think we've to some extent made peace with the idea that even new Foo[5] could have type Foo![]!, since initialization ¯_(ツ)_/¯
I am too lazy to dig up threads right now to confirm that.
Current decision: Array subtyping is already and always strange. Java declares them to be covariant, and there's nothing we can do that's better than that.
Argument for changing: None.
Timing: This must be decided before version 1.0 of the spec.
Proposal for 1.0: Finalize the current decision. If you agree, please add a thumbs-up emoji (👍) to this comment. If you disagree, please add a thumbs-down emoji (👎) to this comment and briefly explain your disagreement. Please only add a thumbs-down if you feel you can make a strong case why this decision will be materially worse for users or tool providers than an alternative.
**Results: Five 👍s and no other votemojis.
Reacted by Chris Povirk, Kevin Bourrillion, David P. Baker, Manu Sridharan and Lázaro Clapp
Activity
kevinb9n commentedon Aug 14, 2019
To be far, what made Java's precedent of array covariance tolerable (in the beginning) was that at the worst you'd reliably get an ASE at runtime.
Since we don't have that, then it's how generics was mixed into that that probably forms a more useful precedent for us. There, creating an array of a parameterized type causes a warning (but only a warning). That said, I'm not actually sure how that should translate to our world.
I would say that a method returning an array for the caller to write into, or accepting an array parameter for its own implementation to write into, is probably irregular/unusual/already-problematic enough that it is not worth fitting the design to that case. And that says that continuing to treat arrays as covariant for our purposes is a defensible choice for usability reasons.
But does that go as far as wanting our semantics to actually dictate the following to be incorrect?
Not sure.
kevinb9n commentedon Aug 19, 2019
I think we are probably best to just embrace array covariance. Because arrays are exchanged in APIs far more often to be read than to be written to (I would bet > 20:1), and because Java already treats them as covariant, I think that the code snippet above should indeed be deemed incorrect. That seems like the cleanest thing to do.
I don't think the stakes are high on getting this decision perfect, because object arrays are already grossly inferior to collections (in every single way except as special-case optimizations), and type annotations already make that even worse (no one will ever find
@A String @B []as readable as@B List<@A String>).Propose to resolve.
cpovirk commentedon Aug 20, 2019
For API purposes, I'm all for setting on covariance as good enough. (Are there any existing checkers that do anything different? It sounds like the Checker Framework treats them as covariant by default (with an option to treat them as invariant).)
While we're on the subject, though, I looked into some of the questions that affect implementation code.
It's possible to create an array of a parameterized type with just a warning (well, 2 warnings):
(And you can hide the one with
@SafeVarargs-- not that it would be appropriate to do so on a method like this, which returns the array to the caller.)But the more direct way of creating one is an error:
So you have to use a wildcard and an unchecked cast:
Or, if you don't mind having both an unchecked warning and a rawtypes warning, you can do it in less code:
Once you have a
List<String>[], it's easy to set up a latentClassCastExceptionwith no further warnings:I, too, am not sure exactly how to apply all this to nullness yet. Certainly an attempt to create an array of non-null objects is at best "aspirational":
(If
@NotNullis the default, then it could be omitted. Perhaps it would be surprising for a line with no nullness annotations and no preexisting objects to produce a warning or even error?)(Another effect of the weird
@Nullable String[]syntax is that it looks like you have to annotate the variable -- normally not required by flow-sensitive nullness analysis -- because in fact you're annotating the element type.)Following the precedent of generics might mean requiring the following and issuing a warning:
(There's no "rawtypes workaround" in the case of
String[].)(Maybe it would be more honest for the code to store the array in a variable of type
@Nullable String[], fill it in, and then perform the unchecked cast to@NotNull String[]. I suppose that libraries could even provide a casting helper that performs a runtime check of the contents, though that still can't protect against future mutation through the original variable (edit: or after assigning to a new variable of typeNullable String[]?).)I was going to say that we could special-case creating an empty array, as in
new Foo[0], but:toArray(T[]), though, and maybe it helps them?)NullPointerExceptionby usingArrays.copyOf(empty, someNonZeroSize). (Not that we're trying to be 100% sound.) [edit: Is this solvable by makingcopyOfreturn a@Nullable T[]when given aT[]?][edit: another matter of implementation code: the type of
Foo[].class:Class<Foo[]>orClass<@Nullable Foo[]>? See jspecify/jdk@71d8267]wmdietlGC commentedon Aug 29, 2019
@cpovirk already mentioned how the Checker Framework handles array covariance in the previous comment, see https://checkerframework.org/manual/#invariant-arrays
By default the CF allows the creation of arrays with non-null component types, but provides an option to be stricter: https://checkerframework.org/manual/#nullness-arrays
and a
castNonNullDeephelper method to ensure correct initialization at runtime:https://github.com/typetools/checker-framework/blob/master/checker/src/main/java/org/checkerframework/checker/nullness/NullnessUtil.java#L82
cpovirk commentedon Sep 23, 2019
Checker Framework also has special handling of
Collection.toArray-- both overloads, I think. Certainly it's a weird API. Maybe we can take the easy way out (though I haven't even thought about this enough to say what that is :))wmdietlGC commentedon Oct 10, 2019
Following-up to my earlier comment at #33 (comment):
The assignment of
@NullUnspec String[]to@Nullable String[]should also produce a warning: the legacy code might assume that no null value is contained in that array and this client could break that.kevinb9n commentedon Oct 10, 2019
Certainly receiving an array across an API boundary and then writing to it is a thing that can happen, and therefore a soundness analyzer will want to generate the warning you describe (unless it wants to deem the assignment safe anyway from context). So we should not say anything that sounds like we are preventing it from doing that.
I think we should also not say anything that prevents the non-soundness-based checker from deciding to privilege the much more common use case over the other, because there are big advantages to most users for doing that.
Unfortunately we can't just leave this all up to checkers, because an API owner needs a single answer on whether they can safely make a method return
@Nullable String[]if it already has overrides returning@NullUnspec String[]or@NotNull String[].wmdietlGC commentedon Oct 10, 2019
Wasn't our approach to handling sound tools vs bugfinders that we specify certain things as hard errors and other things as various kinds of warnings. Certain warnings, like these warnings for conversions to/from
@NullUnspeccould be ignored by default by certain tools. Other tools could elect to make them hard errors.kevinb9n commentedon Oct 10, 2019
We know that we require the one kind of "warning" discussed in #33 (and the newly filed #73). I am not sure whether we want to get into "various kinds" of warnings at the level of this project or not. This one discussed here would be a different kind.
cpovirk commentedon Oct 25, 2019
A related issue: How much do we plan to encourage "lying" about array nullability when we annotate APIs (directly, like for Guava, or in stub files, like for the JDK)?
As an example, take this method:
(As noted above,
Collection.toArrayis arguably its own special thing, perhaps especiallytoArray(T[]). But let's pretend thatCollection.toArraydoesn't exist and that we're just defining our own method. If that's hard to do, then considerFluentIterable.toArray, which does define its own method, instead.)The returned array will never contain
null. However, as soon as we hand the user a@NotNull Object[](an array of not-null objects; hopefully I got the syntax right), we've opened up a type-system hole. (Well, probably. It depends on the decisions discussed in this issue.)The technically correct thing is going to be to return
@Nullable Object[]. But are we willing to put users through that? Maybe: If we're putting users ofMap.getthrough that (#71), then is this really any worse? (Maybe it is: At least withMap.get, you can callrequireNonNullon the result. Here, you have to suppress or manually loop if you want to pass an array to a method likeImmutableList.copyOf.)cpovirk commentedon Oct 25, 2019
...and I suppose we can ask the same question about parameter types:
Do we:
@NotNull E[]when there's really "no such thing" and someone has to suppress a warning to obtain a reference of such a type -- though maybe the suppression will be performed often by libraries, as discussed in my previous post?@Nullable E[]when we know that we would throw NPE if the array containednull? (This is somewhat like theJoinercase, in which appeared willing to use a signature that would permit NPE, but in that case, not all uses of null elements would NPE.)kevinb9n commentedon Oct 25, 2019
(First a boilerplate reminder that using object arrays will be approximately equally painful no matter what we do here.)
I suspect that for most array writes in existence it is relatively easy to verify that the array reference can't have undergone a covariant assignment (usually because it was locally initialized to
new SameType[]). All those writes are safe. All the trouble comes from the other writes, which I currently suspect to be a small minority.I think we don't want to let those naughty writes become a tail that wags the dog here. So I think we can embrace array covariance, to the point of saying:
Anything that should be safe as long as those naughty writes aren't happening somewhere is safe enough for us; a checker can always choose to be more concerned about that risk.
Anything that tries to treat an array as contravariant though is right out.
kevinb9n commentedon Oct 25, 2019
so, re: the specific question just asked, I definitely very much want those APIs to be able to say
@NotNull E[]. That's good documentation even if it were nothing else.cpovirk commentedon Oct 25, 2019
And, if all we're concerned about is nullness (as opposed to solving the larger problem of array covariance), then we can also permit any attempt to write a value into an array if we know that that value is not null. This probably covers a good fraction of the remaining cases.
17 remaining items
cpovirk commentedon Jan 24, 2024
A couple notes about the implications for
toArraymethods:ImmutableCollectioncan override the no-argtoArray()to returnObject[]instead of the default of@Nullable Object[]. (I think this works out OK for Kotlin callers, but it does not work if you try to write a similar hierarchy in Kotlin, since Kotlin arrays are not covariant.)toArrayoverload, we've been using the signature<T extends @Nullable Object> T[] toArray(T[]). I just wrote something on that in a comment on the internal review thread for Make J2CL super-source'd classes pass nullness checking. google/guava#6942:toArray(IntFunction<T[]>)overload.kevinb9n commentedon Jan 31, 2024
Re: bullet 2, it's a weird method that is really two in one: (a) the one where you actually expect elements to be written in, and (b) the one where you're just using an empty array as a type token.
In case (a), it's the kind of method I was discussing way up top:
... and I still think it's fine to throw this case under the bus (and we really have no other choice).
In case (b), logically the same type token ought to work whether the caller wants to get a
Foo![]!or aFoo?[]!back out. And that type token itself could be either aFoo![]!or aFoo?[]!, couldn't it? (aside: it seems like an analyzer should treat a literalnew Foo[0]ornew Foo[] {}as having the typeFoo![]!, as there's just no call for bringing null into the picture, so to speak.) So in theory all four combinations are valid?(What's more dodgy is trying to get a
Foo![]!from aCollection!<Foo?>, but I believe we've already made our peace with this anomaly is that right? And that's an issue regardless of what gets passed in.)kevinb9n commentedon Jan 31, 2024
(so to be more clear I'm possibly raising a very minor question over whether
<T extends @Nullable Object> T[] toArray(T[])is the right signature or not... but mostly I'm just confused, this stuff is hard to think about.)cpovirk commentedon Feb 2, 2024
I wrote a bunch of theoretical stuff and then deleted it :)
[edit: The continuation of the
toArraydiscussion has moved to #481.]As for the aside:
I think we've to some extent made peace with the idea that even
new Foo[5]could have typeFoo![]!, since initialization ¯_(ツ)_/¯I am too lazy to dig up threads right now to confirm that.
netdpb commentedon Apr 9, 2024
Current decision: Array subtyping is already and always strange. Java declares them to be covariant, and there's nothing we can do that's better than that.
Argument for changing: None.
Timing: This must be decided before version 1.0 of the spec.
Proposal for 1.0: Finalize the current decision. If you agree, please add a thumbs-up emoji (👍) to this comment. If you disagree, please add a thumbs-down emoji (👎) to this comment and briefly explain your disagreement. Please only add a thumbs-down if you feel you can make a strong case why this decision will be materially worse for users or tool providers than an alternative.
**Results: Five 👍s and no other votemojis.
JSpecify subtyping checks for arrays (#956)
netdpb commentedon Jun 13, 2024
Decision: Array subtyping is already and always strange. Java declares them to be covariant, and there's nothing we can do that's better than that.
[-]Subtyping rules for array types [working decision: covariant][/-][+]Subtyping rules for array types[/+]