summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Zhu <peter@peterzhu.ca>2024-06-10 19:05:58 -0400
committerGitHub <noreply@github.com>2024-06-10 16:05:58 -0700
commit40251ed0dfe99bb09c2fa542fce603ade25e3729 (patch)
tree9b3bb804720dcddf10e501b66a8fb2a9f8d57dc1
parentd0327a7224d8d778a75c7554b287369895dc17be (diff)
Fix inconsistent evaluation of keyword splat (#10959)
[Bug #20180] Backports #9624.
-rw-r--r--compile.c7
-rw-r--r--test/ruby/test_syntax.rb15
2 files changed, 21 insertions, 1 deletions
diff --git a/compile.c b/compile.c
index 3c92729760..a938d58abb 100644
--- a/compile.c
+++ b/compile.c
@@ -4826,7 +4826,12 @@ compile_array_1(rb_iseq_t *iseq, LINK_ANCHOR *const ret, const NODE *node)
}
else {
CHECK(COMPILE_(ret, "array element", node, FALSE));
- ADD_INSN1(ret, node, newarray, INT2FIX(1));
+ if (keyword_node_p(node)) {
+ ADD_INSN1(ret, node, newarraykwsplat, INT2FIX(1));
+ }
+ else {
+ ADD_INSN1(ret, node, newarray, INT2FIX(1));
+ }
}
return 1;
diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb
index 2d4ce59b39..a7a25ef3c0 100644
--- a/test/ruby/test_syntax.rb
+++ b/test/ruby/test_syntax.rb
@@ -233,6 +233,7 @@ class TestSyntax < Test::Unit::TestCase
def test_array_kwsplat_hash
kw = {}
h = {a: 1}
+ a = []
assert_equal([], [**{}])
assert_equal([], [**kw])
assert_equal([h], [**h])
@@ -247,6 +248,20 @@ class TestSyntax < Test::Unit::TestCase
assert_equal([1, kw], [1, kw])
assert_equal([1, h], [1, h])
+ assert_equal([], [*a, **{}])
+ assert_equal([], [*a, **kw])
+ assert_equal([h], [*a, **h])
+ assert_equal([{}], [*a, {}])
+ assert_equal([kw], [*a, kw])
+ assert_equal([h], [*a, h])
+
+ assert_equal([1], [1, *a, **{}])
+ assert_equal([1], [1, *a, **kw])
+ assert_equal([1, h], [1, *a, **h])
+ assert_equal([1, {}], [1, *a, {}])
+ assert_equal([1, kw], [1, *a, kw])
+ assert_equal([1, h], [1, *a, h])
+
assert_equal([], [**kw, **kw])
assert_equal([], [**kw, **{}, **kw])
assert_equal([1], [1, **kw, **{}, **kw])