Repository navigation
Description
Previously considered: well-formedness of wildcard types
In #31:
For class C<T extends @NotNull Object> we agreed to not require an explicit bound at the instantiation sites, i.e. C<?> and C<? extends Object> both mean C<? extends @NotNull Object>. This is in agreement with the logic of JLS that applies implicit bounds to wildcards (i.e. Foo<? extends Bar> means Foo<? extends Bar&Bounds_of_T> where Foo is declared to be Foo<T extends Bounds_of_T>).
Instantiating types without wildcards
Type variables with @NotNull bounds
To illustrate the question, let's consider the following declarations:
class A<TA extends @NotNull Object> {
TA get();
void set(TA t);
}
By our current spec, this is equivalent to:
class A<TA extends @NotNull Object> {
@NotNull TA get();
void set(@NotNull TA t);
}
Observation: I believe we can't allow any instantiation of type A<...> to have set() that accepts @Nullable, because it is not the contract supported by the implementation. In theory, we could allow get() to return @Nullable, but I'd like to avoid it.
So, a simple rule for such classes would be:
A<@X Y> means A<@NotNull Y> for any nullability annotation @X.
Type variables with @Nullable bounds
Type parameters with nullable bounds get parametric at use sites:
class A<TA extends @Nullable Object> {
TA get();
void set(TA t);
}
Usages TA being parametric means that (in the body of A):
A<TA>.get()can return nullA<TA>.set()may fail when passed a nullTA ta = nullis not safe, becauseTAmay end up being@NotNull String@NotNull Object foo(TA ta) { return ta; }is not safe, becauseTAmay end up being@Nullable String@Nullable T getOrNull()this.set(this.getOrNull())- unsafe to pass@Nullable TforT, becauseTmay end up being@NotNull String
Here, unlike the previous case, the first intuition is that A<@Nullable C> is not the same as A<@NotNull C>, because in the first case get() can return null, and in the second case it can not.
The intuitive substitution rule is:
Parametric usages of T are replaced with the corresponding arguments including their annotations
Examples:
A<@Nullable String> a; a.get().length()- not safe to dereferencea.get()because it's nullableA<@Nullable String> a; a.set(null)- okA<@NotNull String> a; a.get().length()- safe to dereferencea.get()because it's not nullA<@NotNull String> a; a.set(null)- not safe, becauseset()takes@NotNull String- It may seem that the body of
sethas to be null-tolerant (because it must be prepared toTAbeing a nullable type), but it can store the argument in a field whichgetlater returns, so we must require that its argument is not null
- It may seem that the body of
A<@UnknownNulness String> a; a.get().length()- dereference ofa.get()is of unknown safety, unsafe under the strict mode, ok under lenient modeA<@UnknownNulness String> a; a.set(null)- not sure whether we shoudl consider this unsafe under the strict mode, I'd say we shouldn't, and definitely no complaints in the lenient mode
Type variables with @NullnessUnknown bounds
class A<TA extends @NullnessUnknown Object> {
TA get();
void set(TA t);
}
By our current spec, this is equivalent to:
class A<TA extends @NullnessUnknown Object> {
@NullnessUnknown TA get();
void set(@NullnessUnknown TA t);
}
In #45 we discussed the case of substituting for type parameters whose bounds have @NullnessUnknown: the common understanding seems to be that the usages of such type variables we consider implicitly also annotated @NullnessUnknown, so when we substitute anything for them, they do not turn into @NotNull.
So, I'd say this case should be equivalent to the case of extends @NotNull:
A<@X Y> means A<@NullnessUnknown Y> for any nullability annotation @X.
Bottomline: substitution rules
See the old proposal by @abreslav
The corresponding substitution rules are:
- When type variable usage
Tis parametric (i.e. not annotated explicitly, and has@Nullableupper bound)T[T←@A C] :=@A C, forAin{@Nullable, @NotNull, @NullnessUnknown}
- When type variable usage
Tis annotated explicitly or implicitly (trough its bound being@NotNullor@NullnessUnknown)@A T[T←@CA C] :=@A C, forAin{@Nullable, @NotNull, @NullnessUnknown}and any@CA
Activity
abreslav commentedon Aug 2, 2019
@kevin1e100 @wmdietlGC Please have a look at this writeup. I'm still unsure about constructors, will file a separate issue (#58)
wmdietlGC commentedon Aug 3, 2019
Thanks for summarizing this!
The last line in the
Type variables with @Nullable boundssection isA<@Nullable String> a; a.set(null) - not sure whether we shoudl consider this unsafe under the strict mode, I'd say we shouldn't, and definitely no complaints in the lenient mode.Here the type of
ashould beA<UnknownNulness String>, right?I would like to argue that the call to
setshould give an "unchecked" warning in strict mode. We do not know what the nullness of the variable is, so we should raise a warning.We don't know whether it was intended to be nullable or non-null, so we need to forbid setting it to null - the same reason why on
a.get().foo()we need to warn about a possible dereference.Regarding well-formedness of types:
@Nullable: all instantiations are allowed.@NotNull: only@NotNullinstantiations are allowed.@UnknownNullnessgives an unchecked warning,@Nullablegives a warning.@UnknownNullness: only@UnknownNullnessinstantiations are allowed. Both@NotNulland@Nullableinstantiations give unchecked warnings.Enforcing these well-formedness rules will ensure that the types written in a program correspond to the behavior one will see. It seems misleading to have a class with a
@NotNullupper bound and silently allow a@Nullable Stringinstantiation, even though it will always be treated as@NotNull String.abreslav commentedon Aug 3, 2019
Fixed, thanks
Fair point, this will be consistent with the other lenient behaviors that we have. Same for the rules you propose. I keep thinking in the lenient mode most of the time, will take time to integrate the idea of the strict mode :)
stephan-herrmann commentedon Aug 4, 2019
If I understand correctly, then Werner's well-formedness-rules affect several combinations in Andrey's substitution rules: If a type variable has a
@NotNullor@UnknownNullnessbound, then for warning free codeCA == A.Is the intention behind the more general substitution rule to let the type bound win in case of (reported) conflict, so that a generic type can never be used in violation of its own contract? Makes sense to me, just checking if we're on the same page.
kevin1e100 commentedon Aug 5, 2019
I think there are 3 related questions here:
@Nullableshouldn't be allowed when the type parameter is bounded by@NotNull, for instance.My hope is that we can use the definition of "effective nullness specifiers", which are recursively defined for all relevant type usages based on defaulting annotations and type use annotations, for all these things.
Effective specifiers: it's pretty clear to me that if any type usage is explicitly annotated, that needs to be that type usage's effective specifier. Otherwise:
a) type arguments: in the absence of explicit annotations we can use defaulting annotations and the corresponding type parameter's bound's effective specifier. My current thinking is to make sure the resulting effective specifiers are "well-formed" (see next point) in most all cases where the type argument isn't explicitly annotation, as discussed in issue should some type arguments not be affected by surrounding defaults? #46.
b) type variable usages: type variable usages are either "parametric" (if the type parameter bound's effective specifier is
@Nullableor is itself parametric) or have a concrete effective specifier.Well-formedness: Once effective specifiers A for a type argument and B for the bound of the corresponding type parameter have been determined, we can define whether the type argument is "well formed" using rules akin to JLS section 4.5. Presumably we'll at a minimum require that A <: B, but we need to figure out exactly what to do when A or B are "unknown". Anyway this should mean that a parameterized type written
ImmutableList<@Nullable String>is ill-formed, at a minimum, while, as discussed in issue should some type arguments not be affected by surrounding defaults? #46,ImmutableList<String>could be defined to mean the same thing asImmutableList<@NotNull String>irrespective of surrounding defaults.Interpreting type variable usages when a type variable is instantiated with a type argument, given that type argument's effective specifier. The rules defined above in this issue are a plausible proposal for that: specifically we could say that we want to substitute the type argument's effective specifier iff the type variable usage is "parametric", and otherwise use the (concrete) effective specifier of the type variable usage.
kevinb9n commentedon Jun 19, 2021
I think we can close this. I think this discussion did its job and fed into our thought processes when we needed it.
The text is now 2 years stale and hard to see as actionable. In case anything within here isn't solved and we have nothing else tracking it, please file that as an issue.