diff options
author | Nobuyoshi Nakada <[email protected]> | 2024-04-06 17:45:58 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <[email protected]> | 2024-04-08 11:13:29 +0900 |
commit | 4dd9e5cf7447ec70a55206fd5e1b9e8c79dbba7e (patch) | |
tree | 008d9f18d1f674f4acd1f859e84d252856eafb82 | |
parent | 5d1702e01a36e11b183fe29ce10780a9b1a41cf0 (diff) |
Add builtin type assertion
-rw-r--r-- | include/ruby/assert.h | 11 | ||||
-rw-r--r-- | string.c | 2 | ||||
-rw-r--r-- | symbol.c | 12 |
3 files changed, 18 insertions, 7 deletions
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 @@ -282,6 +282,17 @@ RBIMPL_WARNING_IGNORED(-Wgnu-zero-variadic-macro-arguments) #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. * * @copydetails #RUBY_ASSERT @@ -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; } @@ -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; |