From 996af2ce086249e904b2ce95ab2fcd1de7d757be Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Mon, 31 Aug 2020 14:58:31 +0900 Subject: Disable deprecation warning by the default [Feature #16345] And `-w` option turns it on. --- error.c | 4 +- internal/error.h | 1 + ruby.c | 12 ++++ spec/ruby/core/data/constants_spec.rb | 16 +++-- spec/ruby/core/env/index_spec.rb | 14 ++-- spec/ruby/core/integer/constants_spec.rb | 32 ++++----- spec/ruby/core/kernel/match_spec.rb | 2 +- spec/ruby/core/module/deprecate_constant_spec.rb | 10 +++ spec/ruby/language/predefined_spec.rb | 4 +- .../library/net/http/HTTPServerException_spec.rb | 2 +- test/ruby/test_argf.rb | 4 -- test/ruby/test_enumerator.rb | 1 - test/ruby/test_io.rb | 77 ---------------------- test/ruby/test_lambda.rb | 6 -- test/ruby/test_module.rb | 40 ++++++----- test/ruby/test_object.rb | 9 --- test/ruby/test_rubyoptions.rb | 7 ++ test/ruby/test_string.rb | 7 -- tool/lib/envutil.rb | 4 +- 19 files changed, 98 insertions(+), 154 deletions(-) diff --git a/error.c b/error.c index 6a9aa2f2c3..9f8cdf8fe6 100644 --- a/error.c +++ b/error.c @@ -142,7 +142,9 @@ rb_syntax_error_append(VALUE exc, VALUE file, int line, int column, return exc; } -static unsigned int warning_disabled_categories; +static unsigned int warning_disabled_categories = ( + 1U << RB_WARN_CATEGORY_DEPRECATED | + 0); static unsigned int rb_warning_category_mask(VALUE category) diff --git a/internal/error.h b/internal/error.h index ff60d0075d..cf6495fbd0 100644 --- a/internal/error.h +++ b/internal/error.h @@ -44,6 +44,7 @@ typedef enum { RB_WARN_CATEGORY_NONE, RB_WARN_CATEGORY_DEPRECATED, RB_WARN_CATEGORY_EXPERIMENTAL, + RB_WARN_CATEGORY_ALL_BITS = 0x6, /* no RB_WARN_CATEGORY_NONE bit */ } rb_warning_category_t; extern long rb_backtrace_length_limit; diff --git a/ruby.c b/ruby.c index cfde2ffa93..9ca980dfbd 100644 --- a/ruby.c +++ b/ruby.c @@ -1109,6 +1109,7 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt) warning = 1; ruby_verbose = Qtrue; } + FEATURE_SET(opt->warn, RB_WARN_CATEGORY_ALL_BITS); s++; goto reswitch; @@ -1155,6 +1156,17 @@ proc_options(long argc, char **argv, ruby_cmdline_options_t *opt, int envopt) } } warning = 1; + switch (v) { + case 0: + FEATURE_SET_TO(opt->warn, RB_WARN_CATEGORY_ALL_BITS, 0); + break; + case 1: + FEATURE_SET_TO(opt->warn, 1U << RB_WARN_CATEGORY_DEPRECATED, 0); + break; + default: + FEATURE_SET(opt->warn, RB_WARN_CATEGORY_ALL_BITS); + break; + } } goto reswitch; diff --git a/spec/ruby/core/data/constants_spec.rb b/spec/ruby/core/data/constants_spec.rb index 0e47a82e26..18f6d4291c 100644 --- a/spec/ruby/core/data/constants_spec.rb +++ b/spec/ruby/core/data/constants_spec.rb @@ -1,13 +1,15 @@ require_relative '../../spec_helper' -describe "Data" do - it "is a subclass of Object" do - suppress_warning do - Data.superclass.should == Object +ruby_version_is ""..."3.0" do + describe "Data" do + it "is a subclass of Object" do + suppress_warning do + Data.superclass.should == Object + end end - end - it "is deprecated" do - -> { Data }.should complain(/constant ::Data is deprecated/) + it "is deprecated" do + -> { Data }.should complain(/constant ::Data is deprecated/) + end end end diff --git a/spec/ruby/core/env/index_spec.rb b/spec/ruby/core/env/index_spec.rb index 43875f5a50..a0c90f8eb2 100644 --- a/spec/ruby/core/env/index_spec.rb +++ b/spec/ruby/core/env/index_spec.rb @@ -1,12 +1,14 @@ require_relative '../../spec_helper' require_relative 'shared/key' -describe "ENV.index" do - it_behaves_like :env_key, :index +ruby_version_is ""..."3.0" do + describe "ENV.index" do + it_behaves_like :env_key, :index - it "warns about deprecation" do - -> do - ENV.index("foo") - end.should complain(/warning: ENV.index is deprecated; use ENV.key/) + it "warns about deprecation" do + -> do + ENV.index("foo") + end.should complain(/warning: ENV.index is deprecated; use ENV.key/) + end end end diff --git a/spec/ruby/core/integer/constants_spec.rb b/spec/ruby/core/integer/constants_spec.rb index 3b8b01e330..35601f82b9 100644 --- a/spec/ruby/core/integer/constants_spec.rb +++ b/spec/ruby/core/integer/constants_spec.rb @@ -1,25 +1,27 @@ require_relative '../../spec_helper' -describe "Fixnum" do - it "is unified into Integer" do - suppress_warning do - Fixnum.should equal(Integer) +ruby_version_is ""..."3.0" do + describe "Fixnum" do + it "is unified into Integer" do + suppress_warning do + Fixnum.should equal(Integer) + end end - end - it "is deprecated" do - -> { Fixnum }.should complain(/constant ::Fixnum is deprecated/) + it "is deprecated" do + -> { Fixnum }.should complain(/constant ::Fixnum is deprecated/) + end end -end -describe "Bignum" do - it "is unified into Integer" do - suppress_warning do - Bignum.should equal(Integer) + describe "Bignum" do + it "is unified into Integer" do + suppress_warning do + Bignum.should equal(Integer) + end end - end - it "is deprecated" do - -> { Bignum }.should complain(/constant ::Bignum is deprecated/) + it "is deprecated" do + -> { Bignum }.should complain(/constant ::Bignum is deprecated/) + end end end diff --git a/spec/ruby/core/kernel/match_spec.rb b/spec/ruby/core/kernel/match_spec.rb index 6dc1eb7de8..e8ef320d6f 100644 --- a/spec/ruby/core/kernel/match_spec.rb +++ b/spec/ruby/core/kernel/match_spec.rb @@ -14,7 +14,7 @@ describe "Kernel#=~" do end end - ruby_version_is "2.6" do + ruby_version_is "2.6"..."3.0" do it "is deprecated" do -> do Object.new =~ /regexp/ diff --git a/spec/ruby/core/module/deprecate_constant_spec.rb b/spec/ruby/core/module/deprecate_constant_spec.rb index 7bcced981b..6a8086bc8f 100644 --- a/spec/ruby/core/module/deprecate_constant_spec.rb +++ b/spec/ruby/core/module/deprecate_constant_spec.rb @@ -10,6 +10,16 @@ describe "Module#deprecate_constant" do @module.private_constant :PRIVATE @module.deprecate_constant :PRIVATE @pattern = /deprecated/ + if Warning.respond_to?(:[]) + @deprecated = Warning[:deprecated] + Warning[:deprecated] = true + end + end + + after :each do + if Warning.respond_to?(:[]) + Warning[:deprecated] = @deprecated + end end describe "when accessing the deprecated module" do diff --git a/spec/ruby/language/predefined_spec.rb b/spec/ruby/language/predefined_spec.rb index 5ce4e77906..cb0462731b 100644 --- a/spec/ruby/language/predefined_spec.rb +++ b/spec/ruby/language/predefined_spec.rb @@ -654,7 +654,7 @@ describe "Predefined global $," do -> { $, = Object.new }.should raise_error(TypeError) end - ruby_version_is "2.7" do + ruby_version_is "2.7"..."3.0" do it "warns if assigned non-nil" do -> { $, = "_" }.should complain(/warning: `\$,' is deprecated/) end @@ -693,7 +693,7 @@ describe "Predefined global $;" do $; = nil end - ruby_version_is "2.7" do + ruby_version_is "2.7"..."3.0" do it "warns if assigned non-nil" do -> { $; = "_" }.should complain(/warning: `\$;' is deprecated/) end diff --git a/spec/ruby/library/net/http/HTTPServerException_spec.rb b/spec/ruby/library/net/http/HTTPServerException_spec.rb index 87841ab499..6800c625f7 100644 --- a/spec/ruby/library/net/http/HTTPServerException_spec.rb +++ b/spec/ruby/library/net/http/HTTPServerException_spec.rb @@ -13,7 +13,7 @@ ruby_version_is ""..."2.6" do end end -ruby_version_is "2.6" do +ruby_version_is "2.6"..."3.0" do describe "Net::HTTPServerException" do it "is a subclass of Net::ProtoServerError and is warned as deprecated" do -> { Net::HTTPServerException.should < Net::ProtoServerError }.should complain(/warning: constant Net::HTTPServerException is deprecated/) diff --git a/test/ruby/test_argf.rb b/test/ruby/test_argf.rb index 4734d5b3ae..e558f7648d 100644 --- a/test/ruby/test_argf.rb +++ b/test/ruby/test_argf.rb @@ -1006,7 +1006,6 @@ class TestArgf < Test::Unit::TestCase ARGF.lines {|l| s << l } p s }; - assert_match(/deprecated/, f.gets) assert_equal("[\"1\\n\", \"2\\n\", \"3\\n\", \"4\\n\", \"5\\n\", \"6\\n\"]\n", f.read) end end @@ -1017,7 +1016,6 @@ class TestArgf < Test::Unit::TestCase $stderr = $stdout print Marshal.dump(ARGF.bytes.to_a) }; - assert_match(/deprecated/, f.gets) assert_equal([49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10], Marshal.load(f.read)) end end @@ -1028,7 +1026,6 @@ class TestArgf < Test::Unit::TestCase $stderr = $stdout print [Marshal.dump(ARGF.chars.to_a)].pack('m') }; - assert_match(/deprecated/, f.gets) assert_equal(["1", "\n", "2", "\n", "3", "\n", "4", "\n", "5", "\n", "6", "\n"], Marshal.load(f.read.unpack('m').first)) end end @@ -1039,7 +1036,6 @@ class TestArgf < Test::Unit::TestCase $stderr = $stdout print Marshal.dump(ARGF.codepoints.to_a) }; - assert_match(/deprecated/, f.gets) assert_equal([49, 10, 50, 10, 51, 10, 52, 10, 53, 10, 54, 10], Marshal.load(f.read)) end end diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb index 75cf1aeec6..b619150571 100644 --- a/test/ruby/test_enumerator.rb +++ b/test/ruby/test_enumerator.rb @@ -72,7 +72,6 @@ class TestEnumerator < Test::Unit::TestCase _, err = capture_io do assert_equal([1, 2, 3], Enumerator.new(@obj, :foo, 1, 2, 3).to_a) end - assert_match 'Enumerator.new without a block is deprecated', err assert_equal([1, 2, 3], Enumerator.new { |y| i = 0; loop { y << (i += 1) } }.take(3)) assert_raise(ArgumentError) { Enumerator.new } diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index fafb082154..7b1ddce78b 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -418,19 +418,6 @@ class TestIO < Test::Unit::TestCase } end - def test_codepoints - make_tempfile {|t| - bug2959 = '[ruby-core:28650]' - a = "" - File.open(t, 'rt') {|f| - assert_warn(/deprecated/) { - f.codepoints {|c| a << c} - } - } - assert_equal("foo\nbar\nbaz\n", a, bug2959) - } - end - def test_rubydev33072 t = make_tempfile path = t.path @@ -1835,70 +1822,6 @@ class TestIO < Test::Unit::TestCase end) end - def test_lines - verbose, $VERBOSE = $VERBOSE, nil - pipe(proc do |w| - w.puts "foo" - w.puts "bar" - w.puts "baz" - w.close - end, proc do |r| - e = nil - assert_warn(/deprecated/) { - e = r.lines - } - assert_equal("foo\n", e.next) - assert_equal("bar\n", e.next) - assert_equal("baz\n", e.next) - assert_raise(StopIteration) { e.next } - end) - ensure - $VERBOSE = verbose - end - - def test_bytes - verbose, $VERBOSE = $VERBOSE, nil - pipe(proc do |w| - w.binmode - w.puts "foo" - w.puts "bar" - w.puts "baz" - w.close - end, proc do |r| - e = nil - assert_warn(/deprecated/) { - e = r.bytes - } - (%w(f o o) + ["\n"] + %w(b a r) + ["\n"] + %w(b a z) + ["\n"]).each do |c| - assert_equal(c.ord, e.next) - end - assert_raise(StopIteration) { e.next } - end) - ensure - $VERBOSE = verbose - end - - def test_chars - verbose, $VERBOSE = $VERBOSE, nil - pipe(proc do |w| - w.puts "foo" - w.puts "bar" - w.puts "baz" - w.close - end, proc do |r| - e = nil - assert_warn(/deprecated/) { - e = r.chars - } - (%w(f o o) + ["\n"] + %w(b a r) + ["\n"] + %w(b a z) + ["\n"]).each do |c| - assert_equal(c, e.next) - end - assert_raise(StopIteration) { e.next } - end) - ensure - $VERBOSE = verbose - end - def test_readbyte pipe(proc do |w| w.binmode diff --git a/test/ruby/test_lambda.rb b/test/ruby/test_lambda.rb index 75362e2796..03b501a6c9 100644 --- a/test/ruby/test_lambda.rb +++ b/test/ruby/test_lambda.rb @@ -74,12 +74,6 @@ class TestLambdaParameters < Test::Unit::TestCase assert_raise(ArgumentError, bug9605) {proc(&plus).call [1,2]} end - def test_warning_for_non_literal_blocks - assert_warn(/lambda without a literal block/, '[ruby-core:93482] [Feature #15973]') do - lambda(&:symbol) - end - end - def pass_along(&block) lambda(&block) end diff --git a/test/ruby/test_module.rb b/test/ruby/test_module.rb index 94e415b08c..3003a743d1 100644 --- a/test/ruby/test_module.rb +++ b/test/ruby/test_module.rb @@ -1755,23 +1755,31 @@ class TestModule < Test::Unit::TestCase c = Class.new c.const_set(:FOO, "foo") c.deprecate_constant(:FOO) - assert_warn(/deprecated/) {c::FOO} - assert_warn(/#{c}::FOO is deprecated/) {Class.new(c)::FOO} + assert_warn(/deprecated/) do + Warning[:deprecated] = true + c::FOO + end + assert_warn(/#{c}::FOO is deprecated/) do + Warning[:deprecated] = true + Class.new(c)::FOO + end bug12382 = '[ruby-core:75505] [Bug #12382]' - assert_warn(/deprecated/, bug12382) {c.class_eval "FOO"} - Warning[:deprecated] = false - assert_warn('') {c::FOO} - end - - NIL = nil - FALSE = false - deprecate_constant(:NIL, :FALSE) - - def test_deprecate_nil_constant - w = EnvUtil.verbose_warning {2.times {FALSE}} - assert_equal(1, w.scan("::FALSE").size, w) - w = EnvUtil.verbose_warning {2.times {NIL}} - assert_equal(1, w.scan("::NIL").size, w) + assert_warn(/deprecated/, bug12382) do + Warning[:deprecated] = true + c.class_eval "FOO" + end + assert_warn('') do + Warning[:deprecated] = false + c::FOO + end + assert_warn('') do + Warning[:deprecated] = false + Class.new(c)::FOO + end + assert_warn('') do + Warning[:deprecated] = false + c.class_eval "FOO" + end end def test_constants_with_private_constant diff --git a/test/ruby/test_object.rb b/test/ruby/test_object.rb index 3aa0a1b652..782ae6ac72 100644 --- a/test/ruby/test_object.rb +++ b/test/ruby/test_object.rb @@ -990,13 +990,4 @@ class TestObject < Test::Unit::TestCase end EOS end - - def test_matcher - assert_warning(/deprecated Object#=~ is called on Object/) do - assert_equal(Object.new =~ 42, nil) - end - assert_warning(/deprecated Object#=~ is called on Array/) do - assert_equal([] =~ 42, nil) - end - end end diff --git a/test/ruby/test_rubyoptions.rb b/test/ruby/test_rubyoptions.rb index 7101175568..6ca8dbea33 100644 --- a/test/ruby/test_rubyoptions.rb +++ b/test/ruby/test_rubyoptions.rb @@ -80,6 +80,9 @@ class TestRubyOptions < Test::Unit::TestCase assert_in_out_err(%w(-W:experimental -e) + ['p Warning[:experimental]'], "", %w(true), []) assert_in_out_err(%w(-W:no-experimental -e) + ['p Warning[:experimental]'], "", %w(false), []) assert_in_out_err(%w(-W:qux), "", [], /unknown warning category: `qux'/) + assert_in_out_err(%w(-w -e) + ['p Warning[:deprecated]'], "", %w(true), []) + assert_in_out_err(%w(-W -e) + ['p Warning[:deprecated]'], "", %w(true), []) + assert_in_out_err(%w(-e) + ['p Warning[:deprecated]'], "", %w(false), []) ensure ENV['RUBYOPT'] = save_rubyopt end @@ -333,6 +336,10 @@ class TestRubyOptions < Test::Unit::TestCase assert_in_out_err(%w(), "p $VERBOSE", ["true"]) assert_in_out_err(%w(-W1), "p $VERBOSE", ["false"]) assert_in_out_err(%w(-W0), "p $VERBOSE", ["nil"]) + assert_in_out_err(%w(), "p Warning[:deprecated]", ["true"]) + assert_in_out_err(%w(-W0), "p Warning[:deprecated]", ["false"]) + assert_in_out_err(%w(-W1), "p Warning[:deprecated]", ["false"]) + assert_in_out_err(%w(-W2), "p Warning[:deprecated]", ["true"]) ENV['RUBYOPT'] = '-W:deprecated' assert_in_out_err(%w(), "p Warning[:deprecated]", ["true"]) ENV['RUBYOPT'] = '-W:no-deprecated' diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index fde1c9cdc5..810c700c8c 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -1764,13 +1764,6 @@ CODE GC.start assert_equal([], "".split, bug) end; - - begin - fs = $; - assert_warn(/`\$;' is deprecated/) {$; = " "} - ensure - EnvUtil.suppress_warning {$; = fs} - end end def test_split_encoding diff --git a/tool/lib/envutil.rb b/tool/lib/envutil.rb index 860ab30689..d9350395fe 100644 --- a/tool/lib/envutil.rb +++ b/tool/lib/envutil.rb @@ -47,12 +47,13 @@ module EnvUtil class << self attr_accessor :timeout_scale attr_reader :original_internal_encoding, :original_external_encoding, - :original_verbose + :original_verbose, :original_warning def capture_global_values @original_internal_encoding = Encoding.default_internal @original_external_encoding = Encoding.default_external @original_verbose = $VERBOSE + @original_warning = %i[deprecated experimental].to_h {|i| [i, Warning[i]]} end end @@ -209,6 +210,7 @@ module EnvUtil ensure stderr, $stderr = $stderr, stderr $VERBOSE = EnvUtil.original_verbose + EnvUtil.original_warning.each {|i, v| Warning[i] = v} end module_function :verbose_warning -- cgit v1.2.1