diff --git a/include/ruby/assert.h b/include/ruby/assert.h index ceab090427..e9edd9e640 100644 --- a/include/ruby/assert.h +++ b/include/ruby/assert.h @@ -281,6 +281,17 @@ RBIMPL_WARNING_IGNORED(-Wgnu-zero-variadic-macro-arguments) # define RUBY_ASSERT_WHEN(cond, expr) RUBY_ASSERT_MESG_WHEN((cond), (expr), #expr) #endif +/** + * A variant of #RUBY_ASSERT that asserts when either #RUBY_DEBUG or built-in + * type of `obj` is `type`. + * + * @param obj Object to check its built-in typue. + * @param type Built-in type constant, T_ARRAY, T_STRING, etc. + */ +#define RUBY_ASSERT_BUILTIN_TYPE(obj, type) \ + RUBY_ASSERT(RB_TYPE_P(obj, type), \ + "Actual type is %s", rb_builtin_type_name(BUILTIN_TYPE(obj))) + /** * This is either #RUBY_ASSERT or #RBIMPL_ASSUME, depending on #RUBY_DEBUG. * diff --git a/string.c b/string.c index b656f7ab0d..6c3a95d47b 100644 --- a/string.c +++ b/string.c @@ -11764,7 +11764,7 @@ sym_inspect(VALUE sym) } dest[0] = ':'; - RUBY_ASSERT(BUILTIN_TYPE(str) == T_STRING); + RUBY_ASSERT_BUILTIN_TYPE(str, T_STRING); return str; } diff --git a/symbol.c b/symbol.c index ba28e6650c..7126154bf8 100644 --- a/symbol.c +++ b/symbol.c @@ -430,8 +430,8 @@ static void set_id_entry(rb_symbols_t *symbols, rb_id_serial_t num, VALUE str, VALUE sym) { ASSERT_vm_locking(); - RUBY_ASSERT(BUILTIN_TYPE(str) == T_STRING); - RUBY_ASSERT(SYMBOL_P(sym)); + RUBY_ASSERT_BUILTIN_TYPE(str, T_STRING); + RUBY_ASSERT_BUILTIN_TYPE(sym, T_SYMBOL); size_t idx = num / ID_ENTRY_UNIT; @@ -484,10 +484,10 @@ get_id_serial_entry(rb_id_serial_t num, ID id, const enum id_entry_type t) if (result) { switch (t) { case ID_ENTRY_STR: - RUBY_ASSERT(BUILTIN_TYPE(result) == T_STRING); + RUBY_ASSERT_BUILTIN_TYPE(result, T_STRING); break; case ID_ENTRY_SYM: - RUBY_ASSERT(SYMBOL_P(result)); + RUBY_ASSERT_BUILTIN_TYPE(result, T_SYMBOL); break; default: break; @@ -972,11 +972,11 @@ rb_sym2str(VALUE sym) VALUE str; if (DYNAMIC_SYM_P(sym)) { str = RSYMBOL(sym)->fstr; - RUBY_ASSERT(BUILTIN_TYPE(str) == T_STRING); + RUBY_ASSERT_BUILTIN_TYPE(str, T_STRING); } else { str = rb_id2str(STATIC_SYM2ID(sym)); - RUBY_ASSERT(str == 0 || BUILTIN_TYPE(str) == T_STRING); + if (str) RUBY_ASSERT_BUILTIN_TYPE(str, T_STRING); } return str;