Skip to content

Commit e276716

Browse files
committed
[Bug #20927] Fix compile_shareable_literal_constant for hash with keyword splat
Compilation of NODE_HASH in compile_shareable_literal_constant does not support hash that contains keyword splat. If there is a keyword splat, fallback to default case.
1 parent 3cb79d4 commit e276716

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

compile.c

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10473,6 +10473,12 @@ compile_shareable_literal_constant(rb_iseq_t *iseq, LINK_ANCHOR *ret, enum rb_pa
1047310473
*shareable_literal_p = 0;
1047410474
return COMPILE_OK;
1047510475
}
10476+
for (NODE *n = RNODE_HASH(node)->nd_head; n; n = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_next) {
10477+
if (!RNODE_LIST(n)->nd_head) {
10478+
// If the hash node have a keyword splat, fall back to the default case.
10479+
goto compile_shareable;
10480+
}
10481+
}
1047610482

1047710483
INIT_ANCHOR(anchor);
1047810484
lit = rb_hash_new();
@@ -10482,25 +10488,21 @@ compile_shareable_literal_constant(rb_iseq_t *iseq, LINK_ANCHOR *ret, enum rb_pa
1048210488
int shareable_literal_p2;
1048310489
NODE *key = RNODE_LIST(n)->nd_head;
1048410490
NODE *val = RNODE_LIST(RNODE_LIST(n)->nd_next)->nd_head;
10485-
if (key) {
10486-
CHECK(compile_shareable_literal_constant_next(key, anchor, &key_val, &shareable_literal_p2));
10487-
if (shareable_literal_p2) {
10488-
/* noop */
10489-
}
10490-
else if (RTEST(lit)) {
10491-
rb_hash_clear(lit);
10492-
lit = Qfalse;
10493-
}
10491+
CHECK(compile_shareable_literal_constant_next(key, anchor, &key_val, &shareable_literal_p2));
10492+
if (shareable_literal_p2) {
10493+
/* noop */
1049410494
}
10495-
if (val) {
10496-
CHECK(compile_shareable_literal_constant_next(val, anchor, &value_val, &shareable_literal_p2));
10497-
if (shareable_literal_p2) {
10498-
/* noop */
10499-
}
10500-
else if (RTEST(lit)) {
10501-
rb_hash_clear(lit);
10502-
lit = Qfalse;
10503-
}
10495+
else if (RTEST(lit)) {
10496+
rb_hash_clear(lit);
10497+
lit = Qfalse;
10498+
}
10499+
CHECK(compile_shareable_literal_constant_next(val, anchor, &value_val, &shareable_literal_p2));
10500+
if (shareable_literal_p2) {
10501+
/* noop */
10502+
}
10503+
else if (RTEST(lit)) {
10504+
rb_hash_clear(lit);
10505+
lit = Qfalse;
1050410506
}
1050510507
if (RTEST(lit)) {
1050610508
if (!UNDEF_P(key_val) && !UNDEF_P(value_val)) {
@@ -10516,6 +10518,8 @@ compile_shareable_literal_constant(rb_iseq_t *iseq, LINK_ANCHOR *ret, enum rb_pa
1051610518
}
1051710519

1051810520
default:
10521+
10522+
compile_shareable:
1051910523
if (shareable == rb_parser_shareable_literal &&
1052010524
(SHAREABLE_BARE_EXPRESSION || level > 0)) {
1052110525
CHECK(compile_ensure_shareable_node(iseq, ret, dest, node));
@@ -10529,7 +10533,7 @@ compile_shareable_literal_constant(rb_iseq_t *iseq, LINK_ANCHOR *ret, enum rb_pa
1052910533
return COMPILE_OK;
1053010534
}
1053110535

10532-
/* Array or Hash */
10536+
/* Array or Hash that does not have keyword splat */
1053310537
if (!lit) {
1053410538
if (nd_type(node) == NODE_LIST) {
1053510539
ADD_INSN1(anchor, node, newarray, INT2FIX(RNODE_LIST(node)->as.nd_alen));

test/ruby/test_parse.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# frozen_string_literal: false
33
require 'test/unit'
44
require 'stringio'
5+
require_relative '../lib/parser_support'
56

67
class TestParse < Test::Unit::TestCase
78
def setup
@@ -1609,6 +1610,26 @@ class X
16091610
assert_ractor_shareable(a[0])
16101611
end
16111612

1613+
def test_shareable_constant_value_hash_with_keyword_splat
1614+
# Prism compiler does not support keyword splat in Ractor constant [Bug #20916]
1615+
omit if ParserSupport.prism_enabled?
1616+
1617+
a, b = eval_separately("#{<<~"begin;"}\n#{<<~'end;'}")
1618+
begin;
1619+
# shareable_constant_value: experimental_everything
1620+
# [Bug #20927]
1621+
x = { x: {} }
1622+
y = { y: {} }
1623+
A = { **x }
1624+
B = { x: {}, **y }
1625+
[A, B]
1626+
end;
1627+
assert_ractor_shareable(a)
1628+
assert_ractor_shareable(b)
1629+
assert_equal({ x: {}}, a)
1630+
assert_equal({ x: {}, y: {}}, b)
1631+
end
1632+
16121633
def test_shareable_constant_value_unshareable_literal
16131634
assert_raise_separately(Ractor::IsolationError, /unshareable object to C/,
16141635
"#{<<~"begin;"}\n#{<<~'end;'}")

0 commit comments

Comments
 (0)