From 92ebe6d1ff3b1298b3e956ea38f7724106056606 Mon Sep 17 00:00:00 2001 From: Manoj N Palat Date: Wed, 8 Nov 2023 08:28:52 +0530 Subject: [PATCH] [21][pattern switch] IndexOutOfBoundsException regression against preview 4 #1466 --- .../eclipse/jdt/internal/compiler/ast/SwitchStatement.java | 7 +++++-- .../core/tests/compiler/regression/SwitchPatternTest.java | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java index 464c52fd9c0..1499340a60e 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/SwitchStatement.java @@ -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; @@ -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) { diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchPatternTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchPatternTest.java index e26361bc33b..d9d2efe3d44 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchPatternTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/SwitchPatternTest.java @@ -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"; @@ -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" );