Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mpalat committed Oct 27, 2023
1 parent 3f530f1 commit 432509d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,8 @@ private void generateCodePatternCaseEpilogue(CodeStream codeStream, int caseInde
Pattern pattern = (Pattern) caseStatement.constantExpressions[caseStatement.patternIndex];
pattern.elseTarget.place();
pattern.suspendVariables(codeStream, this.scope);
if (!pattern.isAlwaysTrue()) {
boolean withinIndex = this.containsNull ? caseIndex < this.constants.length : true;
if (!pattern.isAlwaysTrue() && withinIndex) {
codeStream.loadInt(caseIndex);
codeStream.store(this.restartIndexLocal, false);
codeStream.goto_(this.switchPatternRestartTarget);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class SwitchPatternTest extends AbstractRegressionTest9 {
static {
// TESTS_NUMBERS = new int [] { 40 };
// TESTS_RANGE = new int[] { 1, -1 };
// TESTS_NAMES = new String[] { "testIssueExhaustiveness_005"};
// TESTS_NAMES = new String[] { "testIssue1466"};
}

private static String previewLevel = "21";
Expand Down Expand Up @@ -6907,4 +6907,63 @@ public void testValidCodeWithVeryAmbiguousUsageOfWhen() {
},
"true");
}
public void testIssue1466_01() {
this.runConformTest(
new String[] {
"X.java",
"""
public class X {
private static String foo(Integer i) {
return switch (i) {
case null -> "null";
case Integer value when value > 0 -> value.toString();
default -> i.toString();
};
}
public static void main(String[] args) {
System.out.println(foo(0));
}
}
""",
},
"0");
}
public void testIssue1466_02() {
this.runConformTest(
new String[] {
"X.java",
"""
public class X {
public static void main(String[] args) {
constantLabelMustAppearBeforePatternInteger(-1);
constantLabelMustAppearBeforePatternInteger(0);
constantLabelMustAppearBeforePatternInteger(42);
constantLabelMustAppearBeforePatternInteger(-99);
constantLabelMustAppearBeforePatternInteger(Integer.valueOf(123));
constantLabelMustAppearBeforePatternInteger(null);
}
static String constantLabelMustAppearBeforePatternInteger(Integer i) {
switch (i) {
case null -> System.out.println("value unavailable: " + i);
case -1, 1 -> System.out.println("absolute value 1: " + i);
case Integer value when value > 0 -> System.out.println("positive integer: " + i);
default -> System.out.println("other integer: " + i);
}
return i == null ? "null" : i.toString();
}
}
""",
},
"absolute value 1: -1\n" +
"positive integer: 0\n" +
"positive integer: 42\n" +
"positive integer: -99\n" +
"positive integer: 123\n" +
"value unavailable: null"
);
}
}

0 comments on commit 432509d

Please sign in to comment.