<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<span style="letter-spacing: normal; font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; font-weight: 400; color: rgb(0, 0, 0);">Thanks for the analysis and report</span>
<div style="text-align: left; margin: 0px;"><span style="letter-spacing: normal; font-family: "Segoe UI Web (West European)", "Segoe UI", -apple-system, "system-ui", Roboto, "Helvetica Neue", sans-serif; font-size: 12px; font-weight: 400; color: rgb(0, 0, 0);"><br>
</span></div>
<div style="text-align: left; margin: 0px;" class="elementToProof"><span style="letter-spacing: normal; font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; font-weight: 400; color: rgb(0, 0, 0);">Opened <a href="https://bugs.openjdk.org/browse/JDK-8320660" id="OWAcec77622-a4c0-2c1d-4991-9785f89c95c5" class="OWAAutoLink" style="margin: 0px;">https://bugs.openjdk.org/browse/JDK-8320660</a></span></div>
<div class="elementToProof" style="font-family: "Segoe UI", "Segoe UI Web (West European)", "Helvetica Neue", sans-serif; font-size: 11pt; color: rgb(0, 0, 0);">
<br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> compiler-dev <compiler-dev-retn@openjdk.org> on behalf of Werner Dietl <wdietl@gmail.com><br>
<b>Sent:</b> 22 November 2023 18:55<br>
<b>To:</b> compiler-dev <compiler-dev@openjdk.java.net><br>
<b>Subject:</b> Type use annotations on instanceof patterns</font>
<div> </div>
</div>
<div>
<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="x_gmail_signature_prefix">-- </span><br>
<div dir="ltr" class="x_gmail_signature" data-smartmail="gmail_signature"><a href="https://ece.uwaterloo.ca/~wdietl/" target="_blank">https://ece.uwaterloo.ca/~wdietl/</a></div>
</div>
</div>
</div>
</body>
</html>