From a029b54ec716812ade37fef1f857c49f821a8cc8 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Wed, 28 Aug 2019 16:41:39 -0700 Subject: [PATCH] Make Enumerator::Chain#each treat lambdas as lambda Previously, lambdas were converted to procs because of how rb_block_call works. Switch to rb_funcall_with_block, which handles procs as procs and lambdas as lambdas. Fixes [Bug #15613] --- enumerator.c | 9 +-------- test/ruby/test_enumerator.rb | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/enumerator.c b/enumerator.c index a5d7106b60..67b9302a62 100644 --- a/enumerator.c +++ b/enumerator.c @@ -3111,12 +3111,6 @@ enum_chain_enum_size(VALUE obj, VALUE args, VALUE eobj) return enum_chain_size(obj); } -static VALUE -enum_chain_yield_block(RB_BLOCK_CALL_FUNC_ARGLIST(_, block)) -{ - return rb_funcallv(block, id_call, argc, argv); -} - static VALUE enum_chain_enum_no_size(VALUE obj, VALUE args, VALUE eobj) { @@ -3148,10 +3142,9 @@ enum_chain_each(int argc, VALUE *argv, VALUE obj) enums = objptr->enums; block = rb_block_proc(); - for (i = 0; i < RARRAY_LEN(enums); i++) { objptr->pos = i; - rb_block_call(RARRAY_AREF(enums, i), id_each, argc, argv, enum_chain_yield_block, block); + rb_funcall_with_block(RARRAY_AREF(enums, i), id_each, argc, argv, block); } return obj; diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb index 65adb5a438..75cf1aeec6 100644 --- a/test/ruby/test_enumerator.rb +++ b/test/ruby/test_enumerator.rb @@ -864,4 +864,20 @@ class TestEnumerator < Test::Unit::TestCase ], enum.to_a } end + + def test_chain_each_lambda + c = Class.new do + include Enumerable + attr_reader :is_lambda + def each(&block) + return to_enum unless block + @is_lambda = block.lambda? + end + end + e = c.new + e.chain.each{} + assert_equal(false, e.is_lambda) + e.chain.each(&->{}) + assert_equal(true, e.is_lambda) + end end