<div dir="ltr"><div>Thanks, I have reread the spec more carefully, and you are right: A strict interpretation of the spec does require this behavior (and who could fault the compiler to strictly follow the spec). Though I'm guessing this behavior was not the intent of the JLS just happened to be so due to this not being a possibility before Java 5 (since Java - as far as I know - nowhere requires upcasting besides disambiguating method overloads, and I can see no benefit to mandate it here as well).</div><div><br></div><div>Also, thanks for the link to the bug report, at least I know that this is not an unknown issue. Though since the first issue on this was opened in 2007 (JDK-6557960) I won't hold my breath for this to be fixed :)</div><div><br></div><div>Attila</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Maurizio Cimadamore <<a href="mailto:maurizio.cimadamore@oracle.com">maurizio.cimadamore@oracle.com</a>> ezt írta (időpont: 2024. febr. 23., P, 15:25):<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><u></u>

  
  <div>
    <p>Hi,<br>
      as currently specified, the type of the expression used in array
      access must be an array type - see JLS 15.10.3</p>
    <blockquote type="cite">
      <p>The type of the array reference expression
        must be an array type (call it <span>T</span><code>[]</code>, an array whose components are of
        type <span>T</span>), or a compile-time error
        occurs. </p>
    </blockquote>
    <div>There is nothing in the JLS to suggest
      that an array type is anything other than some type T followed by
      array brackets. E.g. in JLS 10.1:</div>
    <div><br>
    </div>
    <div>
      <blockquote type="cite">
        <p>An array type is written as the name of
          an element type followed by some number of empty pairs of
          square brackets <code>[]</code>. The number
          of bracket pairs indicates the depth of array nesting. </p>
      </blockquote>
      So, javac is behaving as specified in the JLS. Note that wildcards
      are a distraction here. The wildcard generates a captured type -
      that is a type variable whose upper bound is an array type.</div>
    <div><br>
    </div>
    <div>The following program:</div>
    <div><br>
    </div>
    <div>```<br>
      class Foo<X extends Object[]> {<br>
          void test(X x) {  Object o = x[0]; }   <br>
      }<br>
    </div>
    <div>```<br>
      <br>
      Exhibits the same behavior - e.g. it fails to compile, because "x"
      is not an array.</div>
    <div><br>
    </div>
    <div>So, the question you are asking is: why
      can't we assume that a type-variable whose upper bound is an array
      type is also an array type?</div>
    <div><br>
    </div>
    <div>This is discussed here:</div>
    <div><br>
    </div>
    <div><a href="https://bugs.openjdk.org/browse/JDK-8013843" target="_blank">https://bugs.openjdk.org/browse/JDK-8013843</a></div>
    <div><br>
    </div>
    <div>My reading is that it's doable, but
      it's work to make sure that nothing else gets affected. E.g. if we
      treat as array anything that has an array supertype, then the
      "null type" might get tangled in the mix (e.g. null[0]). Of course
      this is not a reason for not doing it - but some care is required.
      For-each loops might be affected as well.</div>
    <div><br>
    </div>
    <div>Cheers</div>
    <div>Maurizio<br>
    </div>
    <div><br></div><blockquote type="cite"><div dir="ltr">
      </div>
    </blockquote>
  </div>

</blockquote></div></div>