Skip to content

Commit

Permalink
* Guarded patterns are incorrectly held to be unconditional
Browse files Browse the repository at this point in the history
* Fixes #2053
  • Loading branch information
srikanth-sankaran committed Mar 5, 2024
1 parent 7c77997 commit 76cbf02
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, Fl
@Override
public void generateCode(BlockScope currentScope, CodeStream codeStream, BranchLabel trueLabel, BranchLabel falseLabel) {
this.primaryPattern.generateCode(currentScope, codeStream, trueLabel, falseLabel);
Constant cst = this.condition.optimizedBooleanConstant();
this.condition.generateOptimizedBoolean(
currentScope,
codeStream,
trueLabel,
null,
cst == Constant.NotAConstant);
true);
}

@Override
Expand All @@ -73,8 +72,11 @@ public boolean matchFailurePossible() {

@Override
public boolean isAlwaysTrue() {
Constant cst = this.condition.optimizedBooleanConstant();
return cst != Constant.NotAConstant && cst.booleanValue() == true;
if (this.primaryPattern.isAlwaysTrue()) {
Constant cst = this.condition.optimizedBooleanConstant();
return cst != Constant.NotAConstant && cst.booleanValue() == true;
}
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2544,7 +2544,7 @@ public void testIssue1224_5() {
"PASS");
}
public void testIssue1224_6() {
runNegativeTest(new String[] {
runConformTest(new String[] {
"X.java",
"record Record(String s){}\n"
+ "public class X {\n"
Expand All @@ -2563,12 +2563,7 @@ public void testIssue1224_6() {
+ " }\n"
+ "}"
},
"----------\n" +
"1. ERROR in X.java (at line 7)\n" +
" default -> {} }\n" +
" ^^^^^^^\n" +
"Switch case cannot have both unconditional pattern and default label\n" +
"----------\n");
"PASS");
}
public void testIssue1224_7() {
runConformTest(new String[] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7504,5 +7504,76 @@ public static void main(String argv[]) {
},
"OK!");
}
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2053
// ECJ rejects guarded pattern in switch as being dominated by prior cases
public void testIssue2053() throws Exception {
runConformTest(
new String[] {
"X.java",
"""
interface I {}
record R (I i, I j) {}
class A implements I {}
class B implements I {}
public class X {
static int swtch(Object o) {
return switch (o) {
case R(A a1, A a2) when true -> 1;
case R(B b1, B b2) when o != null -> 2;
case Object obj -> 3;
};
}
public static void main(String argv[]) {
Object o = new R(new A(), new A());
System.out.print(swtch(o));
o = new R(new B(), new B());
System.out.print(swtch(o));
o = new R(new I() {}, new I() {});
System.out.println(swtch(o));
}
}
"""
},
"123");
}
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/2053
// ECJ rejects guarded pattern in switch as being dominated by prior cases
public void testIssue2053_2() throws Exception {
runConformTest(
new String[] {
"X.java",
"""
interface I {}
record R (I i, I j) {}
class A implements I {}
class B implements I {}
public class X {
static int swtch(Object o) {
return switch (o) {
case R(A a1, A a2) when true -> 1;
case R(B b1, B b2) when o == null -> 2;
case Object obj -> 3;
};
}
public static void main(String argv[]) {
Object o = new R(new A(), new A());
System.out.print(swtch(o));
o = new R(new B(), new B());
System.out.print(swtch(o));
o = new R(new I() {}, new I() {});
System.out.println(swtch(o));
}
}
"""
},
"133");
}
}

0 comments on commit 76cbf02

Please sign in to comment.