Skip to content

Commit ad994db

Browse files
committed
Preserve tuple calls for component selection
1 parent 1b37043 commit ad994db

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/translation/imtranslation/EliminateTuples.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,10 @@ public void visit(ImTupleSelection ts) {
198198
te.setParent(null);
199199
if (i != ti) {
200200
// if not the thing we want to return, just keep it in statements for side-effects
201-
extractSideEffect(te, stmts);
201+
ImExpr remaining = extractSideEffect(te, stmts);
202+
if (SideEffectAnalyzer.quickcheckHasSideeffects(remaining)) {
203+
stmts.add(remaining);
204+
}
202205
} else { // if it is the part we want to return ...
203206
result = extractSideEffect(te, stmts);
204207
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,28 @@ public void tupleReturningCallsAreCapturedBeforeComparison() throws IOException
169169
assertFalse(compiled.contains("tupleEquals"));
170170
}
171171

172+
@Test
173+
public void selectingLaterTupleComponentStillInvokesProducer() throws IOException {
174+
test().testLua(true).executeProg().lines(
175+
"package Test",
176+
"native testSuccess()",
177+
"tuple pair(int x, int y)",
178+
"class Producer",
179+
" int calls",
180+
" @noinline function produce(int seed) returns pair",
181+
" calls++",
182+
" return pair(seed, seed + calls)",
183+
"init",
184+
" let producer = new Producer()",
185+
" let selected = producer.produce(5).y",
186+
" if selected == 6 and producer.calls == 1",
187+
" testSuccess()"
188+
);
189+
190+
String compiled = compiledLua("selectingLaterTupleComponentStillInvokesProducer");
191+
assertFalse(compiled.contains("tupleCopy"));
192+
}
193+
172194
@Test
173195
public void compiletimeGenericArrayReplayLeavesAreSplit() {
174196
String compiled = compileLuaWithRunArgs(

0 commit comments

Comments
 (0)