<html>
  <head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    <p>Hi Vitaly,<br>
      the situation is a tad convoluted; basically there's a
      long-standing javac discrepancy which allowed javac to erase the
      return type of an unchecked call *after* some type inference has
      been performed. This behavior is well described here:</p>
    <p><a class="moz-txt-link-freetext" href="https://bugs.openjdk.java.net/browse/JDK-8135087">https://bugs.openjdk.java.net/browse/JDK-8135087</a></p>
    <p>In your example, that means that, since the first actual argument
      to 'foo' has type Class<String> and the first formal is
      Class<T>, javac is able to infer that T = String - which
      gives you a fully instantiated 'foo' with signature:</p>
    <p>String foo(Class<String> c, Collection<? super
      String> baz) <br>
    </p>
    <p>Then, as an unchecked conversion has been required for this
      method call (for the second argument), javac proceeds to erase the
      signature - but there's nothing to do! That is, erasure(String) =
      String, so the invocation type of the inner most call has a return
      type 'String'. That's different from what the spec says - as the
      spec wants to erase the *declared signature* of the method if an
      unchecked conversion occurs - in which case the invocation return
      type would be just Object (and a compiler error would be issued).
      This discrepancy will be addressed as part of JDK 10.</p>
    <p>That said, JDK-8078093 accidentally introduced a change in this
      area - in method context the current javac implementation would
      erase the declared signature; while this behavior is compatible
      with what the spec say, as you observed, it's completely
      inconsistent with what javac does in assignment context, and it's
      also inconsistent with almost everything else javac does (hence
      the weird error you get 'expected: String - found: String'). This
      is just a plain bug - the fix for 8078093 was never meant to alter
      behavior of type-checking with unchecked calls and generic
      methods.</p>
    <p>In the short term we plan to restore the original behavior (as
      with JDK 8). In the medium term we plan to get rid of this
      discrepancy for good.</p>
    <p>So, should the program compile? If you follow the spec literally,
      no, it shouldn't. But the aim is to keep stuff like this working,
      until we come up with a more general cleanup in this area.</p>
    <p>I hope this clarifies the issue.</p>
    <p>Meanwhile, I filed this bug:</p>
    <a class="moz-txt-link-freetext" href="https://bugs.openjdk.java.net/browse/JDK-8174249">https://bugs.openjdk.java.net/browse/JDK-8174249</a>
    <p>to keep track of this issue.</p>
    <p>Maurizio<br>
    </p>
    <p><br>
    </p>
    <br>
    <div class="moz-cite-prefix">On 09/02/17 04:09, Vitaly Davidovich
      wrote:<br>
    </div>
    <blockquote
cite="mid:CAHjP37GpvovsQ97JHBsGJ4tt_p3ccp1r0N5o3V-qO1=YYr2uoQ@mail.gmail.com"
      type="cite">
      <div>Hi Maurizio,</div>
      <div><br>
      </div>
      <div>Thanks for the reply.  So you're saying the example using
        assignment context shouldn't compile either? Also, the call to
        'foo' is unchecked in the wildcard argument, but it has a
        concrete type in the first argument.  Are you saying that any
        unchecked param results in return type erasure?</div>
      <div><br>
      </div>
      <div>Thanks</div>
      <div><br>
        <div class="gmail_quote">
          <div>On Wed, Feb 8, 2017 at 8:38 PM Maurizio Cimadamore <<a
              moz-do-not-send="true"
              href="mailto:maurizio.cimadamore@oracle.com">maurizio.cimadamore@oracle.com</a>>
            wrote:<br>
          </div>
          <blockquote class="gmail_quote" style="margin:0 0 0
            .8ex;border-left:1px #ccc solid;padding-left:1ex">
            <div bgcolor="#FFFFFF" text="#000000" class="gmail_msg">
              <p class="gmail_msg">Hi,<br class="gmail_msg">
                it seems like the behavior you observed started in b83 -
                as a result of this:</p>
              <p class="gmail_msg"><a moz-do-not-send="true"
                  class="m_7581732263134941615moz-txt-link-freetext
                  gmail_msg"
                  href="https://bugs.openjdk.java.net/browse/JDK-8078093"
                  target="_blank">https://bugs.openjdk.java.net/browse/JDK-8078093</a></p>
              <p class="gmail_msg">I believe the error is correct
                (although the diagnostic has always been broken since
                b83 in the 'verbose' mode - the non-verbose mode - w/o
                -Xdiags:verbose is fine). That is, the call to 'foo' is
                unchecked, so the return type should be erased. There
                seems to be an inconsistency in how javac handles this
                in method context compared to assignment context, and
                that needs to be looked at.</p>
              <p class="gmail_msg">Thanks</p>
            </div>
            <div bgcolor="#FFFFFF" text="#000000" class="gmail_msg">
              <p class="gmail_msg"><br class="gmail_msg">
                Maurizio<br class="gmail_msg">
              </p>
            </div>
            <div bgcolor="#FFFFFF" text="#000000" class="gmail_msg">
              <p class="gmail_msg"><br class="gmail_msg">
              </p>
              <br class="gmail_msg">
              <div class="m_7581732263134941615moz-cite-prefix
                gmail_msg">On 08/02/17 19:23, Vitaly Davidovich wrote:<br
                  class="gmail_msg">
              </div>
              <blockquote type="cite" class="gmail_msg">
                <div class="gmail_msg">Hi all,
                  <div class="gmail_msg"><br class="gmail_msg">
                  </div>
                  <div class="gmail_msg">Given the following code:</div>
                  <div class="gmail_msg"><br class="gmail_msg">
                  </div>
                  <div class="gmail_msg">
                    <div class="gmail_msg">import java.util.ArrayList;</div>
                    <div class="gmail_msg">import java.util.Collection;</div>
                    <div class="gmail_msg"><br class="gmail_msg">
                    </div>
                    <div class="gmail_msg">public class Foo {</div>
                    <div class="gmail_msg">    static <T> T
                      foo(Class<T> c, Collection<? super T>
                      baz) {</div>
                    <div class="gmail_msg"><span class="m_7581732263134941615gmail-Apple-tab-span gmail_msg" style="white-space:pre-wrap">    </span>return
                      null;</div>
                    <div class="gmail_msg">    }</div>
                    <div class="gmail_msg"><br class="gmail_msg">
                    </div>
                    <div class="gmail_msg">    static void bar(String c)
                      {</div>
                    <div class="gmail_msg"><br class="gmail_msg">
                    </div>
                    <div class="gmail_msg">    }</div>
                    <div class="gmail_msg"><br class="gmail_msg">
                    </div>
                    <div class="gmail_msg">   
                      @SuppressWarnings("unchecked")</div>
                    <div class="gmail_msg">    public static void
                      main(String[] args) {</div>
                    <div class="gmail_msg"><span class="m_7581732263134941615gmail-Apple-tab-span gmail_msg" style="white-space:pre-wrap">    </span>//
                      this works</div>
                    <div class="gmail_msg"><span class="m_7581732263134941615gmail-Apple-tab-span gmail_msg" style="white-space:pre-wrap">    </span>bar(foo(String.class,
                      new ArrayList<String>()));</div>
                    <div class="gmail_msg"><br class="gmail_msg">
                    </div>
                    <div class="gmail_msg"><span class="m_7581732263134941615gmail-Apple-tab-span gmail_msg" style="white-space:pre-wrap">    </span>//
                      this works with a warning</div>
                    <div class="gmail_msg"><span class="m_7581732263134941615gmail-Apple-tab-span gmail_msg" style="white-space:pre-wrap">    </span>String
                      s = foo(String.class, new ArrayList());</div>
                    <div class="gmail_msg"><span class="m_7581732263134941615gmail-Apple-tab-span gmail_msg" style="white-space:pre-wrap">    </span>bar(s);</div>
                    <div class="gmail_msg"><br class="gmail_msg">
                    </div>
                    <div class="gmail_msg"><span class="m_7581732263134941615gmail-Apple-tab-span gmail_msg" style="white-space:pre-wrap">    </span>//
                      this causes an error on JDK9</div>
                    <div class="gmail_msg"><span class="m_7581732263134941615gmail-Apple-tab-span gmail_msg" style="white-space:pre-wrap">    </span>bar(foo(String.class,
                      new ArrayList()));</div>
                    <div class="gmail_msg">    }</div>
                    <div class="gmail_msg">}</div>
                  </div>
                  <div class="gmail_msg"><br class="gmail_msg">
                  </div>
                  <div class="gmail_msg">javac 9-ea (build 9-ea+154)
                    fails with this interesting error (on the last line
                    in main, as the comments there indicate):</div>
                  <div class="gmail_msg"><br class="gmail_msg">
                  </div>
                  <div class="gmail_msg">
                    <div class="gmail_msg">Foo.java:23: error: method
                      bar in class Foo cannot be applied to given types;</div>
                    <div class="gmail_msg">               
                      bar(foo(String.class, new ArrayList()));</div>
                    <div class="gmail_msg">                ^</div>
                    <div class="gmail_msg">  required: String</div>
                    <div class="gmail_msg">  found: String</div>
                    <div class="gmail_msg">  reason: argument mismatch;
                      Object cannot be converted to String</div>
                    <div class="gmail_msg">1 error</div>
                  </div>
                  <div class="gmail_msg"><br class="gmail_msg">
                  </div>
                  <div class="gmail_msg">Java 8 compiles fine, and the
                    other 2 lines compile in the same java 9 build as
                    well.</div>
                  <div class="gmail_msg"><br class="gmail_msg">
                  </div>
                  <div class="gmail_msg">Is this a javac bug or legit?
                    It seems like a compiler error.  At a minimum, the
                    diagnostic output "required String, found String" is
                    confusing.</div>
                  <div class="gmail_msg"><br class="gmail_msg">
                  </div>
                  <div class="gmail_msg">Thanks</div>
                  <div class="gmail_msg"><br class="gmail_msg">
                  </div>
                  <div class="gmail_msg"><br class="gmail_msg">
                  </div>
                </div>
              </blockquote>
              <br class="gmail_msg">
            </div>
          </blockquote>
        </div>
      </div>
      <div dir="ltr">-- <br>
      </div>
      <div data-smartmail="gmail_signature">Sent from my phone</div>
    </blockquote>
    <br>
  </body>
</html>