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
Naming the third nullness annotation (the one that matches the behavior of unannotated code) #32
We have seemed fairly comfortable with @Nullable and @NotNull (the latter being a shorthand for @NotNullable), but oy, the third one.
The word "legacy" should be involved; we have never come up with something more appropriate. It is the nullness that all Java types have by default before someone goes through and annotates. Admittedly, people will probably use it (rarely) for special cases with non-legacy code, but we think we want to at least discourage that.
I think we also don't want to talk about it as being more closely related to @Nullable than to @NotNull; its relationship to these two should be symmetric. It represents indecision between the two. Conservative analysis treats it more like @Nullable when assigning from, but more like @NotNull when assigning to; lenient analysis would have it the other way around.
Cases like Map.get might be rare among declarations, but are quite prominent usage-wise, and the word "legacy" in such core an API might scare or confuse people. How about something more neutral, e.g. "Unknown" or "Unspecified" or "Any"?
You are right. After saying "it should involve the word legacy", I made the list of names and ended up thinking of @NullUnchecked, and now I'm growing to like it a lot. As an adjective in prose, "null-unchecked" or (when context is clear) "unchecked".
I believe the connection to the "unchecked warnings" developers are familiar with actually might be a good one: it's another case of a type conversion where the types are not truly assignable but we let you make the type conversion anyway.
The analogy to unchecked is interesting. One potential disadvantage is that it already has a specific meaning / baggage. I also see parallels with rawtypes: aren't legacy nullable types raw with respect to nullness information?
The more rare the word, the stronger the connection should have to be for us to want to use it. The generics concept of "raw" is more parallel, but it's a more unusual word so we should be convinced it's "a lot" more parallel.
We think there's virtually no reason to ever use a raw type (in the generics sense) aside from compatibility and ~3 very narrow edge cases. Would we say the same for this? I thought we would, and that was behind my preference for "legacy", but now I'm moving away from that thanks to things like Map.get.
The word "unchecked", root "check", feels pretty basic/common. We describe the systems that use our annotation as "checkers". In some sense, this annotation is akin to telling those checkers to back off a bit on what they do. The more accurate that description is then the better this name is.
That the nullness is "unknown" is something this shares in common with parametric nullness. The difference, I think, is that we're fine with saying that "parametric is not assignable to not-null", full stop, just like nullable is not assignable to not-null, whereas with legacy/unknown/unchecked/whatever, we're willing to make accommodations. (And similarly with "nullable is not assignable to parametric".)
"Unchecked" for me implies some specific behavior expectations from checkers, and I thought we didn't intend to prescribe how they should work. If combined with more specific annotations (in spirit of @PolyNull), "unchecked" could be wrong, because such declarations would still be checked, just in a more sophisticated way.
This name also seems to imply that the annotations are there mostly for checkers, and not e.g. for documentation or other purposes. This might be fine, I'm just not totally sure it is.
Thanks for bringing that up. You're right, avoiding seeming to dictate what a checker should do is important, and that should even have an effect on how things are named. So this is a negative for @Unchecked.
The following may seem weird, but I think it is possible that there is much less of a problem with implying what checkers should not do than there is with implying what they should. The latter part I think we might agree solidly that we need to avoid, but the first part might not be harmful and even be virtuous in making sure library owners are able to annotate properly. I don't know.
(In these discussions I think we will lapse into "and the checkers we imagine will do this" over and over. Perhaps it is inevitable that we have to have some imagined checker to shape our thoughts around, and maybe it will be enough if we remember to repeatedly ask ourselves "okay, but how much of that actually needs to be required of checkers?)
changed the title [-]Naming the legacy nullness annotation[/-][+]Naming the third nullness annotation (the one that matches the behavior of unannotated code)[/+]on Jun 21, 2019
I don't like @Unchecked for the reasons @donnerpeter gives. @UnknownNullness seems better.
Some other options: @JavaNullness, @ClassicNullness, @StandardNullness, or @MachineNullness. But they all have their downsides.
Hmm, "unknown" used to always be on the list, and then there was some reason on Monday that seemed clear to me it could be ruled out. Now I don't know why that was. Back on the table.
In my experience words like "unknown" will easily entail questions like: "why doesn't the tool know? Can't it figure it out?" etc. At Eclipse we had similar fights with the word "potential", where users are confused whether the danger depends on (a) control flow, (b) instantiation of a generic type, (c) some nullable value (which could still be different from null 😄) etc. pp.
From that experience I tend to be explicit about what is the source of uncertainty. In the case here: it's unspecified.
OTOH, could s.o. please point me to the discussion, why this concept (which surely is necessary for the semantics) must have an explicit expression in the syntax? To me it feels a bit like requesting a syntax like List<#RAW>, where actually List says the same already.
If we can't find a name that serves both use cases, we should also consider shifting the focus to what you call the "unspecifiable" case. My thinking:
The case of "I don't want to annotate this yet" might come up a lot when users don't want to annotate whole methods or classes (for which users would use declaration annotations), but...
I don't think users will commonly need to annotate only some of the types in a single method signature (for which they would use type annotations). (And if they do, we might supply them with ways to do that besides by using a @NullnessUnspecified type annotation.)
So maybe we end up with a declaration annotation like @AnnotatedForNullness(false) but a type-use annotation like @NullnessUnspecifiable.
I forget whether we have previously discussed using @Nullable for this but with an element/method/value, as in @Nullable(weWouldLikeToDoBetterThanOutrightNullableButJspecifyIsLettingUsDown = true).
The reason that I thought of this is that it could help with #100 (by making @Nullable @NullnessUnspecified impossible). But I don't think that should be a significant motivation.
What we might like more is that it "hides" the "unspecified" case better than a top-level annotation does. After all, we expect that case to be rare. And users who do need that case have surely already seen @Nullable, so they might understand this new annotation as "nullable, but ...." We can still hold out hope that all we really need to provide is @DefaultNonNull and @Nullable: Such a simple API makes it easy for users to get started, and it gives us some breathing room to add other nullness annotations down the line (@PolyNull? @EnsuresNonNull?).
On the other hand, putting the "unspecified" case into @Nullable is in some ways the opposite of hiding it: I would have to look into how IDEs handle autocompletion for annotations, but we might be inadvertently thrusting the "unspecified" case in front of users who are just trying to type @Nullable.
(Another way to attempt the "hiding" goal (but not to help with #100) is to make this third annotation a nested type enclosed by @Nullable: @Nullable.WeWouldLikeToDoBetterThanOutrightNullableButJspecifyIsLettingUsDown. This might be better or worse than the approach of using an annotation element, depending on IDE autocompletion, Javadoc rendering, and probably other factors.)
If we want to think about an annotation element or nested type, we should also think more about whether we would use those features for possible future additions -- like using it for @PolyNull.
Would we want it to be possible to express onlyJointlyandweWouldLikeToDoBetterThanOutrightNullableButJspecifyIsLettingUsDowntogether? Probably not. That's an argument for not doing both as annotation elements. Not that anyone was proposing doing even one of them that way until just now :)
While I understand the desire to allow documenting "why" something is
annotated @NullnessUnspecified, I have to say I don't relish the idea of
"hardcoding" various reasons into the annotation language. There's the
obvious problem that we may not cover all reasonable reasons, and then
people will still need to use something. An additional concern of mine is
that having an annotation like this at all is somewhat surprising/confusing
already, and I personally basically hope most people don't have to know
about it; having multiple annotations for the same thing seemingly just
calls more (undue) attention to it. It seems that comments can be used to
express intent, or there was at some point the idea of giving type-use
annotations an explanation string "value" attribute (i.e.,
@NullnessUnspecified("TODO: look at this more closely").
Regarding making "unspecified" a flavor of @nullable, I think that was the
idea behind @LessEnforced. It seemingly has appeal b/c "strict" tools may
be able to "just ignore" the less enforced part and
treat @NullnessUnspecified as just @nullable. I think there were other
reasons against this, but one issue with that is that we may get into hot
water with our "unchecked conversions" that we want tools to apply. For
instance, if a base class method has a parameter
annotated @nullable(butDontTellMe=true), and I override it with @nonnull,
then we want that to be legal (with optional warning), but a tool that
ignores "butDontTellMe" would flag it as a violation of behavioral
subtyping. That is to say, it appears we need *all* tools to do special
things for "unspecified" sometimes (though those cases are admittedly rare,
as most of it falls under checker-specific discretion), and to me it seems
the best way to get that is by having a separate annotation for it.
Switching gears again: When will users first be forced to understand unspecified nullness? Often, it will happen when they encounter an error message that mentions it. For example:
error: incompatible types: @Nullable Map<@NullnessUnspecified Foo, @NullnessUnspecified Bar> cannot be converted to Map<Foo, Bar>
In Kotlin-like terms, that's saying:
Type mismatch: inferred type is Map<Foo!, Bar!>? but Map<Foo, Bar> was expected
The appearance of @NullnessUnspecified in error messages changes how I feel about the long name:
After: If people may see @NullnessUnspecified frequently in error messages, then we may worry more about the length. In particular, I'm sad that @NullnessUnspecified is so noisy when it is never the cause of the failure -- at least in "lenient mode." Look back at the error message above: @NullnessUnspecified is the most prominent but least important part! (That's arguably made even sadder by the fact that the token "@NullnessUnspecified" never appears in the source file I'm imagining.)
Possible solutions (some of which require action only by tool authors, not spec authors):
Lenient-mode tools could hide @NullnessUnspecified from their output altogether. The downside: This will eventually confuse someone: "Why can I return a @Nullable T from this method that returns T [which secretly means @NullnessUnspecified T] but not from this other method that returns T [which means 'just T']?" This confusion would not exist if we were honest and said "@NullnessUnspecified T" in the first place. Similarly, users may be confused when they get a NullPointerException from dereferencing an object that their tools say is of type "String" (when the tools mean "@NullnessUnspecified String"). @NullnessUnspecified is already complicated, given that it's the default for unannotated code, so I worry about making it more complicated.
Tools could print error messages in "Kotlin style" -- that is, by printing ! and ? instead of annotations. The downside: This style is less familiar to some users (though more familiar to others). And somehow, users need to understand the mappings @Nullable <-> ? and @NullnessUnspecified <-> ! (unlike in pure Kotlin, which uses ? for both source code and error messages). (Additionally, I don't love the use of ! for "unspecified": To me, ! suggests "not null" (for which Kotlin uses !!). Maybe tools would pick a different symbol (~, ¿, ...) but at the cost of familiarity to Kotlin users.)
We could pick a shorter name for the annotation. The downside: Not only is the name less clear in source, it is less clear in error messages -- which, again, is when many users will encounter it for the first time.
Another thing to recall is that we haven't fully settled whether a @NullnessUnspecified type annotation will even exist [edit: #137]. Suppose for a moment that it does not:
In one way, that simplifies things: We can say whatever we want in error messages, since there will be no equivalent source code that we would want them to correspond to.
In another way, it complicates things: No matter what we pick for our error messages (@NullnessUnspecified, unspecified, [@Nullable?], @???, !, ...), users who want to understand it can no longer just look up the class with that name. (And note that symbols like ? are particularly hard to query for in your favorite search engine. Tools can at least try to help by linking to useful documentation.)
@NullnessUnspecified is so noisy when it is never the cause of the failure
Thanks for observing this.
To me, ! suggests "not null"
That is in fact what it meant in Spec# I believe. I've at times used !?, FWIW, but am not claiming it's a great solution.
It may not be popular, but besides String!? there does seem to be another reasonable solution: just String could be used to mean @NullnessUnspecified String / String!? in error messages as long as both other options, @NonNull String / String!! and @Nullable String / String?, are always made explicit. This would remove "unspecified" clutter as desired and is consistent with the observation that
the token "@NullnessUnspecified" never appears in the source file I'm imagining.
I understand that this would take us away from Kotlin's error messages and is incompatible with the desire to generally write String!! as String. Instead, this solution would leverage "unspecified" as the default-default (so should still be reasonably intuitive to Java users).
The other thing I'll say is that while good error messages will be important, I suspect it's probably out of scope for us to specify error messages exactly. So while I support the effort to make sure tools can produce reasonable messages, I also think we want to be careful not to wade too far into the area: seems likely to me that tools will want some room for experimentation.
Activity
donnerpeter commentedon Jun 20, 2019
Cases like Map.get might be rare among declarations, but are quite prominent usage-wise, and the word "legacy" in such core an API might scare or confuse people. How about something more neutral, e.g. "Unknown" or "Unspecified" or "Any"?
kevinb9n commentedon Jun 20, 2019
You are right. After saying "it should involve the word legacy", I made the list of names and ended up thinking of
@NullUnchecked, and now I'm growing to like it a lot. As an adjective in prose, "null-unchecked" or (when context is clear) "unchecked".I believe the connection to the "unchecked warnings" developers are familiar with actually might be a good one: it's another case of a type conversion where the types are not truly assignable but we let you make the type conversion anyway.
Does anyone dislike this name?
cushon commentedon Jun 20, 2019
The analogy to
uncheckedis interesting. One potential disadvantage is that it already has a specific meaning / baggage. I also see parallels withrawtypes: aren't legacy nullable types raw with respect to nullness information?eaftan commentedon Jun 20, 2019
I like something like
@UnknownNullness. It is descriptive and expresses program semantics.kevinb9n commentedon Jun 20, 2019
The more rare the word, the stronger the connection should have to be for us to want to use it. The generics concept of "raw" is more parallel, but it's a more unusual word so we should be convinced it's "a lot" more parallel.
We think there's virtually no reason to ever use a raw type (in the generics sense) aside from compatibility and ~3 very narrow edge cases. Would we say the same for this? I thought we would, and that was behind my preference for "legacy", but now I'm moving away from that thanks to things like
Map.get.The word "unchecked", root "check", feels pretty basic/common. We describe the systems that use our annotation as "checkers". In some sense, this annotation is akin to telling those checkers to back off a bit on what they do. The more accurate that description is then the better this name is.
That the nullness is "unknown" is something this shares in common with parametric nullness. The difference, I think, is that we're fine with saying that "parametric is not assignable to not-null", full stop, just like nullable is not assignable to not-null, whereas with legacy/unknown/unchecked/whatever, we're willing to make accommodations. (And similarly with "nullable is not assignable to parametric".)
donnerpeter commentedon Jun 21, 2019
"Unchecked" for me implies some specific behavior expectations from checkers, and I thought we didn't intend to prescribe how they should work. If combined with more specific annotations (in spirit of
@PolyNull), "unchecked" could be wrong, because such declarations would still be checked, just in a more sophisticated way.This name also seems to imply that the annotations are there mostly for checkers, and not e.g. for documentation or other purposes. This might be fine, I'm just not totally sure it is.
kevinb9n commentedon Jun 21, 2019
Thanks for bringing that up. You're right, avoiding seeming to dictate what a checker should do is important, and that should even have an effect on how things are named. So this is a negative for
@Unchecked.The following may seem weird, but I think it is possible that there is much less of a problem with implying what checkers should not do than there is with implying what they should. The latter part I think we might agree solidly that we need to avoid, but the first part might not be harmful and even be virtuous in making sure library owners are able to annotate properly. I don't know.
(In these discussions I think we will lapse into "and the checkers we imagine will do this" over and over. Perhaps it is inevitable that we have to have some imagined checker to shape our thoughts around, and maybe it will be enough if we remember to repeatedly ask ourselves "okay, but how much of that actually needs to be required of checkers?)
[-]Naming the legacy nullness annotation[/-][+]Naming the third nullness annotation (the one that matches the behavior of unannotated code)[/+]kevinb9n commentedon Jun 21, 2019
Resolving this probably depends on resolving #32. (EDIT: ha, I meant #33)
wmdietlGC commentedon Jun 21, 2019
I don't like
@Uncheckedfor the reasons @donnerpeter gives.@UnknownNullnessseems better.Some other options:
@JavaNullness,@ClassicNullness,@StandardNullness, or@MachineNullness. But they all have their downsides.cushon commentedon Jun 21, 2019
@UnknownNullnessis looking OK to me too.kevinb9n commentedon Jun 21, 2019
Hmm, "unknown" used to always be on the list, and then there was some reason on Monday that seemed clear to me it could be ruled out. Now I don't know why that was. Back on the table.
kevinb9n commentedon Jun 26, 2019
We seem to be converging on
@NullnessUnknown.(Should I still make a case for
@NullUnknown?)stephan-herrmann commentedon Aug 2, 2019
In my experience words like "unknown" will easily entail questions like: "why doesn't the tool know? Can't it figure it out?" etc. At Eclipse we had similar fights with the word "potential", where users are confused whether the danger depends on (a) control flow, (b) instantiation of a generic type, (c) some nullable value (which could still be different from null 😄) etc. pp.
From that experience I tend to be explicit about what is the source of uncertainty. In the case here: it's unspecified.
OTOH, could s.o. please point me to the discussion, why this concept (which surely is necessary for the semantics) must have an explicit expression in the syntax? To me it feels a bit like requesting a syntax like
List<#RAW>, where actuallyListsays the same already.wmdietlGC commentedon Aug 2, 2019
The discussion of why we make the third state explicitly expressible can be found here: #2
25 remaining items
wmdietlGC commentedon Apr 3, 2020
#27 discussed
@LessEnforcedas a separate concept from@NullnessUnspecified.cpovirk commentedon Apr 6, 2020
If we can't find a name that serves both use cases, we should also consider shifting the focus to what you call the "unspecifiable" case. My thinking:
@NullnessUnspecifiedtype annotation.)So maybe we end up with a declaration annotation like
@AnnotatedForNullness(false)but a type-use annotation like@NullnessUnspecifiable.mernst commentedon Apr 6, 2020
It sounds like you are in agreement that we need to distinguish the cases of:
For the former, a declaration rather than type annotation for this is a nice solution.
For the latter, just "unspecifiable" is a misleading name. The semantics is not unspecifiable, it's just unspecifiable by this version of Jspecify.
cpovirk commentedon Apr 13, 2020
Switching gears:
I forget whether we have previously discussed using
@Nullablefor this but with an element/method/value, as in@Nullable(weWouldLikeToDoBetterThanOutrightNullableButJspecifyIsLettingUsDown = true).The reason that I thought of this is that it could help with #100 (by making
@Nullable @NullnessUnspecifiedimpossible). But I don't think that should be a significant motivation.What we might like more is that it "hides" the "unspecified" case better than a top-level annotation does. After all, we expect that case to be rare. And users who do need that case have surely already seen
@Nullable, so they might understand this new annotation as "nullable, but ...." We can still hold out hope that all we really need to provide is@DefaultNonNulland@Nullable: Such a simple API makes it easy for users to get started, and it gives us some breathing room to add other nullness annotations down the line (@PolyNull?@EnsuresNonNull?).On the other hand, putting the "unspecified" case into
@Nullableis in some ways the opposite of hiding it: I would have to look into how IDEs handle autocompletion for annotations, but we might be inadvertently thrusting the "unspecified" case in front of users who are just trying to type@Nullable.(Another way to attempt the "hiding" goal (but not to help with #100) is to make this third annotation a nested type enclosed by
@Nullable:@Nullable.WeWouldLikeToDoBetterThanOutrightNullableButJspecifyIsLettingUsDown. This might be better or worse than the approach of using an annotation element, depending on IDE autocompletion, Javadoc rendering, and probably other factors.)cpovirk commentedon Apr 13, 2020
If we want to think about an annotation element or nested type, we should also think more about whether we would use those features for possible future additions -- like using it for
@PolyNull.Would we want it to be possible to express
onlyJointlyandweWouldLikeToDoBetterThanOutrightNullableButJspecifyIsLettingUsDowntogether? Probably not. That's an argument for not doing both as annotation elements. Not that anyone was proposing doing even one of them that way until just now :)kevin1e100 commentedon Apr 13, 2020
cpovirk commentedon Sep 23, 2020
Switching gears again: When will users first be forced to understand unspecified nullness? Often, it will happen when they encounter an error message that mentions it. For example:
error: incompatible types: @Nullable Map<@NullnessUnspecified Foo, @NullnessUnspecified Bar> cannot be converted to Map<Foo, Bar>In Kotlin-like terms, that's saying:
Type mismatch: inferred type is Map<Foo!, Bar!>? but Map<Foo, Bar> was expectedThe appearance of
@NullnessUnspecifiedin error messages changes how I feel about the long name:Before: If people should rarely write
@NullnessUnspecifiedin source code, we probably don't mind making them write a long name.After: If people may see
@NullnessUnspecifiedfrequently in error messages, then we may worry more about the length. In particular, I'm sad that@NullnessUnspecifiedis so noisy when it is never the cause of the failure -- at least in "lenient mode." Look back at the error message above:@NullnessUnspecifiedis the most prominent but least important part! (That's arguably made even sadder by the fact that the token "@NullnessUnspecified" never appears in the source file I'm imagining.)Possible solutions (some of which require action only by tool authors, not spec authors):
Lenient-mode tools could hide
@NullnessUnspecifiedfrom their output altogether. The downside: This will eventually confuse someone: "Why can I return a@Nullable Tfrom this method that returnsT[which secretly means@NullnessUnspecified T] but not from this other method that returnsT[which means 'justT']?" This confusion would not exist if we were honest and said "@NullnessUnspecified T" in the first place. Similarly, users may be confused when they get aNullPointerExceptionfrom dereferencing an object that their tools say is of type "String" (when the tools mean "@NullnessUnspecified String").@NullnessUnspecifiedis already complicated, given that it's the default for unannotated code, so I worry about making it more complicated.Tools could print error messages in "Kotlin style" -- that is, by printing
!and?instead of annotations. The downside: This style is less familiar to some users (though more familiar to others). And somehow, users need to understand the mappings@Nullable <-> ?and@NullnessUnspecified <-> !(unlike in pure Kotlin, which uses?for both source code and error messages). (Additionally, I don't love the use of!for "unspecified": To me,!suggests "not null" (for which Kotlin uses!!). Maybe tools would pick a different symbol (~,¿, ...) but at the cost of familiarity to Kotlin users.)We could pick a shorter name for the annotation. The downside: Not only is the name less clear in source, it is less clear in error messages -- which, again, is when many users will encounter it for the first time.
Another thing to recall is that we haven't fully settled whether a
@NullnessUnspecifiedtype annotation will even exist [edit: #137]. Suppose for a moment that it does not:In one way, that simplifies things: We can say whatever we want in error messages, since there will be no equivalent source code that we would want them to correspond to.
In another way, it complicates things: No matter what we pick for our error messages (
@NullnessUnspecified,unspecified,[@Nullable?],@???,!, ...), users who want to understand it can no longer just look up the class with that name. (And note that symbols like?are particularly hard to query for in your favorite search engine. Tools can at least try to help by linking to useful documentation.)kevin1e100 commentedon Sep 28, 2020
Thanks for observing this.
That is in fact what it meant in Spec# I believe. I've at times used
!?, FWIW, but am not claiming it's a great solution.It may not be popular, but besides
String!?there does seem to be another reasonable solution: justStringcould be used to mean@NullnessUnspecified String/String!?in error messages as long as both other options,@NonNull String/String!!and@Nullable String/String?, are always made explicit. This would remove "unspecified" clutter as desired and is consistent with the observation thatI understand that this would take us away from Kotlin's error messages and is incompatible with the desire to generally write
String!!asString. Instead, this solution would leverage "unspecified" as the default-default (so should still be reasonably intuitive to Java users).The other thing I'll say is that while good error messages will be important, I suspect it's probably out of scope for us to specify error messages exactly. So while I support the effort to make sure tools can produce reasonable messages, I also think we want to be careful not to wade too far into the area: seems likely to me that tools will want some room for experimentation.
cpovirk commentedon Sep 28, 2020
On the topic of using symbols for types, I opened #129 and left a reply there.
kevinb9n commentedon Sep 20, 2022
Reopen if we reverse course on #137.