Skip to content

Commit 7bfbb9a

Browse files
committed
8348928: Check for case label validity are misbehaving when binding patterns with unnamed bindings are present
Reviewed-by: asotona, abimpoudis, vromero
1 parent 188c236 commit 7bfbb9a

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4777,7 +4777,7 @@ boolean hasBindings(JCTree p) {
47774777
new TreeScanner() {
47784778
@Override
47794779
public void visitBindingPattern(JCBindingPattern tree) {
4780-
bindings[0] = !tree.var.sym.isUnnamedVariable();
4780+
bindings[0] |= !tree.var.sym.isUnnamedVariable();
47814781
super.visitBindingPattern(tree);
47824782
}
47834783
}.scan(p);

test/langtools/tools/javac/patterns/SwitchErrors.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* @test /nodynamiccopyright/
3-
* @bug 8262891 8269146 8269113
3+
* @bug 8262891 8269146 8269113 8348928
44
* @summary Verify errors related to pattern switches.
55
* @compile/fail/ref=SwitchErrors.out -XDrawDiagnostics -XDshould-stop.at=FLOW SwitchErrors.java
66
*/
@@ -307,4 +307,22 @@ void test8269301bb(Integer i) {
307307
break;
308308
}
309309
}
310+
311+
void testPatternWithoutBindingCantOverridePatternWithBinding8348928a(Object o) {
312+
record R(int i, String s) {}
313+
switch (o) {
314+
case Integer _, R(int x, String _) -> {}
315+
default -> {}
316+
}
317+
}
318+
319+
void testPatternWithoutBindingCantOverridePatternWithBinding8348928b(Object o) {
320+
record R(int i, String s) {}
321+
switch (o) {
322+
case Integer _:
323+
case R(int x, String _):
324+
break;
325+
default:
326+
}
327+
}
310328
}

test/langtools/tools/javac/patterns/SwitchErrors.out

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ SwitchErrors.java:276:49: compiler.err.cant.resolve.location.args: kindname.meth
5757
SwitchErrors.java:278:55: compiler.err.cant.resolve.location.args: kindname.method, length, , , (compiler.misc.location: kindname.class, java.lang.Object, null)
5858
SwitchErrors.java:284:26: compiler.err.pattern.type.cannot.infer
5959
SwitchErrors.java:299:21: compiler.err.invalid.case.label.combination
60+
SwitchErrors.java:314:29: compiler.err.flows.through.from.pattern
61+
SwitchErrors.java:323:18: compiler.err.flows.through.to.pattern
6062
SwitchErrors.java:10:9: compiler.err.not.exhaustive.statement
6163
SwitchErrors.java:16:9: compiler.err.not.exhaustive.statement
6264
SwitchErrors.java:22:9: compiler.err.not.exhaustive.statement
@@ -69,4 +71,4 @@ SwitchErrors.java:98:9: compiler.err.not.exhaustive.statement
6971
SwitchErrors.java:105:9: compiler.err.not.exhaustive.statement
7072
SwitchErrors.java:153:9: compiler.err.not.exhaustive.statement
7173
SwitchErrors.java:226:9: compiler.err.not.exhaustive.statement
72-
71 errors
74+
73 errors

0 commit comments

Comments
 (0)