Skip to content

Commit 4dd9e5c

Browse files
committed
Add builtin type assertion
1 parent 5d1702e commit 4dd9e5c

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

include/ruby/assert.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,17 @@ RBIMPL_WARNING_IGNORED(-Wgnu-zero-variadic-macro-arguments)
281281
# define RUBY_ASSERT_WHEN(cond, expr) RUBY_ASSERT_MESG_WHEN((cond), (expr), #expr)
282282
#endif
283283

284+
/**
285+
* A variant of #RUBY_ASSERT that asserts when either #RUBY_DEBUG or built-in
286+
* type of `obj` is `type`.
287+
*
288+
* @param obj Object to check its built-in typue.
289+
* @param type Built-in type constant, T_ARRAY, T_STRING, etc.
290+
*/
291+
#define RUBY_ASSERT_BUILTIN_TYPE(obj, type) \
292+
RUBY_ASSERT(RB_TYPE_P(obj, type), \
293+
"Actual type is %s", rb_builtin_type_name(BUILTIN_TYPE(obj)))
294+
284295
/**
285296
* This is either #RUBY_ASSERT or #RBIMPL_ASSUME, depending on #RUBY_DEBUG.
286297
*

string.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11764,7 +11764,7 @@ sym_inspect(VALUE sym)
1176411764
}
1176511765
dest[0] = ':';
1176611766

11767-
RUBY_ASSERT(BUILTIN_TYPE(str) == T_STRING);
11767+
RUBY_ASSERT_BUILTIN_TYPE(str, T_STRING);
1176811768

1176911769
return str;
1177011770
}

symbol.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,8 @@ static void
430430
set_id_entry(rb_symbols_t *symbols, rb_id_serial_t num, VALUE str, VALUE sym)
431431
{
432432
ASSERT_vm_locking();
433-
RUBY_ASSERT(BUILTIN_TYPE(str) == T_STRING);
434-
RUBY_ASSERT(SYMBOL_P(sym));
433+
RUBY_ASSERT_BUILTIN_TYPE(str, T_STRING);
434+
RUBY_ASSERT_BUILTIN_TYPE(sym, T_SYMBOL);
435435

436436
size_t idx = num / ID_ENTRY_UNIT;
437437

@@ -484,10 +484,10 @@ get_id_serial_entry(rb_id_serial_t num, ID id, const enum id_entry_type t)
484484
if (result) {
485485
switch (t) {
486486
case ID_ENTRY_STR:
487-
RUBY_ASSERT(BUILTIN_TYPE(result) == T_STRING);
487+
RUBY_ASSERT_BUILTIN_TYPE(result, T_STRING);
488488
break;
489489
case ID_ENTRY_SYM:
490-
RUBY_ASSERT(SYMBOL_P(result));
490+
RUBY_ASSERT_BUILTIN_TYPE(result, T_SYMBOL);
491491
break;
492492
default:
493493
break;
@@ -972,11 +972,11 @@ rb_sym2str(VALUE sym)
972972
VALUE str;
973973
if (DYNAMIC_SYM_P(sym)) {
974974
str = RSYMBOL(sym)->fstr;
975-
RUBY_ASSERT(BUILTIN_TYPE(str) == T_STRING);
975+
RUBY_ASSERT_BUILTIN_TYPE(str, T_STRING);
976976
}
977977
else {
978978
str = rb_id2str(STATIC_SYM2ID(sym));
979-
RUBY_ASSERT(str == 0 || BUILTIN_TYPE(str) == T_STRING);
979+
if (str) RUBY_ASSERT_BUILTIN_TYPE(str, T_STRING);
980980
}
981981

982982
return str;

0 commit comments

Comments
 (0)