From c3860917346fb53ed3d23f11d112b58b8cd8448a Mon Sep 17 00:00:00 2001 From: Jan Lahoda Date: Thu, 3 Aug 2023 08:37:15 +0000 Subject: [PATCH] 8312984: javac may crash on a record pattern with too few components Reviewed-by: vromero --- .../share/classes/com/sun/tools/javac/comp/Flow.java | 4 +++- .../javac/patterns/PatternErrorRecovery-old.out | 4 +++- .../tools/javac/patterns/PatternErrorRecovery.java | 12 ++++++++++-- .../tools/javac/patterns/PatternErrorRecovery.out | 4 +++- 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java index d897dc3ef24..2fe5183bb11 100644 --- a/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java +++ b/src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java @@ -3480,7 +3480,9 @@ public class Flow { for (List it = record.nested; it.nonEmpty(); it = it.tail, i++) { - nestedDescriptions[i] = makePatternDescription(types.erasure(componentTypes[i]), it.head); + Type componentType = i < componentTypes.length ? componentTypes[i] + : syms.errType; + nestedDescriptions[i] = makePatternDescription(types.erasure(componentType), it.head); } return new RecordPattern(record.type, componentTypes, nestedDescriptions); } else if (pattern instanceof JCAnyPattern) { diff --git a/test/langtools/tools/javac/patterns/PatternErrorRecovery-old.out b/test/langtools/tools/javac/patterns/PatternErrorRecovery-old.out index 6a149913c02..3b8f5d1b115 100644 --- a/test/langtools/tools/javac/patterns/PatternErrorRecovery-old.out +++ b/test/langtools/tools/javac/patterns/PatternErrorRecovery-old.out @@ -1,3 +1,5 @@ PatternErrorRecovery.java:12:18: compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.pattern.switch), 20, 21 +PatternErrorRecovery.java:17:19: compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.deconstruction.patterns), 20, 21 +PatternErrorRecovery.java:17:27: compiler.err.illegal.start.of.type PatternErrorRecovery.java:11:18: compiler.err.pattern.expected -2 errors +4 errors diff --git a/test/langtools/tools/javac/patterns/PatternErrorRecovery.java b/test/langtools/tools/javac/patterns/PatternErrorRecovery.java index 2e73de015b3..d281fcc00a7 100644 --- a/test/langtools/tools/javac/patterns/PatternErrorRecovery.java +++ b/test/langtools/tools/javac/patterns/PatternErrorRecovery.java @@ -1,8 +1,8 @@ /* * @test /nodynamiccopyright/ - * @bug 8268320 + * @bug 8268320 8312984 * @summary Verify user-friendly errors are reported for ill-formed pattern. - * @compile/fail/ref=PatternErrorRecovery.out -XDrawDiagnostics -XDshould-stop.at=FLOW PatternErrorRecovery.java + * @compile/fail/ref=PatternErrorRecovery.out -XDrawDiagnostics -XDshould-stop.at=FLOW -XDdev PatternErrorRecovery.java * @compile/fail/ref=PatternErrorRecovery-old.out --release 20 -XDrawDiagnostics -XDshould-stop.at=FLOW PatternErrorRecovery.java */ public class PatternErrorRecovery { @@ -12,4 +12,12 @@ public class PatternErrorRecovery { case Object obj: break; } } + int errorRecoveryNoPattern2(Object o) { + return switch(o) { + case R(var v, ) -> 1; + default -> -1; + }; + } + + record R(String x) {} } diff --git a/test/langtools/tools/javac/patterns/PatternErrorRecovery.out b/test/langtools/tools/javac/patterns/PatternErrorRecovery.out index 13cc0faf70c..65a20619b1d 100644 --- a/test/langtools/tools/javac/patterns/PatternErrorRecovery.out +++ b/test/langtools/tools/javac/patterns/PatternErrorRecovery.out @@ -1,2 +1,4 @@ +PatternErrorRecovery.java:17:27: compiler.err.illegal.start.of.type PatternErrorRecovery.java:11:18: compiler.err.pattern.expected -1 error +PatternErrorRecovery.java:17:18: compiler.err.incorrect.number.of.nested.patterns: java.lang.String, java.lang.String, +3 errors