ruby-changes:58545
From: Kenichi <ko1@a...>
Date: Sun, 3 Nov 2019 19:03:25 +0900 (JST)
Subject: [ruby-changes:58545] 452bee3ee8 (master): Revert nil error and adding deprecation message
https://git.ruby-lang.org/ruby.git/commit/?id=452bee3ee8 From 452bee3ee8d68059fabd9b1c7a75661b14e3933e Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya <kachick1@g...> Date: Sat, 2 Nov 2019 00:54:36 +0900 Subject: Revert nil error and adding deprecation message diff --git a/re.c b/re.c index e0878ba..6b8eda4 100644 --- a/re.c +++ b/re.c @@ -3323,7 +3323,9 @@ rb_reg_match_m(int argc, VALUE *argv, VALUE re) https://github.com/ruby/ruby/blob/trunk/re.c#L3323 pos = 0; } - str = SYMBOL_P(str) ? rb_sym2str(str) : StringValue(str); + if (NIL_P(str)) { + rb_warn("given argument is nil"); + } pos = reg_match_pos(re, &str, pos); if (pos < 0) { rb_backref_set(Qnil); @@ -3369,6 +3371,10 @@ rb_reg_match_p(VALUE re, VALUE str, long pos) https://github.com/ruby/ruby/blob/trunk/re.c#L3371 const UChar *start, *end; int tmpreg; + if (NIL_P(str)) { + rb_warn("given argument is nil"); + return Qfalse; + } str = SYMBOL_P(str) ? rb_sym2str(str) : StringValue(str); if (pos) { if (pos < 0) { diff --git a/spec/ruby/core/regexp/match_spec.rb b/spec/ruby/core/regexp/match_spec.rb index c117dca..8297be1 100644 --- a/spec/ruby/core/regexp/match_spec.rb +++ b/spec/ruby/core/regexp/match_spec.rb @@ -87,7 +87,7 @@ describe "Regexp#match" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/regexp/match_spec.rb#L87 end end - ruby_version_is ""..."2.7" do + ruby_version_is ""..."3.0" do it "resets $~ if passed nil" do # set $~ /./.match("a") @@ -98,7 +98,13 @@ describe "Regexp#match" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/regexp/match_spec.rb#L98 end end - ruby_version_is "2.7" do + ruby_version_is "2.7"..."3.0" do + it "warns the deprecation when the given argument is nil" do + -> { /foo/.match(nil) }.should complain(/given argument is nil/) + end + end + + ruby_version_is "3.0" do it "raises TypeError when the given argument is nil" do -> { /foo/.match(nil) }.should raise_error(TypeError) end @@ -137,13 +143,19 @@ describe "Regexp#match?" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/regexp/match_spec.rb#L143 /str/i.match?('string', 1).should be_false end - ruby_version_is ""..."2.7" do + ruby_version_is ""..."3.0" do it "returns false when given nil" do /./.match?(nil).should be_false end end - ruby_version_is "2.7" do + ruby_version_is "2.7"..."3.0" do + it "warns the deprecation" do + -> { /./.match?(nil) }.should complain(/given argument is nil/) + end + end + + ruby_version_is "3.0" do it "raises TypeError when given nil" do -> { /./.match?(nil) }.should raise_error(TypeError) end diff --git a/test/ruby/test_regexp.rb b/test/ruby/test_regexp.rb index 03e94a7..6cfd7df 100644 --- a/test/ruby/test_regexp.rb +++ b/test/ruby/test_regexp.rb @@ -539,7 +539,7 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L539 end def test_match - assert_raise(TypeError) { //.match(nil) } + assert_nil(//.match(nil)) assert_equal("abc", /.../.match(:abc)[0]) assert_raise(TypeError) { /.../.match(Object.new)[0] } assert_equal("bc", /../.match('abc', 1)[0]) @@ -561,10 +561,10 @@ class TestRegexp < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_regexp.rb#L561 /backref/ =~ 'backref' # must match here, but not in a separate method, e.g., assert_send, # to check if $~ is affected or not. + assert_equal(false, //.match?(nil)) assert_equal(true, //.match?("")) assert_equal(true, /.../.match?(:abc)) assert_raise(TypeError) { /.../.match?(Object.new) } - assert_raise(TypeError) { //.match?(nil) } assert_equal(true, /b/.match?('abc')) assert_equal(true, /b/.match?('abc', 1)) assert_equal(true, /../.match?('abc', 1)) -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/