Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8044366

4.8: Are indirect supertypes of a raw type erased?

XMLWordPrintable

      JLS 4.8 says the following: "The superclasses (respectively, superinterfaces) of a raw type are the erasures of the superclasses (superinterfaces) of any of the parameterizations of the generic type."

      Example:

      interface Parent<T> { void m(T arg); }
      interface Child extends Parent<String> {}
      interface Grandchild<S> extends Child {}

      Question: is raw Grandchild a subtype of Parent<String>? Is it possible to pass a non-String to the 'm' method of raw Grandchild?

      The ambiguity is in the interpretation of "superclass" and "superinterface". Is it meant to refer to indirect supertypes as well as direct supertypes?

      Arguments for limiting it to direct:
      - Subtyping must be transitive -- raw Grandchild <: Child and Child <: Parent<String> implies raw Grandchild <: Parent<String>.
      - It's safe to not erase -- clearly, the type parameter of Grandchild is irrelevant to a declaration in Parent.
      - Inheritance should be compositional -- the members of a type are derived from its direct supertypes.

      Arguments for including indirect supertypes:
      - Otherwise, a generic interface that directly extends both Child and Parent<String> would, as a raw type, have indirect supertypes Parent<String> _and_ raw Parent. Types should not be two different parameterizations of the same interface (see the declaration-site check in 8.1.5).
      - The pluralization of "superclass" suggests that all indirect superclasses are being considered.

      javac uses the indirect interpretation.

      The conflict could be rendered moot by claiming that non-generic classes have a "raw" type, which passes on the "raw" property to its supertypes. Then the direct erasure would imply an indirect erasure, and subtyping would be sound. This would be fairly invasive, though.

      The problem with multiple parameterizations could be addressed by defining "supertype" and inheritance so that the parameterized supertype of a raw type takes precedence over the raw supertype of a raw type.

            dlsmith Dan Smith
            dlsmith Dan Smith
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: