diff --git a/NEWS.md b/NEWS.md index d760de7c11..3fabf7b331 100644 --- a/NEWS.md +++ b/NEWS.md @@ -121,6 +121,13 @@ Excluding feature bug fixes. your plan to https://github.com/ruby/xmlrpc or https://github.com/ruby/net-telnet. +* EXPERIMENTAL: Hash#each consistently yields a 2-element array [[Bug #12706]] + + * Now `{ a: 1 }.each(&->(k, v) { })` raises an ArgumentError + due to lambda's arity check. + * This is experimental; if it brings a big incompatibility issue, + it may be reverted until 2.8/3.0 release. + ## Stdlib compatibility issues Excluding feature bug fixes. diff --git a/hash.c b/hash.c index 5bbb3da5f2..864de0a480 100644 --- a/hash.c +++ b/hash.c @@ -3105,7 +3105,7 @@ static VALUE rb_hash_each_pair(VALUE hash) { RETURN_SIZED_ENUMERATOR(hash, 0, 0, hash_enum_size); - if (rb_block_arity() > 1) + if (rb_block_pair_yield_optimizable()) rb_hash_foreach(hash, each_pair_i_fast, 0); else rb_hash_foreach(hash, each_pair_i, 0); @@ -4446,7 +4446,7 @@ rb_hash_any_p(int argc, VALUE *argv, VALUE hash) /* yields pairs, never false */ return Qtrue; } - if (rb_block_arity() > 1) + if (rb_block_pair_yield_optimizable()) rb_hash_foreach(hash, any_p_i_fast, (VALUE)args); else rb_hash_foreach(hash, any_p_i, (VALUE)args); @@ -5500,7 +5500,7 @@ env_each_pair(VALUE ehash) } FREE_ENVIRON(environ); - if (rb_block_arity() > 1) { + if (rb_block_pair_yield_optimizable()) { for (i=0; icfp; + VALUE block_handler = rb_vm_frame_block_handler(cfp); + struct rb_block block; + + if (block_handler == VM_BLOCK_HANDLER_NONE) { + rb_raise(rb_eArgError, "no block given"); + } + + block_setup(&block, block_handler); + min = rb_vm_block_min_max_arity(&block, &max); + + switch (vm_block_type(&block)) { + case block_handler_type_symbol: + return 0; + + case block_handler_type_proc: + { + VALUE procval = block_handler; + rb_proc_t *proc; + GetProcPtr(procval, proc); + if (proc->is_lambda) return 0; + if (min != max) return 0; + return min > 1; + } + + default: + return min > 1; + } +} + int rb_block_arity(void) { diff --git a/spec/ruby/core/hash/shared/each.rb b/spec/ruby/core/hash/shared/each.rb index d1f2e5f672..5e88a35445 100644 --- a/spec/ruby/core/hash/shared/each.rb +++ b/spec/ruby/core/hash/shared/each.rb @@ -21,19 +21,37 @@ describe :hash_each, shared: true do ary.sort.should == ["a", "b", "c"] end - it "yields 2 values and not an Array of 2 elements when given a callable of arity 2" do - obj = Object.new - def obj.foo(key, value) - ScratchPad << key << value + ruby_version_is ""..."2.8" do + it "yields 2 values and not an Array of 2 elements when given a callable of arity 2" do + obj = Object.new + def obj.foo(key, value) + ScratchPad << key << value + end + + ScratchPad.record([]) + { "a" => 1 }.send(@method, &obj.method(:foo)) + ScratchPad.recorded.should == ["a", 1] + + ScratchPad.record([]) + { "a" => 1 }.send(@method, &-> key, value { ScratchPad << key << value }) + ScratchPad.recorded.should == ["a", 1] end + end - ScratchPad.record([]) - { "a" => 1 }.send(@method, &obj.method(:foo)) - ScratchPad.recorded.should == ["a", 1] + ruby_version_is "2.8" do + it "yields an Array of 2 elements when given a callable of arity 2" do + obj = Object.new + def obj.foo(key, value) + end - ScratchPad.record([]) - { "a" => 1 }.send(@method, &-> key, value { ScratchPad << key << value }) - ScratchPad.recorded.should == ["a", 1] + -> { + { "a" => 1 }.send(@method, &obj.method(:foo)) + }.should raise_error(ArgumentError) + + -> { + { "a" => 1 }.send(@method, &-> key, value { }) + }.should raise_error(ArgumentError) + end end it "uses the same order as keys() and values()" do diff --git a/struct.c b/struct.c index 79131db2bd..3042d3bed5 100644 --- a/struct.c +++ b/struct.c @@ -821,7 +821,7 @@ rb_struct_each_pair(VALUE s) RETURN_SIZED_ENUMERATOR(s, 0, 0, struct_enum_size); members = rb_struct_members(s); - if (rb_block_arity() > 1) { + if (rb_block_pair_yield_optimizable()) { for (i=0; i(k, v) {}) + end + end end