Skip to content

Commit d00685b

Browse files
committed
Avoid extra EmptyHashLiteralNode when translating HashParseNode
* For instance, `foo(1, 2, **h);` would create: $ jt ruby -e 'def m; h={a:1}; foo(1, 2, **h); end; Truffle::Debug.print_ast method(:m)' ... body[3] = RubyCallNode receiver = SelfNode arguments[0] = IntegerFixnumLiteralNode arguments[1] = IntegerFixnumLiteralNode arguments[2] = ConcatHashLiteralNode children[0] = HashLiteralNode$EmptyHashLiteralNode children[1] = HashCastNodeGen child_ = ReadLocalVariableNode children[2] = HashLiteralNode$EmptyHashLiteralNode and with this fix: body[3] = RubyCallNode receiver = SelfNode arguments[0] = IntegerFixnumLiteralNode arguments[1] = IntegerFixnumLiteralNode arguments[2] = HashCastNodeGen child_ = ReadLocalVariableNode
1 parent c1d3c7c commit d00685b

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,15 +1614,23 @@ public RubyNode visitGlobalVarNode(GlobalVarParseNode node) {
16141614
public RubyNode visitHashNode(HashParseNode node) {
16151615
final SourceIndexLength sourceSection = node.getPosition();
16161616

1617+
if (node.isEmpty()) { // an empty Hash literal like h = {}
1618+
final RubyNode ret = HashLiteralNode.create(language, RubyNode.EMPTY_ARRAY);
1619+
ret.unsafeSetSourceSection(sourceSection);
1620+
return addNewlineIfNeeded(node, ret);
1621+
}
1622+
16171623
final List<RubyNode> hashConcats = new ArrayList<>();
16181624
final List<RubyNode> keyValues = new ArrayList<>();
16191625

16201626
for (ParseNodeTuple pair : node.getPairs()) {
16211627
if (pair.getKey() == null) {
16221628
// This null case is for splats {a: 1, **{b: 2}, c: 3}
1623-
final RubyNode hashLiteralSoFar = HashLiteralNode
1624-
.create(language, keyValues.toArray(RubyNode.EMPTY_ARRAY));
1625-
hashConcats.add(hashLiteralSoFar);
1629+
if (!keyValues.isEmpty()) {
1630+
final RubyNode hashLiteralSoFar = HashLiteralNode
1631+
.create(language, keyValues.toArray(RubyNode.EMPTY_ARRAY));
1632+
hashConcats.add(hashLiteralSoFar);
1633+
}
16261634
hashConcats.add(HashCastNodeGen.create(pair.getValue().accept(this)));
16271635
keyValues.clear();
16281636
} else {
@@ -1636,8 +1644,10 @@ public RubyNode visitHashNode(HashParseNode node) {
16361644
}
16371645
}
16381646

1639-
final RubyNode hashLiteralSoFar = HashLiteralNode.create(language, keyValues.toArray(RubyNode.EMPTY_ARRAY));
1640-
hashConcats.add(hashLiteralSoFar);
1647+
if (!keyValues.isEmpty()) {
1648+
final RubyNode hashLiteralSoFar = HashLiteralNode.create(language, keyValues.toArray(RubyNode.EMPTY_ARRAY));
1649+
hashConcats.add(hashLiteralSoFar);
1650+
}
16411651

16421652
if (hashConcats.size() == 1) {
16431653
final RubyNode ret = hashConcats.get(0);

0 commit comments

Comments
 (0)