Skip to content

Commit

Permalink
[PRISM] Account for nil parent in Call{Operator,And,Or}PathWriteNodes
Browse files Browse the repository at this point in the history
Prior to this commit, we were not accounting for the case of a nil
parent in a CallXPathWriteNode, for example ::A ||= 1. This commit
checks if the parent exists, and if not, uses Object as the inferred
parent
  • Loading branch information
jemmaissroff committed Dec 6, 2023
1 parent 64ab04d commit f80262b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
21 changes: 18 additions & 3 deletions prism_compile.c
Expand Up @@ -2309,7 +2309,12 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
LABEL *lfin = NEW_LABEL(lineno);

pm_constant_path_node_t *target = constant_path_and_write_node->target;
PM_COMPILE_NOT_POPPED(target->parent);
if (target->parent) {
PM_COMPILE_NOT_POPPED(target->parent);
}
else {
ADD_INSN1(ret, &dummy_line_node, putobject, rb_cObject);
}

pm_constant_read_node_t *child = (pm_constant_read_node_t *)target->child;
VALUE child_name = ID2SYM(pm_constant_id_lookup(scope_node, child->name));
Expand Down Expand Up @@ -2347,7 +2352,12 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
LABEL *lfin = NEW_LABEL(lineno);

pm_constant_path_node_t *target = constant_path_or_write_node->target;
PM_COMPILE_NOT_POPPED(target->parent);
if (target->parent) {
PM_COMPILE_NOT_POPPED(target->parent);
}
else {
ADD_INSN1(ret, &dummy_line_node, putobject, rb_cObject);
}

pm_constant_read_node_t *child = (pm_constant_read_node_t *)target->child;
VALUE child_name = ID2SYM(pm_constant_id_lookup(scope_node, child->name));
Expand Down Expand Up @@ -2387,7 +2397,12 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
pm_constant_path_operator_write_node_t *constant_path_operator_write_node = (pm_constant_path_operator_write_node_t*) node;

pm_constant_path_node_t *target = constant_path_operator_write_node->target;
PM_COMPILE_NOT_POPPED(target->parent);
if (target->parent) {
PM_COMPILE_NOT_POPPED(target->parent);
}
else {
ADD_INSN1(ret, &dummy_line_node, putobject, rb_cObject);
}

PM_DUP;
ADD_INSN1(ret, &dummy_line_node, putobject, Qtrue);
Expand Down
3 changes: 3 additions & 0 deletions test/ruby/test_compile_prism.rb
Expand Up @@ -253,15 +253,18 @@ def test_ConstantPathWriteNode
def test_ConstantPathAndWriteNode
assert_prism_eval("Prism::CPAWN = 1; Prism::CPAWN &&= 2")
assert_prism_eval("Prism::CPAWN &&= 1")
assert_prism_eval("::CPAWN = 1; ::CPAWN &&= 2")
end

def test_ConstantPathOrWriteNode
assert_prism_eval("Prism::CPOrWN = nil; Prism::CPOrWN ||= 1")
assert_prism_eval("Prism::CPOrWN ||= 1")
assert_prism_eval("::CPOrWN = nil; ::CPOrWN ||= 1")
end

def test_ConstantPathOperatorWriteNode
assert_prism_eval("Prism::CPOWN = 0; Prism::CPOWN += 1")
assert_prism_eval("::CPOWN = 0; ::CPOWN += 1")
end

def test_GlobalVariableAndWriteNode
Expand Down

0 comments on commit f80262b

Please sign in to comment.