Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dup splat array in certain cases where there is a block argument #3157

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion compile.c
Expand Up @@ -5171,12 +5171,12 @@ setup_args(rb_iseq_t *iseq, LINK_ANCHOR *const args, const NODE *argn,
{
VALUE ret;
if (argn && nd_type(argn) == NODE_BLOCK_PASS) {
unsigned int dup_rest = 1;
DECL_ANCHOR(arg_block);
INIT_ANCHOR(arg_block);
NO_CHECK(COMPILE(arg_block, "block", argn->nd_body));

*flag |= VM_CALL_ARGS_BLOCKARG;
ret = setup_args_core(iseq, args, argn->nd_head, 0, flag, keywords);

if (LIST_INSN_SIZE_ONE(arg_block)) {
LINK_ELEMENT *elem = FIRST_ELEMENT(arg_block);
Expand All @@ -5185,8 +5185,10 @@ setup_args(rb_iseq_t *iseq, LINK_ANCHOR *const args, const NODE *argn,
if (iobj->insn_id == BIN(getblockparam)) {
iobj->insn_id = BIN(getblockparamproxy);
}
dup_rest = 0;
}
}
ret = setup_args_core(iseq, args, argn->nd_head, dup_rest, flag, keywords);
ADD_SEQ(args, arg_block);
}
else {
Expand Down
36 changes: 27 additions & 9 deletions spec/ruby/language/send_spec.rb
Expand Up @@ -421,18 +421,36 @@ def []=(*)
specs.rest_len(0,*a,4,*5,6,7,*c,-1).should == 11
end

it "expands the Array elements from the splat after executing the arguments and block if no other arguments follow the splat" do
def self.m(*args, &block)
[args, block]
ruby_version_is ""..."2.8" do
it "expands the Array elements from the splat after executing the arguments and block if no other arguments follow the splat" do
def self.m(*args, &block)
[args, block]
end

args = [1, nil]
m(*args, &args.pop).should == [[1], nil]

args = [1, nil]
order = []
m(*(order << :args; args), &(order << :block; args.pop)).should == [[1], nil]
order.should == [:args, :block]
end
end

args = [1, nil]
m(*args, &args.pop).should == [[1], nil]
ruby_version_is "2.8" do
it "expands the Array elements from the splat before applying block argument operations" do
def self.m(*args, &block)
[args, block]
end

args = [1, nil]
order = []
m(*(order << :args; args), &(order << :block; args.pop)).should == [[1], nil]
order.should == [:args, :block]
args = [1, nil]
m(*args, &args.pop).should == [[1, nil], nil]

args = [1, nil]
order = []
m(*(order << :args; args), &(order << :block; args.pop)).should == [[1, nil], nil]
order.should == [:args, :block]
end
end

it "evaluates the splatted arguments before the block if there are other arguments after the splat" do
Expand Down
9 changes: 9 additions & 0 deletions test/ruby/test_call.rb
Expand Up @@ -99,4 +99,13 @@ def test_call_splat_order
ary = [1, 2]
assert_equal([0, 1, 2, 1], aaa(0, *ary, ary.shift), bug12860)
end

def test_call_block_order
bug16504 = '[ruby-core:96769] [Bug# 16504]'
b = proc{}
ary = [1, 2, b]
assert_equal([1, 2, b], aaa(*ary, &ary.pop), bug16504)
ary = [1, 2, b]
assert_equal([0, 1, 2, b], aaa(0, *ary, &ary.pop), bug16504)
end
end