Navigation Menu

Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
8269141: Switch statement containing pattern case label element gets …
Browse files Browse the repository at this point in the history
…in the loop during execution

Reviewed-by: vromero
  • Loading branch information
lahodaj committed Jun 30, 2021
1 parent 6b64a79 commit c3c9189
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
Expand Up @@ -1375,8 +1375,7 @@ private void handleSwitch(JCTree swtch, JCExpression selector, List<JCCase> case

if (switchEnv.info.cont != null) {
Assert.check(patternSwitch);
code.resolve(switchEnv.info.cont);
code.resolve(code.branch(goto_), switchStart);
code.resolve(switchEnv.info.cont, switchStart);
}

// Resolve all breaks.
Expand Down
33 changes: 32 additions & 1 deletion test/langtools/tools/javac/patterns/Switches.java
Expand Up @@ -27,7 +27,7 @@

/*
* @test
* @bug 8262891 8268333
* @bug 8262891 8268333 8268896
* @summary Check behavior of pattern switches.
* @compile --enable-preview -source ${jdk.version} Switches.java
* @run main/othervm --enable-preview Switches
Expand Down Expand Up @@ -60,6 +60,8 @@ void run() {
runEnumTest(this::testIntegerWithGuardsExpression1);
runStringWithConstant(this::testStringWithConstant);
runStringWithConstant(this::testStringWithConstantExpression);
runFallThrough(this::testFallThroughStatement);
runFallThrough(this::testFallThroughExpression);
npeTest(this::npeTestStatement);
npeTest(this::npeTestExpression);
exhaustiveStatementSane("");
Expand Down Expand Up @@ -104,6 +106,10 @@ void runStringWithConstant(Function<String, Integer> mapper) {
assertEquals(-1, mapper.apply(null));
}

void runFallThrough(Function<Integer, Integer> mapper) {
assertEquals(2, mapper.apply(1));
}

void npeTest(Consumer<I> testCase) {
try {
testCase.accept(null);
Expand Down Expand Up @@ -309,6 +315,31 @@ String testIntegerWithGuardsExpression1(E e) {
};
}

Integer testFallThroughStatement(Integer i) {
int r = 0;

switch (i) {
case Integer o && o != null:
r = 1;
default:
r = 2;
}

return r;
}

Integer testFallThroughExpression(Integer i) {
int r = switch (i) {
case Integer o && o != null:
r = 1;
default:
r = 2;
yield r;
};

return r;
}

void npeTestStatement(I i) {
switch (i) {
case A a -> {}
Expand Down

1 comment on commit c3c9189

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.