Skip to content

Commit

Permalink
Fix SimplifyExprentsHelper accidentally destroying pattern matches
Browse files Browse the repository at this point in the history
  • Loading branch information
jaskarth committed Jan 10, 2025
1 parent 947c0a4 commit a26d152
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,13 @@ public static boolean simplifyStackVarsStatement(
}
}
} else {
res = simplifyStackVarsExprents(expressions, cl, firstInvocation);
res = simplifyStackVarsExprents(expressions, cl, stat, firstInvocation);
}

return res;
}

private static boolean simplifyStackVarsExprents(List<Exprent> list, StructClass cl, boolean firstInvocation) {
private static boolean simplifyStackVarsExprents(List<Exprent> list, StructClass cl, Statement stat, boolean firstInvocation) {
boolean res = false;

int index = 0;
Expand Down Expand Up @@ -149,7 +149,7 @@ private static boolean simplifyStackVarsExprents(List<Exprent> list, StructClass
}
}

if (isAssignmentReturn(current, next)) {
if (isAssignmentReturn(current, next, stat)) {
list.remove(index);
res = true;
continue;
Expand Down Expand Up @@ -396,7 +396,7 @@ private static int isArrayInitializer(List<Exprent> list, int index) {
* Note that this is transformation will result into java that is less like the original.
* TODO: put this behind a compiler option.
*/
private static boolean isAssignmentReturn(Exprent first, Exprent second) {
private static boolean isAssignmentReturn(Exprent first, Exprent second, Statement stat) {
//If assignment then exit.
if (first instanceof AssignmentExprent
&& second instanceof ExitExprent) {
Expand All @@ -406,12 +406,20 @@ private static boolean isAssignmentReturn(Exprent first, Exprent second) {
if (assignment.getCondType() == null
&& exit.getExitType() == ExitExprent.Type.RETURN
&& exit.getValue() != null
&& assignment.getLeft() instanceof VarExprent
&& exit.getValue() instanceof VarExprent) {
VarExprent assignmentLeft = (VarExprent) assignment.getLeft();
VarExprent exitValue = (VarExprent) exit.getValue();
&& assignment.getLeft() instanceof VarExprent assignmentLeft
&& exit.getValue() instanceof VarExprent exitValue) {
//If the assignment before the return is immediately used in the return, inline it.
if (assignmentLeft.equals(exitValue) && !assignmentLeft.isStack() && !exitValue.isStack()) {
// Avoid doing this transform for potential pattern matches, as they should be processed by the pattern matcher first.
if (stat.getTopParent().mt.getBytecodeVersion().hasIfPatternMatching()
&& stat.getParent() instanceof IfStatement ifst && !ifst.isPatternMatched() && stat.getExprents().indexOf(first) == 0
&& assignment.getRight() instanceof FunctionExprent func && func.getFuncType() == FunctionType.CAST
// Most expensive, do it last
&& ifst.getHeadexprent().getAllExprents(true, false).stream().anyMatch(e -> e instanceof FunctionExprent f && f.getFuncType() == FunctionType.INSTANCEOF)
) {
return false;
}

exit.replaceExprent(exitValue, assignment.getRight());
return true;
}
Expand Down
10 changes: 4 additions & 6 deletions testData/results/pkg/TestPatternMatchingReturn.dec
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package pkg;

public class TestPatternMatchingReturn {
public String getPattern(Object obj) {
if (obj instanceof String) {// 5
return (String)obj;// 6
if (obj instanceof String s) {// 5
return s;// 6
} else {
System.out.println("filler");// 9
return null;// 11
Expand All @@ -30,10 +30,8 @@ class 'pkg/TestPatternMatchingReturn' {
6 4
7 4
8 4
9 5
a 5
b 5
c 5
d 4
e 5
f 5
10 7
11 7
Expand Down

0 comments on commit a26d152

Please sign in to comment.