Skip to content

Address the "findViewById problem" (generally nullable, but in *common* specific cases not) #71

Open

Description

@cpovirk

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:

Possible usages for @NullnessUnspecified on parameter types:

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

  1. kevinb9n commented on Oct 8, 2019

    @kevinb9n
    Collaborator

    I just want to reassert that I think condoning the use of @NullUnspec for 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.

  2. cpovirk commented on Oct 8, 2019

    @cpovirk
    CollaboratorAuthor

    Even Map.get? My impression was that you were fairly recently in favor of using @NullUnspec there (and presumably in the similar Android cases).

  3. kevinb9n commented on Oct 10, 2019

    @kevinb9n
    Collaborator

    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 @Nullable as 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 @NullUnspec on Map.get at all.

  4. cpovirk commented on Oct 11, 2019

    @cpovirk
    CollaboratorAuthor

    Oops, sorry, thanks. I think my belief was based on remembering that you'd closed the @LessEnforced issue 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.get is clearly @Nullable, as are the Android cases, but you're willing to live with the Android team's decision to apply @NulUnspecified instead, since we're not likely to provide another way to get the behavior they'd like?

  5. added
    nullness
    on Oct 25, 2019
  6. PeteGillin commented on Nov 1, 2019

    @PeteGillin
    Collaborator

    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 allows get(null) to NPE, and it does for ConcurrentHashMap. I guess there's still no reasonable choice for Map except to say that its argument is @Nullable Object. That raises the question of whether CHM.get is 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.

  7. PeteGillin commented on Nov 1, 2019

    @PeteGillin
    Collaborator

    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's Thread.currentThread()).

    I guess that Thread.getThreadGroup() is the same.

  8. stephan-herrmann commented on Nov 5, 2019

    @stephan-herrmann

    But it sounds like your position is that Map.get is clearly @nullable, as are the Android cases, but you're willing to live with the Android team's decision to apply @NulUnspecified instead, since we're not likely to provide another way to get the behavior they'd like?

    Since Map.get clearly mentions the null case in its javadoc, I don't see a way around a @Nullable V return type. If google likes to help users for a particular application of Map where it is possible to analyse if a given key "exists", then they are free to define a new subtype of Map with a different contract. Users of platforms other than Android should really not suffer any additional complexity due to this one use case.

  9. PeteGillin commented on Nov 5, 2019

    @PeteGillin
    Collaborator

    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.

  10. kevinb9n commented on Nov 5, 2019

    @kevinb9n
    Collaborator

    Back to the Map.get key 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.

  11. cpovirk commented on Dec 2, 2019

    @cpovirk
    CollaboratorAuthor

    Anecdote from @dzharkov:

    In Kotlin, [Map.get] always had a nullable return type and users used to it.

    Naturally, as he also points out:

    But for Java users, it might be paintful because there's already a lot of code that doesn't check returned value and that code doesn't fail because the user somehow is sure about it.

    But I think this is overall yet more encouragement to make get return @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 @NullnessUnspecified more widely than anticipated, we might want to take that into account while we make decisions about its semantics.

  12. changed the title [-]@NullUnspecified possible use cases[/-] [+]@NullnessUnspecified possible use cases[/+] on Feb 3, 2020
  13. cpovirk commented on Mar 23, 2020

    @cpovirk
    CollaboratorAuthor

    We have a small internal discussion going about Matcher.group(int) and group(String). They're another case in which @Nullable is clearly correct but probably inconvenient in a large fraction of cases. I doubt this is fundamentally different than Map.get, though.

  14. amaembo commented on Mar 24, 2020

    @amaembo
    Collaborator

    Yes, we don't annotate Matcher.group methods 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 from Map.get.

  15. 15 remaining items

  16. changed the title [-]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)[/+] on Jan 31, 2022
  17. modified the milestones: Post-1.0, 1.0 on Sep 14, 2022
  18. kevinb9n commented on Sep 14, 2022

    @kevinb9n
    Collaborator

    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.

  19. added
    design
    and removed on Nov 30, 2022
  20. changed the title [-]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][/+] on Dec 1, 2022
  21. kevinb9n commented on Mar 13, 2024

    @kevinb9n
    Collaborator

    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.

  22. modified the milestones: 1.0, Post-1.0 on Mar 13, 2024
  23. changed the title [-]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)[/+] on Mar 13, 2024
  24. netdpb commented on Apr 9, 2024

    @netdpb
    Collaborator

    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.

  25. added
    needs-decision-post-1.0
    and removed on Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    designneeds-decision-post-1.0nullness

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions