Skip to content

Commit 6ced55b

Browse files
committed
Make warning_categories a map of category symbols to category numbers
Use this to simplify rb_warning_category_from_name. This also adds support for using the :experimental category in Kernel#warn and Warning.warn.
1 parent 52fb696 commit 6ced55b

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

error.c

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
VALUE rb_iseqw_local_variables(VALUE iseqval);
6464
VALUE rb_iseqw_new(const rb_iseq_t *);
6565
int rb_str_end_with_asciichar(VALUE str, int c);
66+
VALUE rb_ident_hash_new(void);
6667

6768
long rb_backtrace_length_limit = -1;
6869
VALUE rb_eEAGAIN;
@@ -161,18 +162,13 @@ rb_warning_category_mask(VALUE category)
161162
rb_warning_category_t
162163
rb_warning_category_from_name(VALUE category)
163164
{
164-
rb_warning_category_t cat = RB_WARN_CATEGORY_NONE;
165+
VALUE cat_value;
165166
Check_Type(category, T_SYMBOL);
166-
if (category == ID2SYM(id_deprecated)) {
167-
cat = RB_WARN_CATEGORY_DEPRECATED;
168-
}
169-
else if (category == ID2SYM(id_experimental)) {
170-
cat = RB_WARN_CATEGORY_EXPERIMENTAL;
171-
}
172-
else {
167+
cat_value = rb_hash_aref(warning_categories, category);
168+
if (cat_value == Qnil) {
173169
rb_raise(rb_eArgError, "unknown category: %"PRIsVALUE, category);
174170
}
175-
return cat;
171+
return NUM2INT(cat_value);
176172
}
177173

178174
void
@@ -318,7 +314,7 @@ rb_warn_category(VALUE str, VALUE category)
318314
{
319315
if (category != Qnil) {
320316
category = rb_to_symbol_type(category);
321-
if (rb_hash_aref(warning_categories, category) != Qtrue) {
317+
if (!RTEST(rb_hash_aref(warning_categories, category))) {
322318
rb_raise(rb_eArgError, "invalid warning category used: %s", rb_id2name(SYM2ID(category)));
323319
}
324320
}
@@ -2836,16 +2832,17 @@ Init_Exception(void)
28362832

28372833
sym_category = ID2SYM(id_category);
28382834

2839-
warning_categories = rb_hash_new();
2835+
warning_categories = rb_ident_hash_new();
28402836
rb_gc_register_mark_object(warning_categories);
2841-
rb_hash_aset(warning_categories, ID2SYM(rb_intern_const("deprecated")), Qtrue);
2837+
rb_hash_aset(warning_categories, ID2SYM(id_deprecated), INT2NUM(RB_WARN_CATEGORY_DEPRECATED));
2838+
rb_hash_aset(warning_categories, ID2SYM(id_experimental), INT2NUM(RB_WARN_CATEGORY_EXPERIMENTAL));
28422839
rb_obj_freeze(warning_categories);
28432840

2844-
warning_category_t_map = rb_hash_new();
2841+
warning_category_t_map = rb_ident_hash_new();
28452842
rb_gc_register_mark_object(warning_category_t_map);
28462843
rb_hash_aset(warning_category_t_map, INT2NUM(RB_WARN_CATEGORY_NONE), Qnil);
2847-
rb_hash_aset(warning_category_t_map, INT2NUM(RB_WARN_CATEGORY_DEPRECATED), ID2SYM(rb_intern_const("deprecated")));
2848-
rb_hash_aset(warning_category_t_map, INT2NUM(RB_WARN_CATEGORY_EXPERIMENTAL), ID2SYM(rb_intern_const("experimental")));
2844+
rb_hash_aset(warning_category_t_map, INT2NUM(RB_WARN_CATEGORY_DEPRECATED), ID2SYM(id_deprecated));
2845+
rb_hash_aset(warning_category_t_map, INT2NUM(RB_WARN_CATEGORY_EXPERIMENTAL), ID2SYM(id_experimental));
28492846
rb_obj_freeze(warning_category_t_map);
28502847
}
28512848

0 commit comments

Comments
 (0)