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
Use type-checking terminology correctly/consistently (subtyping/assignability/etc.) #80
Some discussions start of from @NotNull X <: @NullnessUnspecified X <: @Nullable X, some without being precise about what the symbol <: stands for: subtyping? assignability? compatibility?
To avoid conflicts with the Java type system as defined in JLS, we should at least be aware of its concepts and rules -- ideally, we would define rules for nullness as a consistent extension of the Java type system.
Type Relations
Subtyping
In JLS the symbol <: denotes subtyping as defined in §4.10. For a top-level type component, subtyping is based strictly on extends and implements.
Assignability
In assignment context (§5.2) various conversions are accepted, i.e., assigment checking is more lenient than subtyping.
JLS does not have a special symbol to denote assignability.
Type argument containment
When checking subtyping between two parameterized types, type arguments are checked for type argument containment ($4.5.1).
This relation is denoted as T1 <= T2.
This relation bears in it the following surprise:
List<?> is a subtype of List<?>, because ? <= ?.
But List<List<?>> is not a subtype of List<List<?>>, because the type arguments List<?> are in a subtyping relation, but not in the required containment relation, i.e., List<?> <= List<?> is false.
Widening / narrowing conversions
While widening is unproblematic, narrowing conversions deserve a closer look:
While the generic type G<T> is a subtype of its raw type G (§4.10.2), the opposite direction marks a narrowing conversion, generally an unchecked narrowing conversion (subject to an "unchecked" warning) (§5.1.6.2).
The similarity between NullnessUnspecified and raw types, suggests that the statement @NullnessUnspecified X <: @Nullable X is wrong (<: read as subtyping).
Application of these rules
Type arguments are checked against type parameter bounds using subtyping (§4.5), not admitting any conversions.
Assignability is used notably for assignments and actual arguments in invocations of methods or constructors.
Type arguments containment (as mentioned) is a pre-requisite for subtyping of parameterized types and used no-where else.
Thanks! Despite some past discussions, I was still missing the concept of type-argument containment. (I think I'd picked up from your previous posts that we care about subtyping when deciding whether an argument is valid, but I hadn't even thought about the fact that we needed specific rules for subtyping between parameterized types.) And I'll probably realize I've missed the significance of other things later....
As a starting point, would anyone object if I replaced all references to "assignability" in the design proposal with "subtyping" or whatever else looks most appropriate there? And hopefully I can make it use <:only to refer to subtyping?
You're certainly correct that subtyping not being reflexive is a surprise. Is that for a reason we can understand and agree with, or is it more like an accident of how the spec came out?
You're certainly correct that subtyping not being reflexive is a surprise.
I don't think this statement is precise. Subtyping is reflexive. If you are referring to "List<List<?>> is not a subtype of List<List<?>>", then the mystery can be resolved by saying that each occurrence of ? introduces a new (unknown) type, i.e. lhs and rhs are different types. The relationship between different occurrences gets weaker with each level of generic wrapping:
Okay that's better than being non-reflexive, but I'm still confused why we don't say the same thing ("each ? introduces a new unknown type") when checking whether List<?> and List<?> are subtypes.
Also, it just seems like we are speaking too imprecisely for it to be useful. After all if I have
... then foo.list1() and foo.list2() each return something of type List<List<?>> but it seems wrong to say that one is not a subtype of the other - they come from the same capture so they darn well should be. [edit: whoops! TIL.]
Perhaps when discussing a wildcard type like Foo<? extends Bar> we should refer to it as Foo<CAP#1 extends Bar> (or CAP#2, etc., just to borrow the syntax javac error messages use for no particular reason). Then perhaps we can reason about those types normally?
Capture conversion is an important concept for determining the type of an expression (see e.g., 3rd bullet in JLS §15.11.1 or last sentence in JLS §15.12.3), and hence affects assignabililty. I don't think it plays a role in subtyping.
As for the case of foo.list1() vs. foo.list2(), I'm afraid each evaluation of the receiver foo will create a fresh new type variable. This should be obvious in the case that foo is reassigned between both invocations (The type system does not make special provisions for final variables).
changed the title [-]Consistency with the Java type system[/-][+]Use type-checking terminology correctly/consistently (subtyping/assignability/etc.)[/+]on May 28, 2020
Activity
cpovirk commentedon Nov 5, 2019
Thanks! Despite some past discussions, I was still missing the concept of type-argument containment. (I think I'd picked up from your previous posts that we care about subtyping when deciding whether an argument is valid, but I hadn't even thought about the fact that we needed specific rules for subtyping between parameterized types.) And I'll probably realize I've missed the significance of other things later....
As a starting point, would anyone object if I replaced all references to "assignability" in the design proposal with "subtyping" or whatever else looks most appropriate there? And hopefully I can make it use
<:only to refer to subtyping?kevinb9n commentedon Nov 5, 2019
(no objection here; I only used the term assignability in ignorance of the what the distinction really was.)
kevinb9n commentedon Nov 22, 2019
You're certainly correct that subtyping not being reflexive is a surprise. Is that for a reason we can understand and agree with, or is it more like an accident of how the spec came out?
The debate about where to put unspec is #33.
stephan-herrmann commentedon Nov 23, 2019
I don't think this statement is precise. Subtyping is reflexive. If you are referring to "
List<List<?>>is not a subtype ofList<List<?>>", then the mystery can be resolved by saying that each occurrence of?introduces a new (unknown) type, i.e. lhs and rhs are different types. The relationship between different occurrences gets weaker with each level of generic wrapping:? <= ?(but not? = ?)List<?> <: List<?>(but notList<?> <= List<?>)List<List<?>> </: List<List<?>>kevinb9n commentedon Nov 26, 2019
Okay that's better than being non-reflexive, but I'm still confused why we don't say the same thing ("each ? introduces a new unknown type") when checking whether
List<?>andList<?>are subtypes.Also, it just seems like we are speaking too imprecisely for it to be useful. After all if I have
... then
foo.list1()andfoo.list2()each return something of typeList<List<?>>but it seems wrong to say that one is not a subtype of the other - they come from the same capture so they darn well should be. [edit: whoops! TIL.]Perhaps when discussing a wildcard type like
Foo<? extends Bar>we should refer to it asFoo<CAP#1 extends Bar>(or CAP#2, etc., just to borrow the syntax javac error messages use for no particular reason). Then perhaps we can reason about those types normally?stephan-herrmann commentedon Nov 26, 2019
Capture conversion is an important concept for determining the type of an expression (see e.g., 3rd bullet in JLS §15.11.1 or last sentence in JLS §15.12.3), and hence affects assignabililty. I don't think it plays a role in subtyping.
As for the case of
foo.list1()vs.foo.list2(), I'm afraid each evaluation of the receiverfoowill create a fresh new type variable. This should be obvious in the case thatfoois reassigned between both invocations (The type system does not make special provisions for final variables).[-]Consistency with the Java type system[/-][+]Use type-checking terminology correctly/consistently (subtyping/assignability/etc.)[/+]kevinb9n commentedon May 28, 2020
This issue report is very helpful; I'll leave it open until I feel we've got the terms plumbed through our documents correctly.
kevinb9n commentedon Jul 19, 2021
@cpovirk does it still make sense to have this open?
cpovirk commentedon Jul 19, 2021
The remaining holes in https://jspecify.dev/spec are, as far as I know, unrelated to terminology :) Maybe this GitHub issue is a reminder to think about what to do with, e.g., https://jspecify.dev/design-overview, but I don't think it's worth keeping open in this form.