[前][次][番号順一覧][スレッド一覧]

ruby-changes:55780

From: Yusuke <ko1@a...>
Date: Thu, 23 May 2019 00:20:16 +0900 (JST)
Subject: [ruby-changes:55780] Yusuke Endoh: d3f1c615c5 (trunk): hash.c (rb_hash_s_create): Reject `Hash[[nil]]`

https://git.ruby-lang.org/ruby.git/commit/?id=d3f1c615c5

From d3f1c615c5b81319e422e9c92e1cb8ba82209fba Mon Sep 17 00:00:00 2001
From: Yusuke Endoh <mame@r...>
Date: Thu, 23 May 2019 00:15:55 +0900
Subject: hash.c (rb_hash_s_create): Reject `Hash[[nil]]`

The behavior of `Hash[[nil]] #=> {}` was a bug until 1.9.3, but had been
remained with a warning because some programs depended upon it.
Now, six years passed.  We can remove the compatibility behavior.
[Bug #7300]

diff --git a/hash.c b/hash.c
index b4b0415..2982afa 100644
--- a/hash.c
+++ b/hash.c
@@ -1656,17 +1656,8 @@ rb_hash_s_create(int argc, VALUE *argv, VALUE klass) https://github.com/ruby/ruby/blob/trunk/hash.c#L1656
 		VALUE key, val = Qnil;
 
 		if (NIL_P(v)) {
-#if 0 /* refix in the next release */
 		    rb_raise(rb_eArgError, "wrong element type %s at %ld (expected array)",
 			     rb_builtin_class_name(e), i);
-
-#else
-		    rb_warn("wrong element type %s at %ld (expected array)",
-			    rb_builtin_class_name(e), i);
-		    rb_warn("ignoring wrong elements is deprecated, remove them explicitly");
-		    rb_warn("this causes ArgumentError in the next release");
-		    continue;
-#endif
 		}
 		switch (RARRAY_LEN(v)) {
 		  default:
diff --git a/spec/ruby/core/hash/constructor_spec.rb b/spec/ruby/core/hash/constructor_spec.rb
index 55681c2..4e02a95 100644
--- a/spec/ruby/core/hash/constructor_spec.rb
+++ b/spec/ruby/core/hash/constructor_spec.rb
@@ -45,10 +45,10 @@ describe "Hash.[]" do https://github.com/ruby/ruby/blob/trunk/spec/ruby/core/hash/constructor_spec.rb#L45
   it "ignores elements that are not arrays" do
     -> {
       Hash[[:a]].should == {}
-    }.should complain(/ignoring wrong elements/)
+    }.should raise_error(ArgumentError)
     -> {
       Hash[[:nil]].should == {}
-    }.should complain(/ignoring wrong elements/)
+    }.should raise_error(ArgumentError)
   end
 
   it "raises an ArgumentError for arrays of more than 2 elements" do
diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb
index c934d10..243cad8 100644
--- a/test/ruby/test_hash.rb
+++ b/test/ruby/test_hash.rb
@@ -149,10 +149,9 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L149
     assert_equal(nil, h['b'])
     assert_equal(300, h['c'])
 
-    h = @cls[[["a", 100], "b", ["c", 300]]]
-    assert_equal(100, h['a'])
-    assert_equal(nil, h['b'])
-    assert_equal(300, h['c'])
+    assert_raise(ArgumentError) do
+      @cls[[["a", 100], "b", ["c", 300]]]
+    end
   end
 
   def test_s_AREF_duplicated_key
@@ -944,8 +943,8 @@ class TestHash < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_hash.rb#L943
 
   def test_create
     assert_equal({1=>2, 3=>4}, @cls[[[1,2],[3,4]]])
-    assert_raise(ArgumentError) { Hash[0, 1, 2] }
-    assert_warning(/wrong element type Integer at 1 /) {@cls[[[1, 2], 3]]}
+    assert_raise(ArgumentError) { @cls[0, 1, 2] }
+    assert_raise(ArgumentError) { @cls[[[0, 1], 2]] }
     bug5406 = '[ruby-core:39945]'
     assert_raise(ArgumentError, bug5406) { @cls[[[1, 2], [3, 4, 5]]] }
     assert_equal({1=>2, 3=>4}, @cls[1,2,3,4])
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]