aboutsummaryrefslogtreecommitdiffstats
path: root/struct.c
diff options
context:
space:
mode:
authorYusuke Endoh <mame@ruby-lang.org>2020-03-16 23:03:22 +0900
committerYusuke Endoh <mame@ruby-lang.org>2020-03-16 23:17:12 +0900
commit47141797bed55eb10932c9a722a5132f50d4f3d8 (patch)
treea2934da7ecc862d7746eaf0f504aea16cc35b653 /struct.c
parent4be2a891cce920d2e2c2ece572c66e5aabe98eaa (diff)
downloadruby-47141797bed55eb10932c9a722a5132f50d4f3d8.tar.gz
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]
Diffstat (limited to 'struct.c')
-rw-r--r--struct.c2
1 files changed, 1 insertions, 1 deletions
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<RSTRUCT_LEN(s); i++) {
VALUE key = rb_ary_entry(members, i);
VALUE value = RSTRUCT_GET(s, i);