You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Default nullness of type parameter bound in defaulted context #12
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?
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.
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:
classC1<T> // T extends @NotNull ObjectclassC2<TextendsObject> // T extends @NotNull ObjectclassC3<TextendsNumber> // T extends @NotNull NumberclassC4<Textends@NullableObject>
The alternative would be to treat all upper bounds as Nullable, even in a not-null-by-default context.
classC1<T> // T extends @Nullable ObjectclassC2<TextendsObject> // T extends @Nullable ObjectclassC3<TextendsNumber> // T extends @Nullable NumberclassC4<Textends@NotNullObject>
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.
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?
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
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)
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.
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).
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?
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.
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.
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 :))
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.
changed the title [-]Default nullability of generic parameter[/-][+]Default nullness of type parameter bound in non-null-by-default context[/+]on Apr 17, 2020
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-nullableT.
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.
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
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.
Reacted by Sébastien Deleuze, Chris Povirk, David P. Baker, Kevin Bourrillion, Manu Sridharan, Werner Dietl and Lázaro Clapp
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).
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
Activity
kevinb9n commentedon Nov 26, 2018
(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.
kevin1e100 commentedon Dec 20, 2018
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.
wmdietlGC commentedon Jan 10, 2019
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:
The alternative would be to treat all upper bounds as Nullable, even in a not-null-by-default context.
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.
kevinb9n commentedon Jun 12, 2019
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?
abreslav commentedon Jun 16, 2019
Not exactly what has been raised above, but there's another issue related to this comment:
The 'natural' semantics for
Tis not@NotNull T, but "same as the substituted type" (so that if@Nullable Stringis passed, thenTturns into@Nullable String, not@NotNull String), so it's not 100% clear how we should treat them in the context of@DefaultNotNullkevinb9n commentedon Jun 26, 2019
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)
abreslav commentedon Jul 8, 2019
I think we agreed to this, so there's no debate I know of :)
stephan-herrmann commentedon Aug 2, 2019
Eclipse basically puts this decision into the hands of users, see this comment. In particular, the enum constants
TYPE_PARAMETERandTYPE_BOUNDdecide. A plain, argument-less@NonNullByDefaultmakes 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.Objectbounds as irrelevant wrt nullness defaults, so in rare situtations you may still have to writeclass C<T extends @NonNull Object>, despite a default.kevin1e100 commentedon Aug 2, 2019
stephan-herrmann commentedon Aug 2, 2019
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_PARAMETERis included in the default. OmittingTYPE_PARAMETERallows for parametric nullness with no extra verbosity.Additionally,
TYPE_BOUNDalso affects bounds of a wildcard, whereas wildcards themselves are always excluded from the default.Our default-default includes
TYPE_BOUNDbut notTYPE_PARAMETER, and I heard no complaints against this (but I have no data about how users may fine-tune the default).See also DefaultLocation
kevin1e100 commentedon Aug 3, 2019
Got it. I believe the currently proposed semantics of
@DefaultNotNullwould 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.Objector otherwise. That would then require@DefaultNotNull class C<T extends @Nullable Object>to still allow@Nullableinstantiations (or alternatively@DefaultNullable class C<T>).There's several tradeoffs here of course. But it does seem like
@DefaultNotNullwould be "representable" in terms of Eclipse's more configurable system?stephan-herrmann commentedon Aug 4, 2019
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:
We'll have to explain two things:
@DefaultNotNull- can only ever be instantiated with a nonnull type.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
@DefaultNotNullfuture) all collection classes to be re-written to s.t. likeList<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> listwould only ever issue 1 warning against this declaration and furthermore assume thatlistactually has typeList<@NotNull String>(due to the substitution rules in #57).I understand the main intention of
@DefaultNotNullwere 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.9 remaining items
cpovirk commentedon Nov 12, 2019
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>.javapoutput: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.cpovirk commentedon Nov 12, 2019
(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.)
cpovirk commentedon Nov 12, 2019
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 typeFoo."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 expressnew List<? extends @Nullable Object>[], onlynew 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 :))
kevinb9n commentedon Apr 17, 2020
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
[-]Default nullability of generic parameter[/-][+]Default nullness of type parameter bound in non-null-by-default context[/+]kevinb9n commentedon May 8, 2020
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-nullableT.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.
[-]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)[/+]netdpb commentedon Apr 9, 2024
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.)Objectis 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>andclass Foo<T extends Object>are identical in bytecode, making this change would require JSpecify to treat even type parameters explicitly bounded byObjectspecially.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 treatObjecttype usages differently when they appear as a type parameter bound, compared to all their other appearances.Treating
Objecttype parameter bounds specially (compared with other type parameter bounds and other type usages ofObject) 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
Objectis 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.
netdpb commentedon May 8, 2024
Decision: The default nullness of a type parameter bound in the scope of
@NullMarkedis non-nullable. Specifically, the bound ofTin@NullMarked class Foo<T>isObject!(aka@NonNull Object).[-]Default nullness of type parameter bound in defaulted context (working decision: non-nullable)[/-][+]Default nullness of type parameter bound in defaulted context[/+]