Skip to content

Type-use annotations on var lambda parameter should be rejected #4593

@amaembo

Description

@amaembo

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, Eclipse compiler accepts this code (I tried different versions 4.20, 4.34 and 4.37). I believe it's a mistake: it should be rejected.

Side note: javac also accepts this code but in a different manner. The ecj compiler generates the RuntimeVisibleTypeAnnotations attribute for @Anno, while javac ignores it completely. I think both compilers are wrong, but I will be happy to know that it's me who is wrong and I understand the specification incorrectly. Thank you in advance.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions