From 9bf9de3d9d2f67bc401151fb94abf75b5eff5913 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 17 Dec 2019 14:52:15 +0900 Subject: Made the warning for deprecated constants follow the category flag --- error.c | 22 ++++++++++++++-------- internal.h | 7 +++++++ test/ruby/test_module.rb | 5 +++++ variable.c | 3 ++- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/error.c b/error.c index 9da36ae2e3..4cb17df292 100644 --- a/error.c +++ b/error.c @@ -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; } diff --git a/internal.h b/internal.h index c35e3bdb6f..fc2ff0eaff 100644 --- a/internal.h +++ b/internal.h @@ -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) \ diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb index 3ddfcd9c4f..2e7e5804d0 100644 --- a/test/ruby/test_module.rb +++ b/test/ruby/test_module.rb @@ -28,10 +28,13 @@ class TestModule < Test::Unit::TestCase 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 @@ -1580,6 +1583,8 @@ class TestModule < Test::Unit::TestCase 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 diff --git a/variable.c b/variable.c index 74c4472910..4d5c9a6f54 100644 --- a/variable.c +++ b/variable.c @@ -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)); } -- cgit v1.2.3