Skip to content

Commit 6fed42f

Browse files
Type-use annotations on var lambda parameter should be rejected (#4613)
1 parent b1652c2 commit 6fed42f

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/Annotation.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,9 @@ private static AnnotationTargetAllowed isAnnotationTargetAllowed(Binding recipie
12471247
if ((metaTagBits & TagBits.AnnotationForParameter) != 0) {
12481248
return AnnotationTargetAllowed.YES;
12491249
} else if ((metaTagBits & TagBits.AnnotationForTypeUse) != 0) {
1250-
if (isTypeUseCompatible(localVariableBinding.declaration.type, scope)) {
1250+
if (localVariableBinding.declaration.isVarTyped(scope)) {
1251+
return AnnotationTargetAllowed.NO;
1252+
} else if (isTypeUseCompatible(localVariableBinding.declaration.type, scope)) {
12511253
return AnnotationTargetAllowed.YES;
12521254
} else {
12531255
return AnnotationTargetAllowed.TYPE_ANNOTATION_ON_QUALIFIED_NAME;

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/JEP323VarLambdaParamsTest.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,42 @@ public static void main(String[] args) {
366366
"'var' cannot be used with type arguments\n" +
367367
"----------\n");
368368
}
369+
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/4593
370+
// Type-use annotations on var lambda parameter should be rejected
371+
public void testIssue4593() throws IOException {
372+
runNegativeTest(new String[] {
373+
"X.java",
374+
"""
375+
import java.lang.annotation.Retention;
376+
import java.lang.annotation.Target;
377+
import java.util.function.Function;
378+
379+
import static java.lang.annotation.ElementType.TYPE_USE;
380+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
381+
382+
@Retention(value=RUNTIME)
383+
@Target(value={TYPE_USE})
384+
@interface Anno {
385+
}
386+
public class X {
387+
public static void main(String[] args) {
388+
@Anno var bkah = "hello";
389+
Function<Integer, String> f = (@Anno var val) -> Integer.toHexString(val);
390+
System.out.println(f.apply(10));
391+
}
392+
}
393+
"""
394+
},
395+
"----------\n" +
396+
"1. ERROR in X.java (at line 14)\n" +
397+
" @Anno var bkah = \"hello\";\n" +
398+
" ^^^^^\n" +
399+
"The annotation @Anno is disallowed for this location\n" +
400+
"----------\n" +
401+
"2. ERROR in X.java (at line 15)\n" +
402+
" Function<Integer, String> f = (@Anno var val) -> Integer.toHexString(val);\n" +
403+
" ^^^^^\n" +
404+
"The annotation @Anno is disallowed for this location\n" +
405+
"----------\n");
406+
}
369407
}

0 commit comments

Comments
 (0)