Skip to content

Commit

Permalink
8314578: Non-verifiable code is emitted when two guards declare patte…
Browse files Browse the repository at this point in the history
…rn variables in colon-switch
  • Loading branch information
biboudis committed Sep 8, 2023
1 parent dac1727 commit 6cb25ea
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4635,11 +4635,11 @@ void checkSwitchCaseStructure(List<JCCase> cases) {
if (previousCompletessNormally &&
c.stats.nonEmpty() &&
c.labels.head instanceof JCPatternCaseLabel patternLabel &&
hasBindings(patternLabel.pat)) {
(hasBindings(patternLabel.pat) || hasBindings(c.guard))) {
log.error(c.labels.head.pos(), Errors.FlowsThroughToPattern);
} else if (c.stats.isEmpty() &&
c.labels.head instanceof JCPatternCaseLabel patternLabel &&
hasBindings(patternLabel.pat) &&
(hasBindings(patternLabel.pat) || hasBindings(c.guard)) &&
hasStatements(l.tail)) {
log.error(c.labels.head.pos(), Errors.FlowsThroughFromPattern);
}
Expand All @@ -4648,7 +4648,7 @@ void checkSwitchCaseStructure(List<JCCase> cases) {
}
}

boolean hasBindings(JCPattern p) {
boolean hasBindings(JCTree p) {
boolean[] bindings = new boolean[1];

new TreeScanner() {
Expand Down
34 changes: 34 additions & 0 deletions test/langtools/tools/javac/patterns/T8314578.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @test /nodynamiccopyright/
* @bug 8314578
* @summary Parsing of erroneous patterns succeeds
* @compile/fail/ref=T8314578.out -XDrawDiagnostics T8314578.java
*/

public class T8314578 {
record R1() {}
record R2() {}

static void test(Object o) {
switch (o) {
case R1() when o instanceof String s:
case R2() when o instanceof Integer i:
System.out.println("hello: " + i);
break;
default:
break;
}
}

static void test2(Object o) {
switch (o) {
case R1() when o instanceof String s:
System.out.println("hello: " + s);
case R2() when o instanceof Integer i:
System.out.println("hello: " + i);
break;
default:
break;
}
}
}
4 changes: 4 additions & 0 deletions test/langtools/tools/javac/patterns/T8314578.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
T8314578.java:14:18: compiler.err.flows.through.from.pattern
T8314578.java:15:18: compiler.err.flows.through.to.pattern
T8314578.java:27:18: compiler.err.flows.through.to.pattern
3 errors

0 comments on commit 6cb25ea

Please sign in to comment.