Skip to content

Commit

Permalink
[21][pattern switch] IndexOutOfBoundsException regression against pr…
Browse files Browse the repository at this point in the history
…eview 4 #1466
  • Loading branch information
mpalat authored Nov 8, 2023
1 parent 52c1af8 commit 92ebe6d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class SwitchStatement extends Expression {

public boolean containsPatterns;
public boolean containsNull;
private boolean nullProcessed = false;
BranchLabel switchPatternRestartTarget;
/* package */ public Pattern totalPattern;

Expand Down Expand Up @@ -970,14 +971,16 @@ private void generateCodePatternCaseEpilogue(CodeStream codeStream, int caseInde
Pattern pattern = (Pattern) caseStatement.constantExpressions[caseStatement.patternIndex];
pattern.elseTarget.place();
pattern.suspendVariables(codeStream, this.scope);
boolean withinIndex = this.containsNull ? caseIndex < this.constants.length : true;
if (!pattern.isAlwaysTrue() && withinIndex) {
caseIndex = this.nullProcessed ? caseIndex - 1 : caseIndex;
if (!pattern.isAlwaysTrue()) {
codeStream.loadInt(caseIndex);
codeStream.store(this.restartIndexLocal, false);
codeStream.goto_(this.switchPatternRestartTarget);
}
pattern.thenTarget.place();
pattern.resumeVariables(codeStream, this.scope);
} else if (this.containsNull && caseStatement != null) {
this.nullProcessed |= caseStatement.patternIndex == -1;
}
}
private void generateCodeSwitchPatternPrologue(BlockScope currentScope, CodeStream codeStream) {
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[] { "testIssue1466"};
// TESTS_NAMES = new String[] { "testIssue1466_02"};
}

private static String previewLevel = "21";
Expand Down Expand Up @@ -6959,9 +6959,9 @@ static String constantLabelMustAppearBeforePatternInteger(Integer i) {
""",
},
"absolute value 1: -1\n" +
"positive integer: 0\n" +
"other integer: 0\n" +
"positive integer: 42\n" +
"positive integer: -99\n" +
"other integer: -99\n" +
"positive integer: 123\n" +
"value unavailable: null"
);
Expand Down

0 comments on commit 92ebe6d

Please sign in to comment.