Skip to content

Commit cd47fe8

Browse files
committed
Translate nodes - return
1 parent deddcfb commit cd47fe8

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

spec/tags/truffle/parsing/parsing_tags.txt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,6 @@ fails:Parsing a Complex number (Complex literal `bri` (without real part) where
5959
fails:Parsing a Complex number (Complex literal in format of a + bi is represented as `a + Complex.convert(0, b)`) case is parsed correctly
6060
fails:Parsing a Def (Name / Non singleton / in an anonymous module) case is parsed correctly
6161
fails:Parsing a Def (Name / Singleton / in an anonymous module) case is parsed correctly
62-
fails:Parsing a Def (Tail expression / with explicit return inside then branch of the `if` operator) case is parsed correctly
63-
fails:Parsing a Def (Tail expression / with explicit return inside then/else branches of the `if` operator) case is parsed correctly
64-
fails:Parsing a Def (Tail expression / with explicit return at the end of a sequence of multiple expressions) case is parsed correctly
65-
fails:Parsing a Def (Tail expression / with explicit return) case is parsed correctly
66-
fails:Parsing a Def (Tail expression / with explicit return inside then branch of the `unless` operator) case is parsed correctly
67-
fails:Parsing a Def (Singleton method definition) case is parsed correctly
68-
fails:Parsing a Def (Method definition with empty body) case is parsed correctly
69-
fails:Parsing a Def (Method definition with not empty body) case is parsed correctly
70-
fails:Parsing a Def (Method definition without parameters) case is parsed correctly
7162
fails:Parsing a defined? (with yield in a method body (defined? yield)) case is parsed correctly
7263
fails:Parsing a Ensure keyword (ensure in a do/end block) case is parsed correctly
7364
fails:Parsing a Ensure keyword (ensure in a method) case is parsed correctly
@@ -153,7 +144,6 @@ fails:Parsing a Rescue keyword (modifier / backtrace optimization / enabled / wh
153144
fails:Parsing a Rescue keyword (with exception and variable) case is parsed correctly
154145
fails:Parsing a Rescue keyword (with multiple rescue branches) case is parsed correctly
155146
fails:Parsing a Rescue keyword (without exception but with a variable) case is parsed correctly
156-
fails:Parsing a Return (return operator at the top level) case is parsed correctly
157147
fails:Parsing a Return (return operator in a block (with a value)) case is parsed correctly
158148
fails:Parsing a Return (return operator in a block in a class definition body) case is parsed correctly
159149
fails:Parsing a Return (return operator in a block in a module definition body) case is parsed correctly

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@
5858
import org.truffleruby.language.constants.WriteConstantNode;
5959
import org.truffleruby.language.control.AndNodeGen;
6060
import org.truffleruby.language.control.BreakNode;
61+
import org.truffleruby.language.control.DynamicReturnNode;
6162
import org.truffleruby.language.control.IfElseNode;
6263
import org.truffleruby.language.control.IfElseNodeGen;
6364
import org.truffleruby.language.control.IfNodeGen;
65+
import org.truffleruby.language.control.InvalidReturnNode;
66+
import org.truffleruby.language.control.LocalReturnNode;
6467
import org.truffleruby.language.control.NextNode;
6568
import org.truffleruby.language.control.NotNodeGen;
6669
import org.truffleruby.language.control.OrNodeGen;
@@ -1557,7 +1560,25 @@ public RubyNode visitRetryNode(Nodes.RetryNode node) {
15571560
}
15581561

15591562
public RubyNode visitReturnNode(Nodes.ReturnNode node) {
1560-
return defaultVisit(node);
1563+
final RubyNode rubyNode;
1564+
final RubyNode argumentsNode = translateControlFlowArguments(node.arguments);
1565+
1566+
if (environment.isBlock()) {
1567+
// Lambda behaves a bit differently from block and "return" to a class/module body is correct -
1568+
// so DynamicReturnNode should be used instead of InvalidReturnNode.
1569+
// It's handled later and InvalidReturnNode is replaced with DynamicReturnNode in YARPBlockTranslator.
1570+
final ReturnID returnID = environment.getReturnID();
1571+
if (returnID == ReturnID.MODULE_BODY) {
1572+
rubyNode = new InvalidReturnNode(argumentsNode);
1573+
} else {
1574+
rubyNode = new DynamicReturnNode(returnID, argumentsNode);
1575+
}
1576+
} else {
1577+
rubyNode = new LocalReturnNode(argumentsNode);
1578+
}
1579+
1580+
assignNodePositionInSource(node, rubyNode);
1581+
return rubyNode;
15611582
}
15621583

15631584
public RubyNode visitSelfNode(Nodes.SelfNode node) {

0 commit comments

Comments
 (0)