diff options
author | Jemma Issroff <[email protected]> | 2023-12-06 11:05:43 -0500 |
---|---|---|
committer | Jemma Issroff <[email protected]> | 2023-12-06 11:31:43 -0500 |
commit | f80262b14d01a977ce386a5a377e911a90ce328d (patch) | |
tree | 785407ea3e991fb4ab05e33e12d9c3fbb6f9371c | |
parent | 64ab04da6ae15243e1615739f2529f37e532943d (diff) |
[PRISM] Account for nil parent in Call{Operator,And,Or}PathWriteNodes
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
-rw-r--r-- | prism_compile.c | 21 | ||||
-rw-r--r-- | test/ruby/test_compile_prism.rb | 3 |
2 files changed, 21 insertions, 3 deletions
diff --git a/prism_compile.c b/prism_compile.c index f6d31e3cdc..7c41f5804b 100644 --- a/prism_compile.c +++ b/prism_compile.c @@ -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)); @@ -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)); @@ -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); diff --git a/test/ruby/test_compile_prism.rb b/test/ruby/test_compile_prism.rb index 8c74003f5e..62edb329bc 100644 --- a/test/ruby/test_compile_prism.rb +++ b/test/ruby/test_compile_prism.rb @@ -253,15 +253,18 @@ module Prism 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 |