Skip to content

Commit 8ebd234

Browse files
committed
8312984: javac may crash on a record pattern with too few components
Backport-of: c3860917346fb53ed3d23f11d112b58b8cd8448a
1 parent 6dc4a07 commit 8ebd234

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3480,7 +3480,9 @@ public PatternDescription makePatternDescription(Type selectorType, JCPattern pa
34803480
for (List<JCPattern> it = record.nested;
34813481
it.nonEmpty();
34823482
it = it.tail, i++) {
3483-
nestedDescriptions[i] = makePatternDescription(types.erasure(componentTypes[i]), it.head);
3483+
Type componentType = i < componentTypes.length ? componentTypes[i]
3484+
: syms.errType;
3485+
nestedDescriptions[i] = makePatternDescription(types.erasure(componentType), it.head);
34843486
}
34853487
return new RecordPattern(record.type, componentTypes, nestedDescriptions);
34863488
} else if (pattern instanceof JCAnyPattern) {
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
PatternErrorRecovery.java:12:18: compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.pattern.switch), 20, 21
2+
PatternErrorRecovery.java:17:19: compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.deconstruction.patterns), 20, 21
3+
PatternErrorRecovery.java:17:27: compiler.err.illegal.start.of.type
24
PatternErrorRecovery.java:11:18: compiler.err.pattern.expected
3-
2 errors
5+
4 errors
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* @test /nodynamiccopyright/
3-
* @bug 8268320
3+
* @bug 8268320 8312984
44
* @summary Verify user-friendly errors are reported for ill-formed pattern.
5-
* @compile/fail/ref=PatternErrorRecovery.out -XDrawDiagnostics -XDshould-stop.at=FLOW PatternErrorRecovery.java
5+
* @compile/fail/ref=PatternErrorRecovery.out -XDrawDiagnostics -XDshould-stop.at=FLOW -XDdev PatternErrorRecovery.java
66
* @compile/fail/ref=PatternErrorRecovery-old.out --release 20 -XDrawDiagnostics -XDshould-stop.at=FLOW PatternErrorRecovery.java
77
*/
88
public class PatternErrorRecovery {
@@ -12,4 +12,12 @@ void errorRecoveryNoPattern1(Object o) {
1212
case Object obj: break;
1313
}
1414
}
15+
int errorRecoveryNoPattern2(Object o) {
16+
return switch(o) {
17+
case R(var v, ) -> 1;
18+
default -> -1;
19+
};
20+
}
21+
22+
record R(String x) {}
1523
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
PatternErrorRecovery.java:17:27: compiler.err.illegal.start.of.type
12
PatternErrorRecovery.java:11:18: compiler.err.pattern.expected
2-
1 error
3+
PatternErrorRecovery.java:17:18: compiler.err.incorrect.number.of.nested.patterns: java.lang.String, java.lang.String,<any>
4+
3 errors

0 commit comments

Comments
 (0)