<div dir="ltr"><div>Hi,</div><div><br></div>I was surprised to find that the following code is accepted by javac (as recently as jdk-20-ea+16):<div><br></div><div>import static java.lang.annotation.ElementType.TYPE_USE;<br><br>import java.lang.annotation.Target;<br><br>class X {<br>  C c = (@Anno var o) -> {};<br><br>  interface C {<br>    void c(Object o);<br>  }<br><br>  @Target(TYPE_USE)<br>  @interface Anno {}<br>}<br></div><div><br></div><div>Since type-use annotations are rejected on var local variables, I would have expected them to be rejected on var lambda parameters, too.</div><div><br></div><div>As further support, I found that <a href="https://openjdk.org/jeps/323">https://openjdk.org/jeps/323</a> specifically refers to the "uniformity" it produces between "local variables and lambda formals." Yes, the JEP gives an example of using a @NonNull annotation on a var lambda parameter. But the JEP also uses the annotation on a var local variable, so I think we're meant to conclude that it's a declaration annotation.<br></div><div><br></div><div>I didn't find a clear statement either way in my browsing of the JLS. On the one hand, JLS 9.7.4 specifically forbids using type-use annotations with var only for local variables:</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."<br><br>On the other hand, I don't think any of the JLS rules for finding the "closest type" would apply in the case of a var lambda parameter.</div><div><br></div><div><div>For what it's worth, I haven't found any usages of type-use annotations on var lambda parameters in Google's codebase, with one exception: I found a javac test that uses a TYPE_USE+PARAMETER+LOCAL_VARIABLE annotation and verifies that it doesn't appear as a type-use annotation in the output. (Note that that's contrary to what happens with type-use annotations on <i>non</i>-var lambda parameters, which <a href="https://mail.openjdk.org/pipermail/compiler-dev/2015-December/009866.html">are copied to the generated synthetic method</a>.)</div><div><br></div><div><a href="https://github.com/openjdk/jdk/blob/d362e16924913207b67b5650ad4cafb6ab019cb1/test/langtools/tools/javac/annotations/typeAnnotations/VariablesDeclaredWithVarTest.java#L76">https://github.com/openjdk/jdk/blob/d362e16924913207b67b5650ad4cafb6ab019cb1/test/langtools/tools/javac/annotations/typeAnnotations/VariablesDeclaredWithVarTest.java#L76</a></div></div><div><br></div><div>Based on all that, my best guess is that type-use annotations probably "shouldn't" be accepted on var lambda parameters. I think that would be fine with us, too: The question came up as part of JSpecify nullness discussions, and the main point of discussion has been my surprise that the annotations are accepted today.</div><div><br></div><div>Thanks,</div><div>Chris</div></div>