Skip to content

Commit

Permalink
[PRISM] Fix ElseNode within CaseNode
Browse files Browse the repository at this point in the history
The logic within the consequent for the CaseNodes in popped cases
was incorrect as it wouldn't emit consequent instructions for a
popped CaseNode. This commit fixes that.
  • Loading branch information
jemmaissroff committed Dec 11, 2023
1 parent 0562c24 commit c69d136
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
6 changes: 2 additions & 4 deletions prism_compile.c
Expand Up @@ -2288,9 +2288,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
PM_POP;

if (case_node->consequent) {
if (!popped || !PM_NODE_TYPE_P(((pm_node_t *)case_node->consequent), PM_ELSE_NODE)) {
PM_COMPILE_NOT_POPPED((pm_node_t *)case_node->consequent);
}
PM_COMPILE((pm_node_t *)case_node->consequent);
}
else {
PM_PUTNIL_UNLESS_POPPED;
Expand Down Expand Up @@ -2792,7 +2790,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
PM_COMPILE((pm_node_t *)cast->statements);
}
else {
PM_PUTNIL;
PM_PUTNIL_UNLESS_POPPED;
}
return;
}
Expand Down
11 changes: 11 additions & 0 deletions test/ruby/test_compile_prism.rb
Expand Up @@ -701,6 +701,17 @@ def test_CaseNode
assert_prism_eval("case; when :a, :b; 1; else; 2 end")
assert_prism_eval("case :a; when :b; else; end")
assert_prism_eval("b = 1; case :a; when b; else; end")
assert_prism_eval(<<-CODE)
def self.prism_test_case_node
case :a
when :b
else
return 2
end
1
end
prism_test_case_node
CODE
end

def test_ElseNode
Expand Down

0 comments on commit c69d136

Please sign in to comment.