File tree Expand file tree Collapse file tree
main/java/de/peeeq/wurstscript/intermediatelang/optimizer
test/java/tests/wurstscript/tests Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ;
Original file line number Diff line number Diff 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 ;
You can’t perform that action at this time.
0 commit comments