Repository navigation
Description
We've discussed using @NullUnspecified as a way of saying something like "a smarter tool would be able to tell you when this is safe" -- or just "this method is used commonly enough that some users/owners are unwilling to tolerate null checks on every call size." See this earlier discussion from when we discussed using a @LessEnforced annotation for this and this even earlier discussion.
There are a number of places that this could be useful. Rather than dumping these into #32 (as I did recently), I'll start a new bug with a list, carrying over items from past discussions:
Possible usages for @NullnessUnspecified on return types:
Map.getActivity.findViewById(rough stats on how often callers perform a null check on the result)Context.getSystemServiceFragment.getContextThrowable.getCauseResourceLoader.getClassLoaderThread.getUncaughtExceptionHandlerHttpMethod.resolveDateTimeFormatterFactoryBean.getObjectMap.Entry.getValue: technically "undefined" if the entry has been removed, but some implementations returnnullin that case. Checker Framework leaves it returningV, but we could switch to@NullUnspecified Vif we wanted to be paranoid.Matcher.group(int)Injector.getInstance()- various reflection APIs
Possible usages for @NullnessUnspecified on parameter types:
Joiner.joinMoreObjects.firstNonNull? (Or maybe we'd consider this likecheckNotNull, for which we might allownullinputs that might result in aNullPointerException?)Collection.contains?requireNonNull/checkNotNull?- various reflection APIs
Another category of use cases is @PolyNull, whose status is up in the air. I've compiled a list of @PolyNull methods.
(I got to wondering if an evil way to support @PolyNull without @PolyNull would be to allow stubs to contain 2 versions of a method -- so one could include @Nullable and the other not. This might work, but it would probably require special rules, and anyway, it would require stub files for a library, while we want code to be annotated inline -- and we especially wouldn't want it to be mostly annotated inline but with magic elsewhere.)
Activity
kevinb9n commentedon Oct 8, 2019
I just want to reassert that I think condoning the use of
@NullUnspecfor these methods should be a last resort, to be avoided if at all possible.I think all these type usages are nullable. Then we've done everything we can do via the avenue of a type system to prevent errors. Analyzers can always do more.
cpovirk commentedon Oct 8, 2019
Even
Map.get? My impression was that you were fairly recently in favor of using@NullUnspecthere (and presumably in the similar Android cases).kevinb9n commentedon Oct 10, 2019
I would not describe myself as ever having been "in favor" of it, so much as against defining a fifth kind of nullness. And musing aloud on the question of how much we should try to account for the fact that even if we declare
@Nullableas the right way to go many users will still go this route anyway since it might appear to do something like what they want. I don't feel good about@NullUnspeconMap.getat all.cpovirk commentedon Oct 11, 2019
Oops, sorry, thanks. I think my belief was based on remembering that you'd closed the
@LessEnforcedissue in favor of@NullUnspecified, apparently backtracking on your hope to support "cop-out" use cases without@NullUnspecified.But it sounds like your position is that
Map.getis clearly@Nullable, as are the Android cases, but you're willing to live with the Android team's decision to apply@NulUnspecifiedinstead, since we're not likely to provide another way to get the behavior they'd like?PeteGillin commentedon Nov 1, 2019
When you talk about
Map.get, you're talking about the return type, I guess? Because there's also a slight question mark over the parameter type: the spec allowsget(null)to NPE, and it does forConcurrentHashMap. I guess there's still no reasonable choice forMapexcept to say that its argument is@Nullable Object. That raises the question of whetherCHM.getis allowed to take@NonNull Object, which is accurate but would be an illegal override if we really treated nullability like a type hierarchy. But maybe that's a different question to the one that this bug is addressing.PeteGillin commentedon Nov 1, 2019
One method that I think should be added to the list for this issue is
Thread.getUncaughtExceptionHandler(), which returns null but only if the thread has terminated. In Android, I made its return type nullable, which broke some folks legitimately not checking for null because they know the thread is running (e.g. because it'sThread.currentThread()).I guess that
Thread.getThreadGroup()is the same.stephan-herrmann commentedon Nov 5, 2019
Since
Map.getclearly mentions thenullcase in its javadoc, I don't see a way around a@Nullable Vreturn type. If google likes to help users for a particular application ofMapwhere it is possible to analyse if a given key "exists", then they are free to define a new subtype ofMapwith a different contract. Users of platforms other than Android should really not suffer any additional complexity due to this one use case.PeteGillin commentedon Nov 5, 2019
I think there's been some miscommunication. Android, AFAIK, does indeed declare
@Nullable V get(@Nullable Object). I don't think we consider ourselves special in this case. I know that there are some folks (not Android-specific) who would prefer to be more liberal. I'm pretty sure that Android will go along with whatever the consensus is here.There are some interesting Android-specific examples, such as the return type of
Activity.findViewById(int). The overwhelmingly common way of using this is to pass in a constant defined in generated code as its argument, e.g.findViewById(R.id.toolbar), and it is guaranteed to return non-null when you do that. For anyone following that pattern, which is practically everyone, requiring then to do null checks adds friction. But it is formally speaking nullable, because it returns null for other arguments.kevinb9n commentedon Nov 5, 2019
Back to the
Map.getkey parameter type: that's a nasty one, but I think the best we can do is to say that some implementations are kinda "doing it wrong". They should arguably reannotate the key to @NotNull and suppress the warning that results just in case someone is actually speaking directly to that static type; on the other hand, maybe we don't want to encourage that kind of thing.cpovirk commentedon Dec 2, 2019
Anecdote from @dzharkov:
Naturally, as he also points out:
But I think this is overall yet more encouragement to make
getreturn@Nullable.I don't think we're in danger of making any kind of binding decision here, but I would say that, if people feel strongly the other way, it would be good to hear about it at their convenience. If we end up using
@NullnessUnspecifiedmore widely than anticipated, we might want to take that into account while we make decisions about its semantics.[-]@NullUnspecified possible use cases[/-][+]@NullnessUnspecified possible use cases[/+]cpovirk commentedon Mar 23, 2020
We have a small internal discussion going about
Matcher.group(int)andgroup(String). They're another case in which@Nullableis clearly correct but probably inconvenient in a large fraction of cases. I doubt this is fundamentally different thanMap.get, though.amaembo commentedon Mar 24, 2020
Yes, we don't annotate
Matcher.groupmethods as Nullable. A sophisticated analysis could be done to check whether the original pattern contains alternating groups, but this is out of the scope of our project and cannot be always determined statically. I agree that the problem is not substantially different fromMap.get.15 remaining items
[-]Decide our stance on "the `findViewById` problem" (generally nullable; in *common* specific cases not)[/-][+]General solutions for "the `findViewById` problem" (generally nullable; in *common* specific cases not)[/+]kevinb9n commentedon Sep 14, 2022
The feeling has continued to gnaw at me that we will have to have some reasonable approach to this case by 1.0 (but not by 0.3).
It could be just advice for users that is almost satisfying while being better than "leave it unspec". If such a stance exists.
My concern is that without this, users will have irritating experiences that will get disproportionate attention.
[-]General solutions for "the `findViewById` problem" (generally nullable; in *common* specific cases not)[/-][+]Address the "`findViewById` problem" (generally nullable, but in *common* specific cases not) [working decision: no][/+]kevinb9n commentedon Mar 13, 2024
Hello from 18 months later. I think it's now fair to assume we won't have anything special for this case in 1.0. I'm moving to post-1.0, and filing a separate doc bug for what to do in the meantime.
[-]Address the "`findViewById` problem" (generally nullable, but in *common* specific cases not) [working decision: no][/-][+]Address the "`findViewById` problem" (generally nullable, but in *common* specific cases not)[/+]netdpb commentedon Apr 9, 2024
Current decision: JSpecify 1.0 will not explicitly address this problem.
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.