8317300: javac erroneously allows "final" in front of a record pattern

Reviewed-by: jlahoda
This commit is contained in:
Aggelos Biboudis 2023-10-17 12:57:25 +00:00 committed by Jan Lahoda
parent 2bf1863e24
commit 5ca1beb30e
3 changed files with 30 additions and 0 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac/parser
test/langtools/tools/javac/patterns

@ -909,6 +909,7 @@ public class JavacParser implements Parser {
if (mods.annotations.nonEmpty()) {
log.error(mods.annotations.head.pos(), Errors.RecordPatternsAnnotationsNotAllowed);
}
checkNoMods(pos, mods.flags & Flags.FINAL);
new TreeScanner() {
@Override
public void visitAnnotatedType(JCAnnotatedType tree) {

@ -0,0 +1,24 @@
/*
* @test /nodynamiccopyright/
* @bug 8317300
* @summary javac erroneously allows "final" in front of a record pattern
* @compile/fail/ref=T8317300.out -XDrawDiagnostics T8317300.java
*/
public class T8317300 {
record Foo (int x) {}
record Bar (Foo x) {}
void test1(Object obj) {
switch (obj) {
case final Foo(int x) -> {}
default -> {}
}
}
void test2(Object obj) {
switch (obj) {
case Bar(final Foo(int x)) -> {}
default -> {}
}
}
}

@ -0,0 +1,5 @@
T8317300.java:13:18: compiler.err.mod.not.allowed.here: final
T8317300.java:20:22: compiler.err.illegal.start.of.expr
T8317300.java:20:31: compiler.err.expected: token.identifier
T8317300.java:20:37: compiler.err.expected: ';'
4 errors