diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java index 57061d38dd8..e6de3f574b3 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java @@ -4733,10 +4733,13 @@ public class Check { return false; } } - if (currentPattern instanceof JCBindingPattern) { - return existingPattern instanceof JCBindingPattern; + if (currentPattern instanceof JCBindingPattern || + currentPattern instanceof JCAnyPattern) { + return existingPattern instanceof JCBindingPattern || + existingPattern instanceof JCAnyPattern; } else if (currentPattern instanceof JCRecordPattern currentRecordPattern) { - if (existingPattern instanceof JCBindingPattern) { + if (existingPattern instanceof JCBindingPattern || + existingPattern instanceof JCAnyPattern) { return true; } else if (existingPattern instanceof JCRecordPattern existingRecordPattern) { List existingNested = existingRecordPattern.nested; diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java index d12d36e038b..7780321e7a7 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java @@ -879,6 +879,7 @@ public class JavacParser implements Parser { JCExpression e; if (token.kind == UNDERSCORE && parsedType == null) { nextToken(); + checkSourceLevel(Feature.UNNAMED_VARIABLES); pattern = toP(F.at(token.pos).AnyPattern()); } else { diff --git a/test/langtools/tools/javac/T8312163.java b/test/langtools/tools/javac/T8312163.java new file mode 100644 index 00000000000..3554522f401 --- /dev/null +++ b/test/langtools/tools/javac/T8312163.java @@ -0,0 +1,57 @@ +/* + * @test /nodynamiccopyright/ + * @bug 8312163 + * @summary Crash in dominance check when compiling unnamed patterns + * @enablePreview + * @compile/fail/ref=T8312163.out -XDrawDiagnostics T8312163.java + */ +public class T8312163 { + sealed interface A permits B {} + record B() implements A {} + + record Rec(A a, A b) {} + + public void test(Rec r) + { + switch (r) { + case Rec(_, _): break; + case Rec(_, B()): // dominated + } + + switch (r) { + case Rec(_, B()): break; + case Rec(_, _): + } + + switch (r) { + case Rec(_, _): break; + case Rec(_, A a): // dominated + } + + switch (r) { + case Rec(_, A a): break; + case Rec(_, _): // dominated + } + + // duplicated unnamed patterns with unnamed pattern variables for reference + switch (r) { + case Rec(var _, var _): break; + case Rec(var _, B()): // dominated + } + + switch (r) { + case Rec(var _, B()): break; + case Rec(var _, var _): + } + + switch (r) { + case Rec(var _, var _): break; + case Rec(var _, A a): // dominated + } + + switch (r) { + case Rec(var _, A a): break; + case Rec(var _, var _): // dominated + } + } +} diff --git a/test/langtools/tools/javac/T8312163.out b/test/langtools/tools/javac/T8312163.out new file mode 100644 index 00000000000..80ec36552c0 --- /dev/null +++ b/test/langtools/tools/javac/T8312163.out @@ -0,0 +1,9 @@ +T8312163.java:18:18: compiler.err.pattern.dominated +T8312163.java:28:18: compiler.err.pattern.dominated +T8312163.java:33:18: compiler.err.pattern.dominated +T8312163.java:39:18: compiler.err.pattern.dominated +T8312163.java:49:18: compiler.err.pattern.dominated +T8312163.java:54:18: compiler.err.pattern.dominated +- compiler.note.preview.filename: T8312163.java, DEFAULT +- compiler.note.preview.recompile +6 errors \ No newline at end of file