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

Unchecked conversion is allowed by method type argument bound checking

    Details

      Description

      The following compiles without error:

      <T extends Iterable<String>> void m(T arg) {}

      public void test(Iterable rawIterable) {
        this.<Iterable>m(rawIterable); // expected: error; actual: allowed
      }

      The same occurs if no explicit type argument is provided, because inference concludes T=Iterable:

      <T extends Iterable<String>> void m(T arg) {}

      public void test(Iterable rawIterable) {
        m(rawIterable); // expected: error; actual: allowed
      }

      Per JLS 15.12.2.2 and 15.12.2.3, this is incorrect: a raw type is _convertible to_ a parameterization of that type, but is not a _subtype of_ a parameterization of that type. The specification is clear that subtyping, not some more general conversion, is used to check method invocation bounds. So the method should not be considered applicable.

      Notably, there is an error if such a type argument is provided to a generic class:

      class C<T extends Iterable<String>> {}
      C<Iterable> c1 = null; // expected: error; actual: error

      The bug is present in all version of javac on my system, including 8 b104. (It is also present in Eclipse.)

        Activity

        Hide
        mcimadamore Maurizio Cimadamore added a comment -
        Show
        mcimadamore Maurizio Cimadamore added a comment - Fixed in Valhalla as part of this changeset: http://hg.openjdk.java.net/valhalla/valhalla/langtools/rev/f0d0dd18b996
        Hide
        vromero Vicente Arturo Romero Zaldivar added a comment -
        I have fixed this bug it in the tiered attribution repo, but then this code, extracted from jdk9/dev, fails:

        class Test {
            @SuppressWarnings({"unchecked"})
            Object parseEnumValue(Class<? extends Enum> enumType, String constName) {
                return Enum.valueOf(enumType, constName);
            }
        }
        Show
        vromero Vicente Arturo Romero Zaldivar added a comment - I have fixed this bug it in the tiered attribution repo, but then this code, extracted from jdk9/dev, fails: class Test {     @SuppressWarnings({"unchecked"})     Object parseEnumValue(Class<? extends Enum> enumType, String constName) {         return Enum.valueOf(enumType, constName);     } }

          People

          • Assignee:
            dlsmith Dan Smith
            Reporter:
            dlsmith Dan Smith
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated: