TYPE_USE annotation on var lambda parameter should be rejected

XMLWordPrintable

    • Type: Bug
    • Resolution: Unresolved
    • Priority: P4
    • 27
    • Affects Version/s: 20, 21, 22, 23, 24, 25
    • Component/s: tools
    • None
    • 11

      Following the specification change in Java 20 (JDK-8295807), the following code should be illegal, as TYPE_USE annotation should not be applicable to variable declaration that uses 'var' (including lambda parameter declaration):

      ```
      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));
          }
      }
      ```

      Nevertheless, javac accepts this code and silently ignores the annotation (it doesn't appear in the generated TestClass.class, despite the RUNTIME retention). It's expected that this code is rejected with a compilation error "annotation interface not applicable to this kind of declaration"

            Assignee:
            Jan Lahoda
            Reporter:
            Tagir Valeev
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: