Skip to content

Use type-checking terminology correctly/consistently (subtyping/assignability/etc.) #80

Closed

Description

@stephan-herrmann

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.

Activity

  1. cpovirk commented on Nov 5, 2019

    @cpovirk
    Collaborator

    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?

  2. kevinb9n commented on Nov 5, 2019

    @kevinb9n
    Collaborator

    (no objection here; I only used the term assignability in ignorance of the what the distinction really was.)

  3. kevinb9n commented on Nov 22, 2019

    @kevinb9n
    Collaborator

    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.

  4. stephan-herrmann commented on Nov 23, 2019

    @stephan-herrmann
    Author

    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:

    • ? <= ? (but not ? = ?)
    • List<?> <: List<?> (but not List<?> <= List<?>)
    • List<List<?>> </: List<List<?>>
  5. kevinb9n commented on Nov 26, 2019

    @kevinb9n
    Collaborator

    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

    class Foo<T> {
      List<List<T>> list1();
      List<List<T>> list2();
    }
    ...
    Foo<?> foo = ...;
    

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

  6. stephan-herrmann commented on Nov 26, 2019

    @stephan-herrmann
    Author

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

  7. changed the title [-]Consistency with the Java type system[/-] [+]Use type-checking terminology correctly/consistently (subtyping/assignability/etc.)[/+] on May 28, 2020
  8. kevinb9n commented on May 28, 2020

    @kevinb9n
    Collaborator

    This issue report is very helpful; I'll leave it open until I feel we've got the terms plumbed through our documents correctly.

  9. kevinb9n commented on Jul 19, 2021

    @kevinb9n
    Collaborator

    @cpovirk does it still make sense to have this open?

  10. cpovirk commented on Jul 19, 2021

    @cpovirk
    Collaborator

    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.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions