diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/GuardedPattern.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/GuardedPattern.java index a6424343045..582bf1e426b 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/GuardedPattern.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/ast/GuardedPattern.java @@ -54,6 +54,7 @@ public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, Fl @Override public void generateCode(BlockScope currentScope, CodeStream codeStream, BranchLabel patternMatchLabel, BranchLabel matchFailLabel) { BranchLabel guardCheckLabel = new BranchLabel(codeStream); + this.primaryPattern.setOuterExpressionType(this.outerExpressionType); this.primaryPattern.generateCode(currentScope, codeStream, guardCheckLabel, matchFailLabel); guardCheckLabel.place(); this.condition.generateOptimizedBoolean(currentScope, codeStream, null, matchFailLabel, true); @@ -100,6 +101,7 @@ public TypeBinding resolveType(BlockScope scope) { if (this.resolvedType != null || this.primaryPattern == null) return this.resolvedType; this.resolvedType = this.primaryPattern.resolveType(scope); + try { scope.resolvingGuardExpression = true; // as guards cannot nest in the same scope, no save & restore called for this.condition.resolveTypeExpectingWithBindings(this.primaryPattern.bindingsWhenTrue(), scope, TypeBinding.BOOLEAN); diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PrimitiveInPatternsTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PrimitiveInPatternsTest.java index e016862438f..6fcfa962a65 100644 --- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PrimitiveInPatternsTest.java +++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/PrimitiveInPatternsTest.java @@ -6802,7 +6802,30 @@ public static void main(String argv[]) { """ }, "true"); + } + public void testGuardedPattern_001() { + runConformTest(new String[] { + "X.java", + """ + public class X { + public static int foo(Integer myInt) { + return switch (myInt) { + case int i when i > 10 -> i; + default -> 0; + }; + } + + public static void main(String argv[]) { + Integer i = 100; + System.out.println(X.foo(i) == i); + } + } + """ + }, + "true"); } // test from spec + + // test from spec public void _testSpec001() { runConformTest(new String[] { "X.java",