Skip to content

Commit

Permalink
Made the warning for deprecated constants follow the category flag
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Dec 19, 2019
1 parent 76035e5 commit 9bf9de3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
22 changes: 14 additions & 8 deletions error.c
Expand Up @@ -129,32 +129,38 @@ rb_syntax_error_append(VALUE exc, VALUE file, int line, int column,
}

static unsigned int warning_disabled_categories;
#define RB_WARN_CATEGORY_DEPRECATED 1

static unsigned int
rb_warning_category_mask(VALUE category)
{
unsigned int mask = 0;
return 1U << rb_warning_category_from_name(category);
}

rb_warning_category_t
rb_warning_category_from_name(VALUE category)
{
rb_warning_category_t cat = RB_WARN_CATEGORY_NONE;
Check_Type(category, T_SYMBOL);
if (category == ID2SYM(rb_intern("deprecated"))) {
mask = RB_WARN_CATEGORY_DEPRECATED;
cat = RB_WARN_CATEGORY_DEPRECATED;
}
else {
rb_raise(rb_eArgError, "unknown category: %"PRIsVALUE, category);
}
return mask;
return cat;
}

static int
rb_warning_category_enabled_p(VALUE category)
MJIT_FUNC_EXPORTED bool
rb_warning_category_enabled_p(rb_warning_category_t category)
{
return !(warning_disabled_categories & rb_warning_category_mask(category));
return !(warning_disabled_categories & (1U << category));
}

static VALUE
rb_warning_s_aref(VALUE mod, VALUE category)
{
if (rb_warning_category_enabled_p(category))
rb_warning_category_t cat = rb_warning_category_from_name(category);
if (rb_warning_category_enabled_p(cat))
return Qtrue;
return Qfalse;
}
Expand Down
7 changes: 7 additions & 0 deletions internal.h
Expand Up @@ -1554,6 +1554,13 @@ PRINTF_ARGS(void rb_sys_enc_warning(rb_encoding *enc, const char *fmt, ...), 2,
PRINTF_ARGS(void rb_syserr_enc_warning(int err, rb_encoding *enc, const char *fmt, ...), 3, 4);
#endif

typedef enum {
RB_WARN_CATEGORY_NONE,
RB_WARN_CATEGORY_DEPRECATED,
} rb_warning_category_t;
rb_warning_category_t rb_warning_category_from_name(VALUE category);
bool rb_warning_category_enabled_p(rb_warning_category_t category);

#define rb_raise_cstr(etype, mesg) \
rb_exc_raise(rb_exc_new_str(etype, rb_str_new_cstr(mesg)))
#define rb_raise_static(etype, mesg) \
Expand Down
5 changes: 5 additions & 0 deletions test/ruby/test_module.rb
Expand Up @@ -28,10 +28,13 @@ def assert_method_not_defined?(klass, mid, message="")
def setup
@verbose = $VERBOSE
$VERBOSE = nil
@deprecated = Warning[:deprecated]
Warning[:deprecated] = true
end

def teardown
$VERBOSE = @verbose
Warning[:deprecated] = @deprecated
end

def test_LT_0
Expand Down Expand Up @@ -1580,6 +1583,8 @@ def test_deprecate_constant
assert_warn(/#{c}::FOO is deprecated/) {Class.new(c)::FOO}
bug12382 = '[ruby-core:75505] [Bug #12382]'
assert_warn(/deprecated/, bug12382) {c.class_eval "FOO"}
Warning[:deprecated] = false
assert_warn('') {c::FOO}
end

NIL = nil
Expand Down
3 changes: 2 additions & 1 deletion variable.c
Expand Up @@ -2302,7 +2302,8 @@ rb_autoload_at_p(VALUE mod, ID id, int recur)
MJIT_FUNC_EXPORTED void
rb_const_warn_if_deprecated(const rb_const_entry_t *ce, VALUE klass, ID id)
{
if (RB_CONST_DEPRECATED_P(ce)) {
if (RB_CONST_DEPRECATED_P(ce) &&
rb_warning_category_enabled_p(RB_WARN_CATEGORY_DEPRECATED)) {
if (klass == rb_cObject) {
rb_warn("constant ::%"PRIsVALUE" is deprecated", QUOTE_ID(id));
}
Expand Down

0 comments on commit 9bf9de3

Please sign in to comment.