Skip to content

Checking and interpreting type instantiations wrt declared bounds #57

Closed

Description

@abreslav

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 null
  • A<TA>.set() may fail when passed a null
  • TA ta = null is not safe, because TA may end up being @NotNull String
  • @NotNull Object foo(TA ta) { return ta; } is not safe, because TA may end up being @Nullable String
  • @Nullable T getOrNull()
    • this.set(this.getOrNull()) - unsafe to pass @Nullable T for T, because T may 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 dereference a.get() because it's nullable
  • A<@Nullable String> a; a.set(null) - ok
  • A<@NotNull String> a; a.get().length() - safe to dereference a.get() because it's not null
  • A<@NotNull String> a; a.set(null) - not safe, because set() takes @NotNull String
    • It may seem that the body of set has to be null-tolerant (because it must be prepared to TA being a nullable type), but it can store the argument in a field which get later returns, so we must require that its argument is not null
  • A<@UnknownNulness String> a; a.get().length() - dereference of a.get() is of unknown safety, unsafe under the strict mode, ok under lenient mode
  • A<@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 T is parametric (i.e. not annotated explicitly, and has @Nullable upper bound)
    • T [T ← @A C] := @A C, for A in {@Nullable, @NotNull, @NullnessUnknown}
  • When type variable usage T is annotated explicitly or implicitly (trough its bound being @NotNull or @NullnessUnknown)
    • @A T [T ← @CA C] := @A C, for A in {@Nullable, @NotNull, @NullnessUnknown} and any @CA

Activity

  1. abreslav commented on Aug 2, 2019

    @abreslav
    Author

    @kevin1e100 @wmdietlGC Please have a look at this writeup. I'm still unsure about constructors, will file a separate issue (#58)

  2. wmdietlGC commented on Aug 3, 2019

    @wmdietlGC
    Collaborator

    Thanks for summarizing this!

    The last line in the Type variables with @Nullable bounds section is
    A<@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 a should be A<UnknownNulness String>, right?

    I would like to argue that the call to set should 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:

    • if the upper bound is @Nullable: all instantiations are allowed.
    • if the upper bound is @NotNull: only @NotNull instantiations are allowed. @UnknownNullness gives an unchecked warning, @Nullable gives a warning.
    • if the upper bound is @UnknownNullness: only @UnknownNullness instantiations are allowed. Both @NotNull and @Nullable instantiations 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 @NotNull upper bound and silently allow a @Nullable String instantiation, even though it will always be treated as @NotNull String.

  3. abreslav commented on Aug 3, 2019

    @abreslav
    Author

    Here the type of a should be A, right?

    Fixed, thanks

    I would like to argue that the call to set should give an "unchecked" warning in strict mode. We do not know what the nullness of the variable is, so we should raise a warning.

    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 :)

  4. stephan-herrmann commented on Aug 4, 2019

    @stephan-herrmann

    If I understand correctly, then Werner's well-formedness-rules affect several combinations in Andrey's substitution rules: If a type variable has a @NotNull or @UnknownNullness bound, then for warning free code CA == 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.

  5. kevin1e100 commented on Aug 5, 2019

    @kevin1e100
    Collaborator

    I think there are 3 related questions here:

    1. what's the "effective nullness specifier" of type arguments and type variables, depending on explicit annotations on the type argument, defaulting annotations, and the effective specifier of the corresponding type parameter? Issue should some type arguments not be affected by surrounding defaults? #46 deals with that in the absence of explicit annotations (though the rules spelled out there differ slightly from the ones here).
    2. Is a given type argument's effective nullness specifier "well-formed" (cf https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.5) given the effective specifier of the corresponding type parameter? @wmdietlGC referred to that when stating that @Nullable shouldn't be allowed when the type parameter is bounded by @NotNull, for instance.
    3. What does that mean for interpreting occurrences of type variables in declarations? I previously filed issue what happens to type variables when @NullnessUnknown (and @NotNull) type parameters are instantiated? #45 around that.

    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.

    1. 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 @Nullable or is itself parametric) or have a concrete effective specifier.

    2. 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 as ImmutableList<@NotNull String> irrespective of surrounding defaults.

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

  6. kevinb9n commented on Jun 19, 2021

    @kevinb9n
    Collaborator

    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.

  7. added
    design
    on Mar 16, 2023
  8. added
    nullness
    on Mar 13, 2024
  9. added this to the 0.3 milestone on Mar 27, 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