Skip to content

should some type arguments not be affected by surrounding defaults? #46

Closed

Description

@kevin1e100

Consider a class like Guava's ImmutableList, whose type parameter would presumably be declared as class ImmutableList<T extends @NotNull Object>, since the class is null-hostile. Now in another source file:

class Foo {
@DefaultNullable
ImmutableList<String> names() {...}

@DefaultNullnessUnknown
ImmutableList<String> values() {...}

ImmutableList<@NullnessUnknown String> keys() {...}

@DefaultNullnessUnknown
List<String> modifiable() {...}
}
  • Should we consider names() to return a ImmutableList<@NotNull String> despite the conflicting default? That seems convenient but is non-obvious from looking at Foo alone.
  • Should we expect the @NotNull to be made explicit, since it conflicts with the default? That avoids the non-obvious problem from the last question but would require "spraying" essentially redundant @NotNull annotations.
  • Should we even worry about @DefaultNullable, since we hope it's rarely used? If it's rarely used then maybe requiring explicit @NotNull is just tolerable.

What about values(), which has unknown default? I think things get extra-tricky here, since this will happen in unannotated legacy code that happens to be using ImmutableList, and it seems implausible, therefore, to require explicit @NotNull qualifiers, therefore (at least when there's no default annotation; maybe it's plausible with explicit @DefaultNullnessUnknown but we've so far tried not to distinguish "no default" from @DefaultNullnessUnknown).

It seems desirable to interpret values() to return ImmutableList<@NotNull String> regardless. But does that match the user's intuition in the context of "unknown nullness by default"? Possibly not but on the other hand there seems little harm at least in this example.

One counter-point could be consider an example like keys(). Here the user was really clear that they want to consider the returned ImmutableList's elements to be considered @NullnessUnknown, and it seems problematic to silently "upgrade" that to @NotNull elements. On the other hand we could consider such a use of @NullnessUnknown to be invalid, though that would be the first time we've considered making unknown nullness invalid. In the case of ImmutableList that may be plausible, but I'm not sure we can make cases like keys() invalid in general.

One other point maybe related point is to consider modifiable(), assuming List is declared to allow nullable elements, i.e., interface List<T extends @Nullable Object>. Here we presumably would interpret modifiable() to return effectively List<@NullnessUnknown String>, and given that it seems potentially confusing if values(), having the same annotations otherwise, effectively returns ImmutableList<@NotNull String>. But again, while it feels inconsistent maybe there is just no harm.

Activity

  1. kevin1e100 commented on Jul 7, 2019

    @kevin1e100
    CollaboratorAuthor

    One other thought here is that it may make sense to try and treat type arguments and wildcards (discussed in #31) somewhat consistently. That is, the goal would be for ImmutableList<String>, Immutable<?> and ImmutableList<? extends String> to have comparable meaning under each defaulting annotation. It seems weird, for instance, to have to explicitly annotate ImmutableList<@NotNull String> in the context of @DefaultNullable, but not ImmutableList<? extends String> (because in the latter case the wildcard's bound is intersected with inherited bounds from type parameters per issue #31).

    Assuming we want to keep the approach from #31 for wildcards, that would suggest we'd want to intersect a type argument's "declared" nullability (which may be defined with an explicit annotation on the type argument or through a defaulting annotation) with the corresponding type parameter's bound. Note that can cause the type argument's declared nullability to be ignored. Here's how we would interpret Foo<String> in the context of the various defaulting annotations depending on how Foo<T> is declared following this rule:

    Type argument Foo<T extends @Nullable Object> Foo<T extends @NotNull Object> Foo<T extends @NullnessUnknown Object>
    @DefaultNullable Foo<String> means ... Foo<@Nullable String> Foo<@NotNull String> Foo<@NullnessUnknown String>
    @DefaultNotNull Foo<String> means ... Foo<@NotNull String> Foo<@NotNull String> Foo<@NotNull String>
    @DefaultNullnessUnkown Foo<String> means ... Foo<@NullnessUnknown String> Foo<@NotNull String> Foo<@NullnessUnknown String>

    In the case of an explicit annotation on the type argument, tools could warn if that annotation is ignored b/c of the type parameter's bound. That is, ImmutableList<@Nullable String> and ImmutableList<@NullnessUnknown String> could cause warnings since the explicit annotations are weaker than ImmutableList's type parameter's bound, @NotNull.

    Note that in what's shown above, "unknown nullness" "wins" over @Nullable but not @NotNull. While I've been trying to avoid this asymmetry, I'm coming to think that's "what you want" in most cases: it seemingly makes sense to preserve known non-nullness even in a context where a lot of other things have unknown nullness.

  2. kevinb9n commented on Jul 8, 2019

    @kevinb9n
    Collaborator

    We think this is a case where we can make either decision for now and changing it later a few months from now shouldn't be too damaging.

  3. kevinb9n commented on Jun 19, 2021

    @kevinb9n
    Collaborator

    @kevin1e100 I think the content in here has gone stale, and has become hard to parse in our new many-fewer-annotations world.

    It's likely most of the original concerns at the heart of it probably sort into one of the buckets "came up again", "will come up again", or "no longer applicable".

    But if there are aspects of this discussion that are still relevant I think I would have to count on you to restate them under today's worldview, in new issues. Short of that this will probably just keep getting ignored so I'm going to go ahead and close it. If that feels wrong but you don't have time to do as requested, then just mash that reopen button and I'll do my best.

  4. 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

    nullness

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions