Skip to content

Commit 4500bb7

Browse files
biboudislahodaj
authored andcommitted
8308727: Compiler should accept final unnamed variables in try-with-resources
Reviewed-by: jlahoda
1 parent 3272e25 commit 4500bb7

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3843,7 +3843,7 @@ protected JCTree resource() {
38433843
if (token.kind == FINAL || token.kind == MONKEYS_AT) {
38443844
JCModifiers mods = optFinal(0);
38453845
JCExpression t = parseType(true);
3846-
return variableDeclaratorRest(token.pos, mods, t, ident(), true, null, true, false, false);
3846+
return variableDeclaratorRest(token.pos, mods, t, identOrUnderscore(), true, null, true, false, false);
38473847
}
38483848
JCExpression t = term(EXPR | TYPE);
38493849
if (wasTypeMode() && LAX_IDENTIFIER.test(token.kind)) {

test/langtools/tools/javac/patterns/Unnamed.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
*/
3232

3333
import java.util.Objects;
34+
import java.lang.annotation.ElementType;
35+
import java.lang.annotation.Retention;
36+
import java.lang.annotation.RetentionPolicy;
37+
import java.lang.annotation.Target;
3438

3539
public class Unnamed {
3640
public static void main(String[] args) throws Throwable {
@@ -89,6 +93,9 @@ private void unnamedTest() {
8993
} catch (Exception _) {}
9094
}
9195
}
96+
try (final Lock _ = null) { }
97+
try (@Foo Lock _ = null) { }
98+
9299
String[] strs = new String[] { "str1", "str2" };
93100
for (var _ : strs) {
94101
for (var _ : strs) {
@@ -290,6 +297,9 @@ interface TwoIntParamsIntRet {
290297
public int run(int a, int b);
291298
}
292299
record R(Object o) {}
300+
@Target(ElementType.LOCAL_VARIABLE)
301+
@Retention(RetentionPolicy.RUNTIME)
302+
public @interface Foo { }
293303

294304
sealed abstract class Base permits R1, R2, R3, R4 { }
295305
final class R1 extends Base { }

0 commit comments

Comments
 (0)