1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Made the warning for deprecated constants follow the category flag

This commit is contained in:
Nobuyoshi Nakada 2019-12-17 14:52:15 +09:00
parent 76035e5bb6
commit 9bf9de3d9d
No known key found for this signature in database
GPG key ID: 4BC7D6DF58D8DF60
4 changed files with 28 additions and 9 deletions

22
error.c
View file

@ -129,32 +129,38 @@ rb_syntax_error_append(VALUE exc, VALUE file, int line, int column,
} }
static unsigned int warning_disabled_categories; static unsigned int warning_disabled_categories;
#define RB_WARN_CATEGORY_DEPRECATED 1
static unsigned int static unsigned int
rb_warning_category_mask(VALUE category) 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); Check_Type(category, T_SYMBOL);
if (category == ID2SYM(rb_intern("deprecated"))) { if (category == ID2SYM(rb_intern("deprecated"))) {
mask = RB_WARN_CATEGORY_DEPRECATED; cat = RB_WARN_CATEGORY_DEPRECATED;
} }
else { else {
rb_raise(rb_eArgError, "unknown category: %"PRIsVALUE, category); rb_raise(rb_eArgError, "unknown category: %"PRIsVALUE, category);
} }
return mask; return cat;
} }
static int MJIT_FUNC_EXPORTED bool
rb_warning_category_enabled_p(VALUE category) 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 static VALUE
rb_warning_s_aref(VALUE mod, VALUE category) 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 Qtrue;
return Qfalse; return Qfalse;
} }

View file

@ -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); PRINTF_ARGS(void rb_syserr_enc_warning(int err, rb_encoding *enc, const char *fmt, ...), 3, 4);
#endif #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) \ #define rb_raise_cstr(etype, mesg) \
rb_exc_raise(rb_exc_new_str(etype, rb_str_new_cstr(mesg))) rb_exc_raise(rb_exc_new_str(etype, rb_str_new_cstr(mesg)))
#define rb_raise_static(etype, mesg) \ #define rb_raise_static(etype, mesg) \

View file

@ -28,10 +28,13 @@ class TestModule < Test::Unit::TestCase
def setup def setup
@verbose = $VERBOSE @verbose = $VERBOSE
$VERBOSE = nil $VERBOSE = nil
@deprecated = Warning[:deprecated]
Warning[:deprecated] = true
end end
def teardown def teardown
$VERBOSE = @verbose $VERBOSE = @verbose
Warning[:deprecated] = @deprecated
end end
def test_LT_0 def test_LT_0
@ -1580,6 +1583,8 @@ class TestModule < Test::Unit::TestCase
assert_warn(/#{c}::FOO is deprecated/) {Class.new(c)::FOO} assert_warn(/#{c}::FOO is deprecated/) {Class.new(c)::FOO}
bug12382 = '[ruby-core:75505] [Bug #12382]' bug12382 = '[ruby-core:75505] [Bug #12382]'
assert_warn(/deprecated/, bug12382) {c.class_eval "FOO"} assert_warn(/deprecated/, bug12382) {c.class_eval "FOO"}
Warning[:deprecated] = false
assert_warn('') {c::FOO}
end end
NIL = nil NIL = nil

View file

@ -2302,7 +2302,8 @@ rb_autoload_at_p(VALUE mod, ID id, int recur)
MJIT_FUNC_EXPORTED void MJIT_FUNC_EXPORTED void
rb_const_warn_if_deprecated(const rb_const_entry_t *ce, VALUE klass, ID id) 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) { if (klass == rb_cObject) {
rb_warn("constant ::%"PRIsVALUE" is deprecated", QUOTE_ID(id)); rb_warn("constant ::%"PRIsVALUE" is deprecated", QUOTE_ID(id));
} }