1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Make method id explicit in rb_exec_recursive_outer

Previously, because opt_aref and opt_aset don't push a frame, when they
would call rb_hash to determine the hash value of the key, the initial
level of recursion would incorrectly use the method id at the top of the
stack instead of "hash".

This commit replaces rb_exec_recursive_outer with
rb_exec_recursive_outer_mid, which takes an explicit method id, so that
we can make the hash calculation behave consistently.

rb_exec_recursive_outer was documented as being internal, so I believe
this should be okay to change.
This commit is contained in:
John Hawthorn 2022-06-10 09:43:05 -07:00
parent f4747958e5
commit 52da90acee
Notes: git 2022-06-11 06:48:42 +09:00
5 changed files with 27 additions and 5 deletions
ext/-test-/recursion
hash.c
include/ruby/internal/intern
test/ruby
thread.c

View file

@ -16,7 +16,7 @@ exec_recursive(VALUE self, VALUE mid)
static VALUE
exec_recursive_outer(VALUE self, VALUE mid)
{
return rb_exec_recursive_outer(recursive_i, self, mid);
return rb_exec_recursive_outer_mid(recursive_i, self, mid, rb_intern("exec_recursive_outer"));
}
void

2
hash.c
View file

@ -195,7 +195,7 @@ obj_any_hash(VALUE obj)
VALUE hval = rb_check_funcall_basic_kw(obj, id_hash, rb_mKernel, 0, 0, 0);
if (hval == Qundef) {
hval = rb_exec_recursive_outer(hash_recursive, obj, 0);
hval = rb_exec_recursive_outer_mid(hash_recursive, obj, 0, id_hash);
}
while (!FIXNUM_P(hval)) {

View file

@ -292,6 +292,7 @@ VALUE rb_exec_recursive_paired(VALUE (*f)(VALUE g, VALUE h, int r), VALUE g, VAL
* @param[in] f The function that possibly recurs.
* @param[in,out] g Passed as-is to `f`.
* @param[in,out] h Passed as-is to `f`.
* @param[in] mid The ID of the method name being called
* @return The return value of f.
*
* @internal
@ -299,7 +300,7 @@ VALUE rb_exec_recursive_paired(VALUE (*f)(VALUE g, VALUE h, int r), VALUE g, VAL
* It seems nobody uses the "it calls rb_throw_obj()" part of this function.
* @shyouhei doesn't understand the needs.
*/
VALUE rb_exec_recursive_outer(VALUE (*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h);
VALUE rb_exec_recursive_outer_mid(VALUE (*f)(VALUE g, VALUE h, int r), VALUE g, VALUE h, ID mid);
/**
* Identical to rb_exec_recursive_outer(), except it checks for the recursion

View file

@ -2166,6 +2166,27 @@ class TestHash < Test::Unit::TestCase
end
end
# Previously this test would fail because rb_hash inside opt_aref would look
# at the current method name
def test_hash_recursion_independent_of_mid
o = Class.new do
def hash(h, k)
h[k]
end
def any_other_name(h, k)
h[k]
end
end.new
rec = []; rec << rec
h = @cls[]
h[rec] = 1
assert o.hash(h, rec)
assert o.any_other_name(h, rec)
end
def test_any_hash_fixable
20.times do
assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")

View file

@ -5158,9 +5158,9 @@ rb_exec_recursive_paired(VALUE (*func) (VALUE, VALUE, int), VALUE obj, VALUE pai
*/
VALUE
rb_exec_recursive_outer(VALUE (*func) (VALUE, VALUE, int), VALUE obj, VALUE arg)
rb_exec_recursive_outer_mid(VALUE (*func) (VALUE, VALUE, int), VALUE obj, VALUE arg, ID mid)
{
return exec_recursive(func, obj, 0, arg, 1, rb_frame_last_func());
return exec_recursive(func, obj, 0, arg, 1, mid);
}
/*