Type-use annotation on var lambda parameter

Tagir Valeev amaembo at gmail.com
Wed Nov 12 07:48:05 UTC 2025


Hello again!

Thanks to Srikanth Sankaran from the Eclipse compiler project, I've got an
additional context about this problem.
Between Java 11 and Java 19, the specification was saying a slightly
different thing [1]:

> 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.

Since Java 20, it was changed:

> 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.

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].
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].

With best regards,
Tagir Valeev

[1] https://docs.oracle.com/javase/specs/jls/se19/html/jls-9.html#jls-9.7.4
[2] https://docs.oracle.com/javase/specs/jls/se20/html/jls-9.html#jls-9.7.4
[3] https://bugs.openjdk.org/browse/JDK-8295807
[4] https://mail.openjdk.org/pipermail/compiler-dev/2022-October/020593.html
[5] https://bugs.openjdk.org/browse/JDK-8371683

On Mon, Nov 3, 2025 at 12:32 PM Tagir Valeev <amaembo at gmail.com> wrote:

> Hello!
>
> I stumbled on the following issue. Let's consider this Java file:
>
> package com.example;
>
> import java.lang.annotation.Retention;
> import java.lang.annotation.Target;
> import java.util.function.Function;
>
> import static java.lang.annotation.ElementType.TYPE_USE;
> import static java.lang.annotation.RetentionPolicy.RUNTIME;
>
> @Retention(value=RUNTIME)
> @Target(value={TYPE_USE})
> @interface Anno {
> }
> public class TestClass {
>     public static void main(String[] args) {
>         Function<Integer, String> f = (@Anno var val ) ->
> Integer.toHexString(val);
>         System.out.println(f.apply(10));
>     }
> }
>
> 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:
>
> > 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.
>
> 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.
>
> 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.
>
> 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!
>
> Tagir Valeev
>
> [1]
> https://docs.oracle.com/javase/specs/jls/se25/html/jls-9.html#jls-9.7.4
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20251112/31f6146a/attachment.htm>


More information about the compiler-dev mailing list