From 47141797bed55eb10932c9a722a5132f50d4f3d8 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Mon, 16 Mar 2020 23:03:22 +0900 Subject: hash.c: Do not use the fast path (rb_yield_values) for lambda blocks As a semantics, Hash#each yields a 2-element array (pairs of keys and values). So, `{ a: 1 }.each(&->(k, v) { })` should raise an exception due to lambda's arity check. However, the optimization that avoids Array allocation by using rb_yield_values for blocks whose arity is more than 1 (introduced at b9d29603375d17c3d1d609d9662f50beaec61fa1 and some commits), seemed to overlook the lambda case, and wrongly allowed the code above to work. This change experimentally attempts to make it strict; now the code above raises an ArgumentError. This is an incompatible change; if the compatibility issue is bigger than our expectation, it may be reverted (until Ruby 3.0 release). [Bug #12706] --- spec/ruby/core/hash/shared/each.rb | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'spec/ruby/core/hash/shared/each.rb') 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 + + -> { + { "a" => 1 }.send(@method, &obj.method(:foo)) + }.should raise_error(ArgumentError) - ScratchPad.record([]) - { "a" => 1 }.send(@method, &-> key, value { ScratchPad << key << value }) - ScratchPad.recorded.should == ["a", 1] + -> { + { "a" => 1 }.send(@method, &-> key, value { }) + }.should raise_error(ArgumentError) + end end it "uses the same order as keys() and values()" do -- cgit v1.2.3