8308727: Compiler should accept final unnamed variables in try-with-resources

Reviewed-by: jlahoda
This commit is contained in:
Aggelos Biboudis 2023-05-25 08:10:02 +00:00 committed by Jan Lahoda
parent 3272e2597a
commit 4500bb7a67
2 changed files with 11 additions and 1 deletions

View File

@ -3843,7 +3843,7 @@ public class JavacParser implements Parser {
if (token.kind == FINAL || token.kind == MONKEYS_AT) {
JCModifiers mods = optFinal(0);
JCExpression t = parseType(true);
return variableDeclaratorRest(token.pos, mods, t, ident(), true, null, true, false, false);
return variableDeclaratorRest(token.pos, mods, t, identOrUnderscore(), true, null, true, false, false);
}
JCExpression t = term(EXPR | TYPE);
if (wasTypeMode() && LAX_IDENTIFIER.test(token.kind)) {

View File

@ -31,6 +31,10 @@
*/
import java.util.Objects;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
public class Unnamed {
public static void main(String[] args) throws Throwable {
@ -89,6 +93,9 @@ public class Unnamed {
} catch (Exception _) {}
}
}
try (final Lock _ = null) { }
try (@Foo Lock _ = null) { }
String[] strs = new String[] { "str1", "str2" };
for (var _ : strs) {
for (var _ : strs) {
@ -290,6 +297,9 @@ public class Unnamed {
public int run(int a, int b);
}
record R(Object o) {}
@Target(ElementType.LOCAL_VARIABLE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Foo { }
sealed abstract class Base permits R1, R2, R3, R4 { }
final class R1 extends Base { }