Skip to content

Commit 34ffb5b

Browse files
committed
Optimise AST to destructure block's single array argument for required keyword argument
1 parent 55caff0 commit 34ffb5b

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

spec/truffle/parsing/fixtures/block/destructuring_array_argument/when_keyword_arguments_present_and_should_destructure.yaml

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ notes: >
44
It handles keyword arguments as usually.
55
A simplified AST for array argument destructuring:
66
7-
(WriteLocalVariableNode frameSlot = 3
8-
(ReadKeywordArgumentNodeGen name = :c))
7+
(ReadKeywordArgumentNodeGen name = :c)
8+
yarp_specific: true # there is optimization in destructuring a single array argument
9+
# to return ReadKeywordArgumentNodeGen for required keyword parameter
10+
# instead of (WriteLocalVariableNode (ReadKeywordArgumentNodeGen))
911
focused_on_node: "org.truffleruby.language.methods.BlockDefinitionNode"
1012
ruby: |
1113
proc do |a, b, c:|
@@ -223,19 +225,13 @@ ast: |
223225
flags = 0
224226
frameSlot = 4 # %destructure_1
225227
type = FRAME_LOCAL
226-
WriteLocalVariableNode
228+
ReadKeywordArgumentNodeGen
227229
attributes:
228230
flags = 0
229-
frameSlot = 3 # c
231+
name = :c
230232
children:
231-
valueNode =
232-
ReadKeywordArgumentNodeGen
233-
attributes:
234-
flags = 0
235-
name = :c
236-
children:
237-
readUserKeywordsHashNode =
238-
ReadUserKeywordsHashNode
233+
readUserKeywordsHashNode =
234+
ReadUserKeywordsHashNode
239235
]
240236
NilLiteralNode
241237
attributes:

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,11 @@ public RubyNode visitMultiTargetNode(Nodes.MultiTargetNode node) {
133133

134134
@Override
135135
public RubyNode visitRequiredKeywordParameterNode(Nodes.RequiredKeywordParameterNode node) {
136-
final int slot = environment.declareVar(node.name);
136+
// Passing single array argument to a block with required keyword arguments should lead
137+
// to ArgumentError 'missing keyword' in runtime.
138+
// So use ReadKeywordArgumentNode to raise this exception.
137139
final var name = language.getSymbol(node.name);
138-
final RubyNode readNode = ReadKeywordArgumentNode.create(name, null);
139-
140-
// actually if there is required keyword argument then ArgumentError 'missing keyword'
141-
// should be raised in runtime before executing this node
142-
return new WriteLocalVariableNode(slot, readNode);
140+
return ReadKeywordArgumentNode.create(name, null);
143141
}
144142

145143
@Override

0 commit comments

Comments
 (0)