Skip to content

Commit 67ce7af

Browse files
committed
Don't mutate underlying list while traversing arguments in reverse.
1 parent 338c2e8 commit 67ce7af

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/main/java/org/truffleruby/parser/BodyTranslator.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.util.ArrayDeque;
1515
import java.util.ArrayList;
1616
import java.util.Arrays;
17-
import java.util.Collections;
1817
import java.util.Deque;
1918
import java.util.Iterator;
2019
import java.util.List;
@@ -2494,10 +2493,15 @@ private RubyNode block(OpElementAsgnParseNode node, ParseNode writeReceiverToTem
24942493
block.add(writeReceiverToTemp);
24952494
block.add(main);
24962495

2496+
/* prepareAndThen is going to take an argument, and the action that comes after it, and return a node that does
2497+
* both of those things. We start off with ret being the block (our final action) and so the first node we
2498+
* should produce is one that evaluates the last argument, and then the block. The final value of ret should be
2499+
* a node that evaluates the first argument, and then any other arguments, and then the block. So, we must go
2500+
* through the argument list in reverse order. */
24972501
RubyNode ret = block.accept(this);
2498-
Collections.reverse(argValues);
2499-
for (var arg : argValues) {
2500-
ret = arg.prepareAndThen(node.getPosition(), ret);
2502+
var listIterator = argValues.listIterator(argValues.size());
2503+
while (listIterator.hasPrevious()) {
2504+
ret = listIterator.previous().prepareAndThen(node.getPosition(), ret);
25012505
}
25022506
return addNewlineIfNeeded(node, ret);
25032507
}

0 commit comments

Comments
 (0)