Assert possible hash functions in RHASH_ST_TABLE (#7107)

Because of the function pointer, it's hard to figure out what hash
functions could be used in Hash objects when st_lookup is used.

Having this assertion makes it easier to understand what
hash_stlike_lookup could possibly do. (AR uses only rb_any_hash)

For example, this clarifies that hash_stlike_lookup never calls a #hash
method when a key is T_STRING or T_SYMBOL.
This commit is contained in:
Takashi Kokubun 2023-01-11 23:14:58 -08:00 committed by GitHub
parent a8537eae2a
commit d4a3882c1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
Notes: git 2023-01-12 07:15:19 +00:00
Merged-By: k0kubun <takashikkbn@gmail.com>

4
hash.c
View File

@ -2110,6 +2110,10 @@ hash_stlike_lookup(VALUE hash, st_data_t key, st_data_t *pval)
return ar_lookup(hash, key, pval); return ar_lookup(hash, key, pval);
} }
else { else {
extern st_index_t rb_iseq_cdhash_hash(VALUE);
RUBY_ASSERT(RHASH_ST_TABLE(hash)->type->hash == rb_any_hash ||
RHASH_ST_TABLE(hash)->type->hash == rb_ident_hash ||
RHASH_ST_TABLE(hash)->type->hash == rb_iseq_cdhash_hash);
return st_lookup(RHASH_ST_TABLE(hash), key, pval); return st_lookup(RHASH_ST_TABLE(hash), key, pval);
} }
} }