Skip to content

Commit

Permalink
8314226: Series of colon-style fallthrough switch cases with guards c…
Browse files Browse the repository at this point in the history
…ompiled incorrectly
  • Loading branch information
biboudis committed Sep 1, 2023
1 parent 145d8bc commit 09db2c7
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ private void handleSwitch(JCTree tree,
int i = 0;
boolean previousCompletesNormally = false;
boolean hasDefault = false;

JCCase previousC = null;
for (var c : cases) {
List<JCCaseLabel> clearedPatterns = c.labels;
boolean hasJoinedNull =
Expand All @@ -541,7 +541,7 @@ private void handleSwitch(JCTree tree,
validCaseLabelList = clearedPatterns.head.hasTag(Tag.PATTERNCASELABEL);
}

if (validCaseLabelList && !previousCompletesNormally) {
if (validCaseLabelList && !previousCompletesNormally || c.guard != null) {
List<JCPatternCaseLabel> labels = clearedPatterns.stream().map(cp -> (JCPatternCaseLabel)cp).collect(List.collector());
bindingContext = new BasicBindingContext();
VarSymbol prevCurrentValue = currentValue;
Expand Down Expand Up @@ -575,7 +575,8 @@ private void handleSwitch(JCTree tree,
}
}

if (c.guard != null) {
boolean hasGuard = c.guard != null;
if (hasGuard) {
test = makeBinary(Tag.AND, accTest, translate(c.guard));
c.guard = null;
} else {
Expand All @@ -585,13 +586,25 @@ private void handleSwitch(JCTree tree,
c.stats = translate(c.stats);
JCContinue continueSwitch = make.at(clearedPatterns.head.pos()).Continue(null);
continueSwitch.target = tree;
c.stats = c.stats.prepend(make.If(makeUnary(Tag.NOT, test).setType(syms.booleanType),
make.Block(0, List.of(make.Exec(make.Assign(make.Ident(index),
makeLit(syms.intType, i + labels.length()))
.setType(syms.intType)),
continueSwitch)),
null));

if (previousC != null && hasGuard && previousCompletesNormally) {
JCExpression or = make.Literal(false);
for (JCPatternCaseLabel label: labels) {
or = makeBinary(Tag.OR, makeTypeTest(make.Ident(temp), make.Type(label.pat.type)), or);
}
test = makeBinary(Tag.AND, or, test);
previousC.stats = previousC.stats.appendList(c.stats); // copying to previous
}

JCIf ifStatement = make.If(makeUnary(Tag.NOT, test).setType(syms.booleanType),
make.Block(0, List.of(make.Exec(make.Assign(make.Ident(index),
makeLit(syms.intType, i + labels.length()))
.setType(syms.intType)),
continueSwitch)),
null);
c.stats = c.stats.prepend(ifStatement);
c.stats = c.stats.prependList(bindingContext.bindingVars(c.pos));
previousC = c;
} finally {
currentValue = prevCurrentValue;
bindingContext.pop();
Expand Down
83 changes: 83 additions & 0 deletions test/langtools/tools/javac/patterns/T8314226.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8314226
* @summary Series of colon-style fallthrough switch cases with guards compiled incorrectly
* @enablePreview
* @compile T8314226.java
* @run main T8314226
*/

public class T8314226 {
int multipleGuardedCases(Object obj) {
switch (obj) {
case Integer _ when ((Integer) obj) > 0:
case String _ when !((String) obj).isEmpty():
return 1;
default:
return -1;
}
}

int multipleGuardedCasesMultiplePatterns(Object obj) {
switch (obj) {
case String _ when !((String) obj).isEmpty():
case Integer _, Byte _ when ((Number) obj).intValue() > 0:
return 1;
default:
return -1;
}
}

int singleGuardedCase(Object obj) {
switch (obj) {
case String _ when !((String) obj).isEmpty():
return 1;
default:
return -1;
}
}

public static void main(String[] args) {
new T8314226().run();
}

private void run() {
assertEquals(1, multipleGuardedCases(42));
assertEquals(1, multipleGuardedCases("test"));
assertEquals(-1, multipleGuardedCases(""));
assertEquals(1, multipleGuardedCasesMultiplePatterns((byte) 42));
assertEquals(1, multipleGuardedCasesMultiplePatterns("test"));
assertEquals(-1, multipleGuardedCasesMultiplePatterns(""));
assertEquals(-1, singleGuardedCase(42));
assertEquals(1, singleGuardedCase("test"));
assertEquals(-1, singleGuardedCase(""));
}

void assertEquals(int expected, int actual) {
if (expected != actual) {
throw new AssertionError("Expected: " + expected + ", but got: " + actual);
}
}
}

0 comments on commit 09db2c7

Please sign in to comment.