Skip to content

Commit 65cca99

Browse files
casperisfinebyroot
andauthored
parse.y: const_decl_path don't replace destination node by a literal (ruby#11314)
[Bug #20668] The `dest` node is assumed to be a `CDECL`, so overwriting it with a `LIT` cause a crash on the next iteration. Co-authored-by: Jean Boussier <[email protected]>
1 parent f85c7de commit 65cca99

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

parse.y

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13528,9 +13528,9 @@ mark_lvar_used(struct parser_params *p, NODE *rhs)
1352813528
}
1352913529

1353013530
static NODE *
13531-
const_decl_path(struct parser_params *p, NODE **dest)
13531+
const_decl_path(struct parser_params *p, NODE *dest)
1353213532
{
13533-
NODE *n = *dest;
13533+
NODE *n = dest;
1353413534
if (!nd_type_p(n, NODE_CALL)) {
1353513535
const YYLTYPE *loc = &n->nd_loc;
1353613536
VALUE path;
@@ -13558,7 +13558,7 @@ const_decl_path(struct parser_params *p, NODE **dest)
1355813558
path = rb_ary_join(rb_ary_reverse(path), rb_str_new_cstr("::"));
1355913559
path = rb_fstring(path);
1356013560
}
13561-
*dest = n = NEW_LIT(path, loc);
13561+
n = NEW_LIT(path, loc);
1356213562
RB_OBJ_WRITTEN(p->ast, Qnil, RNODE_LIT(n)->nd_lit);
1356313563
}
1356413564
return n;
@@ -13584,7 +13584,7 @@ ensure_shareable_node(struct parser_params *p, NODE **dest, NODE *value, const Y
1358413584
{
1358513585
NODE *fcore = NEW_LIT(rb_mRubyVMFrozenCore, loc);
1358613586
NODE *args = NEW_LIST(value, loc);
13587-
args = list_append(p, args, const_decl_path(p, dest));
13587+
args = list_append(p, args, const_decl_path(p, *dest));
1358813588
return NEW_CALL(fcore, rb_intern("ensure_shareable"), args, loc);
1358913589
}
1359013590

test/ruby/test_parse.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,21 @@ def test_shareable_constant_value_simple
14751475
assert !obj.equal?(a)
14761476
end
14771477

1478+
def test_shareable_constant_value_literal_const_refs
1479+
a = eval_separately("#{<<~"begin;"}\n#{<<~'end;'}")
1480+
begin;
1481+
# shareable_constant_value: literal
1482+
# [Bug #20668]
1483+
SOME_CONST = {
1484+
'Object' => Object,
1485+
'String' => String,
1486+
'Array' => Array,
1487+
}
1488+
SOME_CONST
1489+
end;
1490+
assert_ractor_shareable(a)
1491+
end
1492+
14781493
def test_shareable_constant_value_nested
14791494
a, b = eval_separately("#{<<~"begin;"}\n#{<<~'end;'}")
14801495
begin;

0 commit comments

Comments
 (0)