Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add builtin type assertion #10471

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions include/ruby/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion string.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions symbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down