From aa3030ac24389f838fd85390601489c60fd13877 Mon Sep 17 00:00:00 2001 From: eileencodes Date: Wed, 17 Jul 2024 13:26:02 -0400 Subject: [PATCH] Fix empty hash instruction When we have an empty hash the iseq should have a `newhash` but instead had a `duphash`. To fix, check if the node's elements are equal to `0`. If so we want a `newhash`, otherwise use the original `duphash` instructions. Before: ``` == disasm: #@test2.rb:1 (1,0)-(1,2)> 0000 duphash {} ( 1)[Li] 0002 leave ``` After: ``` == disasm: #@test2.rb:1 (1,0)-(1,2)> 0000 newhash 0 ( 1)[Li] 0002 leave ``` Fixes the test `TestYJIT#test_compile_newhash`. Related to ruby/prism#2935 --- prism_compile.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/prism_compile.c b/prism_compile.c index b867b7900b..3055543703 100644 --- a/prism_compile.c +++ b/prism_compile.c @@ -7164,9 +7164,16 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, // is popped, then we know we don't need to do anything since it's // statically known. if (!popped) { - VALUE value = pm_static_literal_value(iseq, node, scope_node); - PUSH_INSN1(ret, location, duphash, value); - RB_OBJ_WRITTEN(iseq, Qundef, value); + const pm_hash_node_t *cast = (const pm_hash_node_t *) node; + + if (cast->elements.size == 0) { + PUSH_INSN1(ret, location, newhash, INT2FIX(0)); + } + else { + VALUE value = pm_static_literal_value(iseq, node, scope_node); + PUSH_INSN1(ret, location, duphash, value); + RB_OBJ_WRITTEN(iseq, Qundef, value); + } } } else {