Skip to content

Commit 647e39b

Browse files
committed
review fix
1 parent f402860 commit 647e39b

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/intermediatelang/optimizer/DispatchCheckDeduplicator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ private void optimizeStmts(ImStmts stmts) {
9292
}
9393

9494
private boolean invalidatesGuard(ImStmt s, GuardPattern guard) {
95+
if (mayWriteTypeIdFromElement(s, guard.failedCond.typeIdVar)) {
96+
return true;
97+
}
9598
if (s instanceof ImSet) {
9699
ImSet set = (ImSet) s;
97100
ImLExpr left = set.getLeft();
98-
if (mayWriteTypeIdFromElement(set.getRight(), guard.failedCond.typeIdVar)) {
99-
return true;
100-
}
101101
if (left instanceof ImVarAccess) {
102102
ImVar v = ((ImVarAccess) left).getVar();
103103
return v == guard.failedCond.receiverVar || v == guard.failedCond.typeIdVar;

de.peeeq.wurstscript/src/test/java/tests/wurstscript/tests/ClassesTests.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,40 @@ public void dispatchGuardNotDedupedAcrossRhsSideEffects() throws IOException {
170170
"Expected inlopt output to keep separate guards across mutating RHS call, found " + inlOptGuardCount);
171171
}
172172

173+
@Test
174+
public void dispatchGuardNotDedupedAcrossNestedMutatingExprStatement() throws IOException {
175+
testAssertOkLines(false,
176+
"package test",
177+
" native testSuccess()",
178+
" class A",
179+
" function bar() returns int",
180+
" return 1",
181+
"",
182+
" function mutatingRhs(A a) returns int",
183+
" destroy a",
184+
" return 0",
185+
"",
186+
" function useA(A a) returns int",
187+
" int r = 0",
188+
" r += a.bar()",
189+
" int unused = mutatingRhs(a) + 0",
190+
" r += a.bar()",
191+
" return r",
192+
"",
193+
" init",
194+
" let a = new A",
195+
" useA(a)",
196+
" testSuccess()",
197+
"endpackage"
198+
);
199+
200+
File inlOptOut = new File(TEST_OUTPUT_PATH + "ClassesTests_dispatchGuardNotDedupedAcrossNestedMutatingExprStatement_inlopt.j");
201+
String inlOpt = Files.readString(inlOptOut.toPath(), StandardCharsets.UTF_8);
202+
int inlOptGuardCount = countOccurrences(inlOpt, "if A_typeId[a] == 0 then");
203+
assertTrue(inlOptGuardCount >= 2,
204+
"Expected inlopt output to keep separate guards across nested mutating expr statement, found " + inlOptGuardCount);
205+
}
206+
173207
private static int countOccurrences(String text, String needle) {
174208
int c = 0;
175209
int i = 0;

0 commit comments

Comments
 (0)