diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/CaseStatement.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/CaseStatement.java index 2edeabb2bb2..43f98c90a54 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/CaseStatement.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/CaseStatement.java @@ -358,8 +358,8 @@ public void generateCode(BlockScope currentScope, CodeStream codeStream) { } patternMatchLabel.place(); } else { - if (this.swich.containsNull) - this.swich.nullProcessed |= true; + if (this.swich.nullCase == this) + this.swich.nullProcessed = true; } codeStream.recordPositionsFrom(pc, this.sourceStart); } 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 217c2b21fba..41c57a40c3e 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 @@ -9587,4 +9587,45 @@ public interface X { "B1 -> B1, B1\n" + "B2 -> B2, B2"); } + + // https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3395 + // [Enhanced Switch] ECG generated code hangs + public void testIssue3395() { + runConformTest( + new String[] { + "X.java", + """ + public class X { + public static void main(String[] args) { + for (String s : new String [] { "World", "Check", "Hello", "Null", "Default" }) { + String sel = s.equals("Null") ? null : s; + switch (sel) { + case "World" -> System.out.print("World"); + case String str when s.equals("Check") -> System.out.print("Check"); + case "Hello" -> System.out.print("Hello"); + case null -> System.out.print("Null"); + default -> System.out.print("Default"); + } + System.out.print("--"); + } + System.out.println(""); + for (String s : new String [] { "Default", "Null", "Hello", "Check", "World" }) { + String sel = s.equals("Null") ? null : s; + switch (sel) { + case "World" -> System.out.print("World"); + case String str when s.equals("Check") -> System.out.print("Check"); + case "Hello" -> System.out.print("Hello"); + case null -> System.out.print("Null"); + default -> System.out.print("Default"); + } + System.out.print("--"); + } + System.out.println(""); + } + } + """, + }, + "World--Check--Hello--Null--Default--\n" + + "Default--Null--Hello--Check--World--"); + } }