<div dir="ltr">Hello again!<div><br></div><div>Thanks to Srikanth Sankaran from the Eclipse compiler project, I've got an additional context about this problem.</div><div>Between Java 11 and Java 19, the specification was saying a slightly different thing [1]:</div><div><br></div><div>> If the annotation appears before a void method declaration or a local variable declaration that uses var (§14.4), then there is no closest type.</div><div><br></div><div>Since Java 20, it was changed:</div><div><br></div><div>> If the annotation appears before a void method declaration or a variable declaration that uses var (§14.4, §15.27.1), then there is no closest type. </div><div><br></div><div>This specification problem was reported as JDK-8295807 by Dan Smith [3] and fixed by Gavin Bierman, as the result of the earlier report by Chris Povirk [4].</div><div>However, it looks like the javac implementation was never updated to reflect this specification change. I've created an issue JDK-8371683 in tools/javac [5].</div><div><br></div><div>With best regards,</div><div>Tagir Valeev</div><div><br></div><div>[1] <a href="https://docs.oracle.com/javase/specs/jls/se19/html/jls-9.html#jls-9.7.4">https://docs.oracle.com/javase/specs/jls/se19/html/jls-9.html#jls-9.7.4</a></div><div>[2] <a href="https://docs.oracle.com/javase/specs/jls/se20/html/jls-9.html#jls-9.7.4">https://docs.oracle.com/javase/specs/jls/se20/html/jls-9.html#jls-9.7.4</a></div><div>[3] <a href="https://bugs.openjdk.org/browse/JDK-8295807">https://bugs.openjdk.org/browse/JDK-8295807</a></div><div>[4] <a href="https://mail.openjdk.org/pipermail/compiler-dev/2022-October/020593.html">https://mail.openjdk.org/pipermail/compiler-dev/2022-October/020593.html</a></div><div>[5] <a href="https://bugs.openjdk.org/browse/JDK-8371683">https://bugs.openjdk.org/browse/JDK-8371683</a></div></div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Mon, Nov 3, 2025 at 12:32 PM Tagir Valeev <<a href="mailto:amaembo@gmail.com">amaembo@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hello!<div><br></div><div>I stumbled on the following issue. Let's consider this Java file:</div><div><br></div><div>package com.example;<br><br>import java.lang.annotation.Retention;<br>import java.lang.annotation.Target;<br>import java.util.function.Function;<br><br>import static java.lang.annotation.ElementType.TYPE_USE;<br>import static java.lang.annotation.RetentionPolicy.RUNTIME;<br><br>@Retention(value=RUNTIME)<br>@Target(value={TYPE_USE})<br>@interface Anno {<br>}<br>public class TestClass {<br> public static void main(String[] args) {<br> Function<Integer, String> f = (@Anno var val ) -> Integer.toHexString(val);<br> System.out.println(f.apply(10));<br> }<br>}</div><div><br></div><div>As you can see, there's a TYPE_USE annotation which is applied to the lambda parameter declared with 'var' keyword. If I understand JLS 9.7.4 [1] correctly, this should not be legal:</div><div><br></div><div>> If the annotation appears before a void method declaration or a variable declaration that uses var (§14.4, §15.27.1), then there is no closest type. If the annotation's interface is deemed to apply only to the type which is closest to the annotation, a compile-time error occurs.</div><br><div>However, javac accepts this code. I tried different versions between Java 11 and Java 25, and the result is the same: the file is compiled successfully. However, the TestClass.class file contains no references to the Anno annotation at all, despite it having the RUNTIME retention. So it looks like the annotation is silently ignored.</div><div><br></div><div>One thing that confuses me is that the Eclipse compiler (ecj-4.34.jar) also accepts this code. However, it generates a RuntimeVisibleTypeAnnotations attribute for the Anno annotation. So either both compilers accept this code incorrectly, or my understanding of the specification is wrong. </div><div><br></div><div>Could you please take a look at the javac issue? I'd be happy to be corrected if I misunderstood the specification. Thank you in advance!</div><div><br></div><div>Tagir Valeev</div><div><br></div><div>[1] <a href="https://docs.oracle.com/javase/specs/jls/se25/html/jls-9.html#jls-9.7.4" target="_blank">https://docs.oracle.com/javase/specs/jls/se25/html/jls-9.html#jls-9.7.4</a></div></div>
</blockquote></div>