<div dir="ltr">Hi all,<div><br></div><div>Take the following program:</div><div><br></div><div>````</div><div>import java.lang.annotation.Target;<br>import java.lang.annotation.ElementType;<br>import java.lang.annotation.Retention;<br>import java.lang.annotation.RetentionPolicy;<br><br>public class InstanceOfPatternVariable {<br>    public void doSomething(final Object x) {<br>        if (x instanceof @TA Float) {<br>        }<br>        if (x instanceof @TA Boolean b) {<br>        }<br>    }<br><br>    @Target(ElementType.TYPE_USE)<br>    @Retention(RetentionPolicy.RUNTIME)<br>    @interface TA {}<br>}<br><div>````</div><div><br></div><div>This successfully compiles with javac (anything >=16 should work, I used javac 21.0.1).</div><div><br></div><div>I think there are two issues:</div><div><br></div><div>1) When looking at the bytecode with `javap -v InstanceOfPatternVariable.class` I only see one use of @TA:<br></div><div><br></div><div>      RuntimeVisibleTypeAnnotations:<br>        0: #19(): INSTANCEOF, offset=1<br>          InstanceOfPatternVariable$TA<br></div><div><br></div><div>The JSL 15.20.2 [1] says "An instanceof expression may perform either type comparison or pattern matching."</div><div>The JVMS 4.7.20-B [2] talks about how type annotations on "instanceof expressions" are stored.</div><div>It would therefore seem to me that both occurrences of TA should be stored in the class file.</div><div><br></div><div>2) When looking at the generated ASTs, for the first InstanceOfTree, getType() returns `@TA Float`.</div><div>For the second InstanceOfTree, getType() only returns `Boolean`, without the type annotation attached.</div><div>For the second InstanceOfTree, getPattern() seems to return a BindingPatternTree that contains a VariableTree which has `@TA Boolean` type.</div><div>It would seem more consistent if getType() for both trees returns an annotated type.</div><div><br></div>Thoughts?</div><div><br></div><div>I did not find an existing bug report related to type annotations and binding patterns. Did I miss one?</div><div><br></div><div>Thanks,</div><div>cu, WMD.<br><div><br></div><div>[1] <a href="https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.20.2">https://docs.oracle.com/javase/specs/jls/se21/html/jls-15.html#jls-15.20.2</a><br></div><div>[2] <a href="https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-4.html#jvms-4.7.20-410">https://docs.oracle.com/javase/specs/jvms/se21/html/jvms-4.html#jvms-4.7.20-410</a></div><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><a href="https://ece.uwaterloo.ca/~wdietl/" target="_blank">https://ece.uwaterloo.ca/~wdietl/</a></div></div></div>