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

Erasure for unchecked invocation happens after inference

XMLWordPrintable

      The below test exercises the clause in JLS 18.5.2 stating that, where unchecked conversion was necessary for the arguments, the return type of the invocation must be erased.

          <T> T m(Class<T> arg1, Class<T> arg2) { return null; }

          void test(Class c1, Class<Class<String>> c2) throws Exception {
              m(c1, c2).newInstance(); // expected: error; actual: ok
              m(c1, c2).newInstance().length(); // expected: error; actual: error
          }

      The erasure of the return type, T, is Object. However, javac seems to be producing raw Class instead. It appears that javac is inferring T=Class<String>, performing substitution on the return type, and *then* erasing to raw Class. This is inconsistent with the spec, going back to JLS 3 (15.12.2.6), which makes clear that substitution of inference results only occurs in the case in which unchecked conversion was not necessary.

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

              Created:
              Updated: