Skip to content

What happens to @NullnessUnspecified T when T is instantiated with @Nullable Foo? #74

Closed

Description

@cpovirk

(Previously I gave an example predicated on using @NullnessUnspecified for Map.get. But there may be less support for that than I'd thought, and anyway, I think this is worth considering independent of that.)

Any generic type that isn't annotated for nullness is likely to have a method that we consider to return @NullnessUnspecified T. For example:

class Reference<T> {
  T get();
}

It seems pretty clear that, if I write Reference<@Nullable String> (probably receiving a warning), I expect for get() to return a @Nullable String.

But the design proposal currently has the following rule:

If the bound’s effective specifier is @NotNull or @NullnessUnknown, then that is the type variable usage’s effective specifier.

So get() returns a @NullnessUnknown String. That's still pretty good, but I can still imagine that users would feel "cheated" here that we didn't respect the @Nullable annotation.

The purpose of the current rule is, as I understand it, more about @NotNull. We don't want to assume that an unannotated method that returns T is meant to return exactly T as opposed to @Nullable T. (@Nullable T, incidentally, would be the correct return type for the get() method on java.lang.ref.Reference.)

Maybe what we really want is a rule about picking the "most nullable" of the nullnesses that are "in play." The type parameter bound says "unspecified," but the instantiation says "nullable," so we go with nullable.

However, such a rule makes sense only for unspecified nullness, not in general: If the type parameter bound says "nullable" but the instantiation says "not null," then we still want not null. We could have a special case for unspecified nullness (as arguably we already do), but it would feel nice for it to be less of a special case.

This brings me back to an idea I mentioned once before (in the last paragraph of this post): What if, when determining the nullness of a type-variable usage, we didn't look at the bound at all? Then, to make unspecified nullness work the way we want it to again, we treat defaults as also applying to type-variable usages -- but only in combination with the type parameter that the class is instantiated with (by picking the "most nullable").

(This better fits my mental model for parametric nullability in unannotated classes, and it also produces a better (IMO) result for incrementally annotating a type, as discussed in bullet 2 in a post I linked earlier.)

I'm still not sure of the right way to communicate all this. I feel like it wouldn't be significantly more complex than the existing proposal once it's written in definition form. But people would need an intuitive feel for it, too, and I'm not sure I'm getting the intuitive model across.

Activity

  1. cpovirk commented on Oct 17, 2019

    @cpovirk
    CollaboratorAuthor

    Hmm, maybe this should be part of #45?

  2. kevinb9n commented on Oct 17, 2019

    @kevinb9n
    Collaborator

    Any generic type that isn't annotated for nullness is likely to have a method that we consider to return @NullnessUnspecified T. For example:

    class Reference<T> { T get(); }

    It seems pretty clear that, if I write Reference<@Nullable String> (probably receiving a warning), I
    expect for get() to return a @Nullable String.

    So far I agree: you probably do expect that, and you probably should get a warning, and the question of whether you will get what you expect or not after ignoring/suppressing that warning is probably unanswered. (I believe you should, because (a) look, you were warned, and (b) otherwise the rules for interacting with that reference are different depending on the foreign definition of Reference, and we don't like that.)

    But the design proposal currently has the following rule:

    If the bound’s effective specifier is @NotNull or @NullnessUnknown, then that is the type variable usage’s effective specifier.

    So get() returns a @NullnessUnknown String.

    I believe this part of the spec is only talking about type usages of the type variable itself (which can only occur inside the scope of the Reference class). When analyzing that class itself, T would be seen as having unspecified nullness.

    Reference<@Nullable String> is a parameterization of Reference and has substituted away all occurrences of T, so it's our defined substitution rules that will govern what nullness the get() return type has. As argued above I think @Nullable should probably win.

    So let me pause there and ask you whether that changes the rest of what you want to say here.

  3. cpovirk commented on Oct 18, 2019

    @cpovirk
    CollaboratorAuthor

    Thanks, that changes a lot. I will say something different, and I will say it on #45, which is definitely getting at the same thing.

  4. changed the title [-]What happens to `@NullnessUnspecified T` when `T` is instantiated with `@Nullable Foo`?[/-] [+]What happens to `@NullnessUnspecified T` when the class is instantiated / parameterized?[/+] on Jan 30, 2020
  5. changed the title [-]What happens to `@NullnessUnspecified T` when the class is instantiated / parameterized?[/-] [+]What happens to `@NullnessUnspecified T` when `T` is instantiated with `@Nullable Foo`?[/+] on Jan 30, 2020
  6. added this to the 0.3 milestone on Dec 2, 2022
  7. added
    design
    on Mar 16, 2023
  8. added
    nullness
    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