Skip to content

Commit

Permalink
Unexpected operand at stack top for unboxed primitive with guarded (#…
Browse files Browse the repository at this point in the history
…2922)

pattern in switch - issue #2914
  • Loading branch information
mpalat authored Sep 6, 2024
1 parent 9706123 commit 84daab0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 84daab0

Please sign in to comment.