Skip to content

Default nullness of type parameter bound in defaulted context #12

Closed

Description

@dzharkov

[update: direct link to doc about this issue]

In the document, it's been said about @DefaultNotNull:

This annotation establishes defaults for unannotated method parameters etc. in the annotated package or class/interface (but not sub-packages or subtypes) and matches CF’s defaults except for declarations of type variables without explicit bounds, which default to non-null in this proposal

Does it mean that generic (type) parameters have not-null bounds independently of the presence of @DefaultNotNull somewhere in the scope or have I just misread the statement?

Activity

  1. kevinb9n commented on Nov 26, 2018

    @kevinb9n
    Collaborator

    (EDIT: There is now a doc covering this issue.)

    I think it does mean that, but I would like to understand the reasoning behind it.

  2. added
    nullness
    on Nov 26, 2018
  3. kevin1e100 commented on Dec 20, 2018

    @kevin1e100
    Collaborator

    I'm sorry for the confusion, I tried to reword this section a bit. There's no implicit default for type parameter bounds or anything else. please take another look and feel free to suggest further wording changes.

  4. wmdietlGC commented on Jan 10, 2019

    @wmdietlGC
    Collaborator

    In a nullable-by-default context, also type parameter bounds are Nullable.
    In a not-null-by-default context, explicit and implicit type parameter bounds are also NotNull.

    This decision stems from our requirement to treat explicit and implicit upper bounds consistent, because we do not want to require adding the defaulted annotation to bytecode. This is different from the Checker Framework behavior, which treats implicit upper bounds as Nullable and explicit upper bounds as NotNull. However, the CF makes the default explicit in bytecode, which we do not want.

    The interpretations would be:

    class C1<T> // T extends @NotNull Object
    class C2<T extends Object> // T extends @NotNull Object
    class C3<T extends Number> // T extends @NotNull Number
    class C4<T extends @Nullable Object>

    The alternative would be to treat all upper bounds as Nullable, even in a not-null-by-default context.

    class C1<T> // T extends @Nullable Object
    class C2<T extends Object> // T extends @Nullable Object
    class C3<T extends Number> // T extends @Nullable Number
    class C4<T extends @NotNull Object>

    This interpretation gives different defaults to types that occur in a type parameter bound relative to other uses, which makes the code harder to read.
    The advantage might be that existing null-friendly code might stay easier to maintain.

    Overall, we felt a consistent NotNull default will be preferential and requiring explicit Nullable annotations on bounds will make the behavior explicit.

  5. kevinb9n commented on Jun 12, 2019

    @kevinb9n
    Collaborator

    I'm a little confused by the discussion here. A typical user will assume that type parameter bounds obey the same default nullability setting as everything else. Are we trying to argue for some different behavior than that?

  6. abreslav commented on Jun 16, 2019

    @abreslav

    Not exactly what has been raised above, but there's another issue related to this comment:

    The 'natural' semantics for T is not @NotNull T, but "same as the substituted type" (so that if @Nullable String is passed, then T turns into @Nullable String, not @NotNull String), so it's not 100% clear how we should treat them in the context of @DefaultNotNull

  7. kevinb9n commented on Jun 26, 2019

    @kevinb9n
    Collaborator

    About the original issue. I think we have been saying (recently) that a type parameter's upper bound (whether <extends BaseType> is explicit in the source or not) should be defaultable (respect the default annotation in effect). Or, is this a debate we should still be having?

    Now to the last comment: (I moved this response to #13)

  8. abreslav commented on Jul 8, 2019

    @abreslav

    a type parameter's upper bound [...] should be defaultable

    I think we agreed to this, so there's no debate I know of :)

  9. stephan-herrmann commented on Aug 2, 2019

    @stephan-herrmann

    Eclipse basically puts this decision into the hands of users, see this comment. In particular, the enum constants TYPE_PARAMETER and TYPE_BOUND decide. A plain, argument-less @NonNullByDefault makes all "relevant" type bounds nonnull, whereas "unbounded" type parameters have unspecified/generic nullness (so as to admit instantiation with nullable and nonnull type arguments).

    To avoid the conflict between explicit & implicit bounds we treat all j.l.Object bounds as irrelevant wrt nullness defaults, so in rare situtations you may still have to write class C<T extends @NonNull Object>, despite a default.

  10. kevin1e100 commented on Aug 2, 2019

    @kevin1e100
    Collaborator
  11. stephan-herrmann commented on Aug 2, 2019

    @stephan-herrmann

    What's the semantic difference of defaulting the TYPE_PARAMETER vs TYPE_BOUND vs both? What do users typically do?

    For a type parameter with an upper bound there is no visible difference. If either constant is included in the default, the type parameter is effectively constrained to nonnull.

    A type parameter with no relevant upper bound is only affected if TYPE_PARAMETER is included in the default. Omitting TYPE_PARAMETER allows for parametric nullness with no extra verbosity.

    Additionally, TYPE_BOUND also affects bounds of a wildcard, whereas wildcards themselves are always excluded from the default.

    Our default-default includes TYPE_BOUND but not TYPE_PARAMETER, and I heard no complaints against this (but I have no data about how users may fine-tune the default).

    See also DefaultLocation

  12. kevin1e100 commented on Aug 3, 2019

    @kevin1e100
    Collaborator

    Got it. I believe the currently proposed semantics of @DefaultNotNull would be closer to @NotNullByDefault({TYPE_PARAMETER, TYPE_BOUND, ...}) then.

    That is, I've hoped to apply any default to all type parameter bounds, j.l.Object or otherwise. That would then require @DefaultNotNull class C<T extends @Nullable Object> to still allow @Nullable instantiations (or alternatively @DefaultNullable class C<T>).

    There's several tradeoffs here of course. But it does seem like @DefaultNotNull would be "representable" in terms of Eclipse's more configurable system?

  13. stephan-herrmann commented on Aug 4, 2019

    @stephan-herrmann

    I believe the currently proposed semantics of @DefaultNotNull would be closer to @NotNullByDefault({TYPE_PARAMETER, TYPE_BOUND, ...}) then.

    Looks like it. I believe that the underlying model is consistent. However, I would like to challenge the proposal, because I think it will need a lot of hand waving to explain the following:

    @DefaultNotNull
    class Foo<T> { ... }
    class Client {
        Foo<@Nullable String> foo; // <= Warning here
    }
    

    We'll have to explain two things:

    • a plain type parameter - within the scope of @DefaultNotNull - can only ever be instantiated with a nonnull type.
    • in order to remove the above constraint, a constraint must be added to the declaration: class Foo<T extends @Nullable Object>

    It's the last item that really worries me.

    Isn't the intention of type parameters to put some choice into the hands of clients? Why should the default be to take away the choice of nullness?

    Of course, implementing a fully generic class may be harder then a class with fixed types. So, an implementer has the choice to make his life easier by putting constraints on the type parameter. Why shouldn't the same rule apply to all properties of the type, i.e., including nullness?

    Furthermore, do we really expect (one day in a bright @DefaultNotNull future) all collection classes to be re-written to s.t. like List<T extends @Nullable Object>? Or is the proposal made in the hope that no more collections with nullable elements will be used anywhere in the body of Java software?

    If I understand correctly, (in that future) declaring List<@Nullable String> list would only ever issue 1 warning against this declaration and furthermore assume that list actually has type List<@NotNull String> (due to the substitution rules in #57).

    I understand the main intention of @DefaultNotNull were to combine elimination of legacy types with a bias towards the good. Please see that an unconstrained type parameter is not a legacy type. There's nothing bad about them, as long as instantiation of these types avoids legacy type arguments.

  14. 9 remaining items

  15. cpovirk commented on Nov 12, 2019

    @cpovirk
    Collaborator

    Something that I hadn't been aware of (but maybe the rest of you were):

    While the bytecode does not distinguish between <T> and <T extends Object>, it does distinguish between <?> and <? extends Object>. javap output:

    java.util.List<?> unbounded;
    ...
    java.util.List<? extends java.lang.Object> boundedByObject;
    

    While this issue is written to be specifically about <T> and <T extends Object>, I figured that the information about <?> and <? extends Object> is still relevant, since we've talked about applying similar rules for the two.

  16. cpovirk commented on Nov 12, 2019

    @cpovirk
    Collaborator

    (Not that it's clear if that's intentional or not: Supposedly "The wildcard ? extends Object is equivalent to the unbounded wildcard ?," with wide-reaching consequences.)

  17. cpovirk commented on Nov 12, 2019

    @cpovirk
    Collaborator

    At a higher level: I have been toying with the "impossible" option that, within the scope of @DefaultNotNull:

    • <T> means "unbounded."
    • <T extends Foo> ought to mean "bounded by the not-null type Foo."

    This is consistent with the Checker Framework and Kotlin. It still isn't consistent with Eclipse -- because, as discussed, it's impossible for a normal Java compiler to apply this rule for the specific case of <T> and <T extends Object>.

    But I'm increasingly considering <T extends Object> to be the special case that deserves special treatment. Under that logic, we would declare it to be a checker error (again, when used within the scope of @DefaultNotNull). We would force users to write something explicit -- presumably <T extends @NotNull Object> if we end up including @NotNull, or something like <@ExtendsObject T> if not.

    The way I got here is kind of weird, and you can see hints of it above: I found in that javac has special treatment for <?>, and I'd like to think we could treat wildcards "similarly to type parameters." For example, Java does not allow users to express new List<? extends @Nullable Object>[], only new List<?>[]. This led me to think that it's important for <?> to really mean "unbounded."

    Now, as also noted above, it's not clear that javac is doing the right thing here. There might still be some practical value in accommodating current versions of javac, but maybe we shouldn't bend over backward for it.

    But I've come to kind of like this option independent of that.

    (Maybe I'll be back in a week with a different option that I like instead :))

  18. kevinb9n commented on Apr 17, 2020

    @kevinb9n
    Collaborator

    I've moved discussion of this issue into an editable document. It's a bit alarming that this one issue fills six pages, but well, at least it's organized/skimmable.

    We invite review, comments, and even edits by all.
    https://docs.google.com/document/d/1mD_wvJLiSvg7YOLcVTqZh303XktqO-S4vzQEgx-Gnts/edit

  19. changed the title [-]Default nullability of generic parameter[/-] [+]Default nullness of type parameter bound in non-null-by-default context[/+] on Apr 17, 2020
  20. kevinb9n commented on May 8, 2020

    @kevinb9n
    Collaborator

    We'll need more scrutiny of this doc before we ever make a final decision, but at this time we are closing issues when we have a "working decision", and I am very confident we've met the bar for that. The implicitly-bounded <T> will have an upper bound of non-nullable T.

    There is no need to stop making/suggesting edits to the document just because this issue is currently closed, and at some point it could even become clear we need to rethink it.

  21. changed the title [-]Default nullness of type parameter bound in non-null-by-default context[/-] [+]Default nullness of type parameter bound in defaulted context (working decision: non-nullable)[/+] on May 8, 2020
  22. added
    design
    on Mar 13, 2024
  23. added this to the 0.3 milestone on Mar 27, 2024
  24. netdpb commented on Apr 9, 2024

    @netdpb
    Collaborator

    Current decision: T's upper bound is non-nullable in @NullMarked class Foo<T> and in @NullMarked class Foo<T extends Object>. (Those are identical in bytecode.) Object is not treated specially when it is a type parameter's upper bound.

    Argument for changing: A bare, apparently unbounded type parameter has always meant effectively unbounded, and with the current decision that's no longer the case. Now to get an actually unbounded type parameter you have to do @NullMarked class Foo<T extends @Nullable Object>. That is awkward and is surprising. It is important to maintain the status quo that a plain type parameter is unbounded. In addition, IDEs like Eclipse have UIs that allow users to augment third-party code with extra annotations. In the case of <T>, there is no visible upper bound to annotate, and users will often want to annotate that upper bound.

    However, if users have assumed that a plain type parameter always means it is unbounded, that has never even included nullness, so it's hard to say that introducing nullness will violate their assumption that those parameters can accomodate nullable types.

    Since class Foo<T> and class Foo<T extends Object> are identical in bytecode, making this change would require JSpecify to treat even type parameters explicitly bounded by Object specially.

    That would mean that unlike any other type parameter bound, <T extends Object> would have a nullable bound; you would have to do <T extends @NonNull Object> to get a non-null bound. That would also make JSpecify treat Object type usages differently when they appear as a type parameter bound, compared to all their other appearances.

    Treating Object type parameter bounds specially (compared with other type parameter bounds and other type usages of Object) means this proposal complicates the JSpecify nullness model a lot, in a way that will affect lots of users. It will have to be carefully documented in the Javadoc, the specification, and other user documentation.

    On the other hand, documenting the fact that plain type parameters are bounded by non-null Object is simpler, especially because it follows from the basic model of nullness.

    The benefits of this special case are outweighed by the complexity costs.

    Timing: This must be decided before version 1.0 of the jar.

    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.

    *Results: This proposal received seven 👍s and no other votes.

  25. netdpb commented on May 8, 2024

    @netdpb
    Collaborator

    Decision: The default nullness of a type parameter bound in the scope of @NullMarked is non-nullable. Specifically, the bound of T in @NullMarked class Foo<T> is Object! (aka @NonNull Object).

  26. changed the title [-]Default nullness of type parameter bound in defaulted context (working decision: non-nullable)[/-] [+]Default nullness of type parameter bound in defaulted context[/+] on May 8, 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