Skip to content

Annotating type parameters and type variables in context of @DefaultNonNull #90

Closed

Description

@kevin1e100

Trying to split this off issue #45. Note this is different from issue #12 which is about <T>, ie., when type parameters are not annotated.

Leaving the question of defaults aside it seems clear that there is a need to be able to specify/modify the type arguments that can parameterize a given type parameter. In particular, assuming we don't have lower bounds, some type parameters should only allow @NonNull types, and others should allow @NonNull or @Nullable. Handling @NonNull and @Nullable (as type parameter bound or type argument nullability) is largely obvious, seemingly, though there is debate in issue #45 on what to do with type use annotations on type variable usages.

@NullnessUnspecified creates severe complications though:

  • How to handle <@NullnessUnspecified String> type arguments? Should they be allowed?
  • What would it mean to bound a type parameter explicitly with @NullnessUnspecified? Should that even be allowed?

My hope is that issue #89 will point a way forward for this issue, but I may be wrong.

Activity

  1. kevin1e100 commented on Jan 18, 2020

    @kevin1e100
    CollaboratorAuthor

    (this is a bit stream-of-consciousness, but I wanted to write this down before I forget:)

    Here are the possible annotations for type parameters we're talking about here:

    1. @DefaultNonNull class ArrayList<T extends @Nullable Object>: seemingly denotes a list that can hold nullable or guaranteed-non-null elements
    2. @DefaultNonNull class ImmutableList<T extends @NonNull Object>: a list that can only hold non-nullable elements
    3. @DefaultNonNull class MyList<T extends @NullnessUnspecified Object>: unclear exactly what this is meant to say.

    My intuition for why a user would write case 3 is to express that MyList's type parameter should treated similar to a generic class, let's call it class UnannotatedList<T>, that isn't annotated in any way; at the very least the user apparently wants to allow both nullable and non-null type arguments, since otherwise case 2 would've been used.

    In particular, treating it similar to an unannotated generic class seems to apply to methods that refer to the type parameter:

    void add(T elem);
    T get(int index);
    

    In other words, it seems important to treat these methods consistently with issue #45: it shouldn't matter whether a type parameter is explicitly declared as <T extends @NullnessUnspecified XXX>, or implicitly by a surrounding @DefaultNullnessUnspecified, or because there is no explicit surrounding default. That goes back to my intuition that @DefaultNullnessUnspecified is a shorthand for explicitly placing that annotation on types (besides type variables) in the program, and really, since it's the default default, that annotation implicitly is everywhere until we say something else.

    My impression is that the "more nullable" approach advocated in issue #45 doesn't give us this similarity between MyList and UnannotatedList: while UnannotatedList<@NonNull String> interprets get's return type, for instance, as @NullnessUnspecified String, MyList<@NonNull String> seems to have a get method that returns @NonNull String. XxxList<@Nullable String> makes get return @Nullable String in all cases btw, so "more nullable" treats case 3 above almost exactly like case 1, and as far as I can tell, there's no way of getting the same interpretation as UnannotatedList<@NonNull String> without [edited: @NullnessUnspecified] annotations to be placed on get's return type, for instance.

    I think this could be problematic: since @NullnessUnspecified is meant for cases where we need to "exempt" parts of a class from the normal @NonNull/@Nullable then, seemingly, we want to be able to exempt type parameters "somehow" and get back to their treatment in absence of any annotations. But it certainly feels weird to do that: maybe @DefaultNonNull just shouldn't be used then?

    An interesting consideration could therefore be to rule out / forbid case 3. However, in more complex cases with multiple type parameters, for instance, it seems @DefaultNonNull could still be convenient and even appropriate, even if one type parameter doesn't want to play by the rules. Additionally, that seemingly gets us into hot water with generic methods: it seems plausible that one might need to resort to unspecified nullness for a single generic method with unclear or difficult-to-specify handling of its type parameters. After all, that's what the @NullnessUnspecified annotation is for. Maybe then @DefaultNullnessUnspecified should be used for the method, but let's just see if we can just allow @NullnessUnspecified on type parameter bounds. That also has the advantage that we don't need special tool support to prevent this case, which we generally aim for.

  2. kevin1e100 commented on Jan 18, 2020

    @kevin1e100
    CollaboratorAuthor

    Update: I realized to get MyList to be interpreted similar to UnannotatedList with "more nullable", one can annotate all type variable usages in MyList with @UnspecifiedNullness T, so an annotation for that exists. Doesn't seem ideal to have to do that, and this depends on "more nullable" treatment of annotations on type variables, which I'm hoping we can discuss in issue #91.

    (I think there's a similar situation in issue #89, though I'm not sure we have an annotation to resolve it there.)

  3. cpovirk commented on Jan 27, 2020

    @cpovirk
    Collaborator

    I think you have a handle on how I'm approaching this, and I agree that most of the current discussion will fit better on #89 and perhaps #91, so just one thought here:

    What would it mean to bound a type parameter explicitly with @NullnessUnspecified?
    ...
    3. @DefaultNonNull class MyList<T extends @NullnessUnspecified Object>: unclear exactly what this is meant to say.

    My intuition for why a user would write case 3 is to express that MyList's type parameter should treated similar to a generic class, let's call it class UnannotatedList<T>, that isn't annotated in any way; at the very least the user apparently wants to allow both nullable and non-null type arguments, since otherwise case 2 would've been used.

    I'm with you on the first half of this, but I see the second half differently. (In short, I wouldn't assume that the user desired to allow both kinds of type arguments, just as I wouldn't assume that for unannotated code. More on that in a bit.)

    The long-winded background:

    The way that I'm envisioning that users will use @NullnessUnspecified -- or at least the main way, the one I'm keeping most in mind -- is to incrementally and mechanically annotate larger parts of a codebase. So a user might start with:

    [@DefaultNullnessUnspecified]
    interface MyList<T> {
      void add(T t);
    
      T get(int index);
    }

    If annotating the whole class at once is too hard, the user might want to flip the default for future code but preserve existing behavior by mechanically applying the defaults to each existing usage. (Tools might help by automating this, either by blindly annotating all usages or by having the smarts to annotate only those for which they can't prove a more precise nullness.)

    @DefaultNonNull
    interface MyList<T extends @NullnessUnspecified Object> {
      @DefaultNullnessUnspecified
      void add(T t);
    
      @DefaultNullnessUnspecified
      T get(int index);
    }

    And, in this simple case, it would make sense to eliminate @DefaultNullnessUnspecified. Under my model, that requires added @NullnessUnspecified on usages of the type variable; under the current design, @NullnessUnspecified wouldn't be necessary.

    @DefaultNonNull
    interface MyList<T extends @NullnessUnspecified Object> {
      void add([@NullnessUnspecified] T t);
    
      [@NullnessUnspecified] T get(int index);
    }

    Again, this all fits with your idea of treating T extends @NullnessUnspecified Object like a type parameter is an unannotated class.

    But the additional property of unannotated classes that I think should apply is that we don't assume that nullable parameterizations of the class are valid. (At least, I see that as a property of unannotated classes; I'm not sure if that's universal. [edit: See also #76. And, as an update: It seems that there probably is general agreement on this issue.])

    I think our difference in perspective is:

    • You're seeing <T extends @NullnessUnspecified Object> as a way of saying "This parameter has parametric nullness, but I haven't annotated its usages yet."
    • I'm seeing <T extends @NullnessUnspecified Object> as a way of saying "I haven't reviewed this bound yet."

    So I'm with you in seeing #89 as the way forward.

  4. kevin1e100 commented on Jan 28, 2020

    @kevin1e100
    CollaboratorAuthor
  5. kevin1e100 commented on Jan 28, 2020

    @kevin1e100
    CollaboratorAuthor

    But the additional property of unannotated classes that I think should apply is that we don't assume that nullable parameterizations of the class are valid.

    I agree with this, sorry for the confusion. When I said "at the very least the user apparently wants to allow both nullable and non-null type arguments" I should've said something like "the user apparently hasn't ruled out nullable type arguments as an option, but she also doesn't mean to clearly endorse them".

    I do feel like it's "nice" to be able to revert all type variable usages back to "assumed parametric nullness" with a single @NullnessUnspecified on the bound, instead of having to "painstakingly" annotate each type variable usage. But after that there doesn't seem to be a good way to make individual type variable usages parametric again (except with additional annotations like @Parametric T, see issue #89).

    In that model there also seemingly isn't a way to make a single type variable usage behave "as if it wasn't annotated", which however is probably what it should probably do in this case:

    @DefaultNonNull
    interface Map<K extends @Nullable Object, V extends @Nullable Object> {
      @NullnessUnspecified V get(K key);
    }
    

    That is to say, by preferring type use annotations, Map.get's return type will always be @NullnessUnspecified, even for @Nullable value type arguments, which is unfortunate/imprecise compared to what we'd want here (leaving aside whether this is what we "should" do for Map.get, it's something that has been asked for).

  6. kevinb9n commented on Jun 23, 2021

    @kevinb9n
    Collaborator

    It's been an eventful 18 months since the last comment here. Can anyone (@kevin1e100, @cpovirk?) provide the modern (and as brief as possible!) synopsis of what has been solved and what is left to be solved, here?

  7. added
    nullness
    on Jul 2, 2021
  8. cpovirk commented on Jul 14, 2021

    @cpovirk
    Collaborator

    I think the mapping between problems and GitHub issues is many-to-many, as usual... :)

    I'm going to optimistically declare that this particular issue does not serve much purpose until we have more annotations than just @NullMarked and @Nullable: To the extent there is disagreement over what it means to use or not use @Nullable within the scope of @NullMarked, that's better handled by the discussions about those annotations, rather than on a thread that brings @NullnessUnspecified and @NonNull into the mix.

    Maybe we close this and link it from Whether to include a type annotation for "unspecified nullness" (#137) as something to consider if we find ourselves thinking more about that?

  9. added this to the 1.0 milestone on Oct 13, 2021
  10. netdpb commented on Dec 8, 2021

    @netdpb
    Collaborator

    I think we can say that it is not necessary to think about this issue until we decide to move forward with #137. So I'll close this issue for now.

  11. removed this from the 1.0 milestone on Dec 8, 2021
  12. added this to the 0.3 milestone on Dec 2, 2022
  13. added
    design
    on Mar 13, 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

    designnullness

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions