Skip to content

Commit 4b63a14

Browse files
committed
We must translate the rescue reference before the rescue statements
1 parent b8103d7 commit 4b63a14

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -414,16 +414,20 @@ public RubyNode visitBeginNode(Nodes.BeginNode node) {
414414
}
415415

416416
private RescueNode translateExceptionNodes(ArrayList<Nodes.Node> exceptionNodes, Nodes.RescueNode rescueClause) {
417-
RubyNode translatedBody = translateNodeOrNil(rescueClause.statements);
418417

419418
final Nodes.Node[] exceptionNodesArray = exceptionNodes.toArray(Nodes.Node.EMPTY_ARRAY);
420419
final RubyNode[] handlingClasses = translate(exceptionNodesArray);
421420

421+
RubyNode translatedBody;
422422
if (rescueClause.reference != null) {
423-
final RubyNode exceptionWriteNode = translateRescueException(
424-
rescueClause.reference);
423+
// We need to translate the reference before the statements,
424+
// because the statements can use the variable defined by the reference.
425+
final RubyNode exceptionWriteNode = translateRescueException(rescueClause.reference);
426+
var translatedStatements = translateNodeOrNil(rescueClause.statements);
425427
translatedBody = sequence(rescueClause,
426-
Arrays.asList(exceptionWriteNode, translatedBody));
428+
Arrays.asList(exceptionWriteNode, translatedStatements));
429+
} else {
430+
translatedBody = translateNodeOrNil(rescueClause.statements);
427431
}
428432

429433
final RescueNode rescueNode = new RescueClassesNode(handlingClasses, translatedBody);

0 commit comments

Comments
 (0)