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]
This commit is contained in:
Yusuke Endoh 2019-05-23 00:15:55 +09:00
Родитель 4d62296948
Коммит d3f1c615c5
3 изменённых файлов: 7 добавлений и 17 удалений

9
hash.c
Просмотреть файл

@ -1656,17 +1656,8 @@ rb_hash_s_create(int argc, VALUE *argv, VALUE klass)
VALUE key, val = Qnil; VALUE key, val = Qnil;
if (NIL_P(v)) { if (NIL_P(v)) {
#if 0 /* refix in the next release */
rb_raise(rb_eArgError, "wrong element type %s at %ld (expected array)", rb_raise(rb_eArgError, "wrong element type %s at %ld (expected array)",
rb_builtin_class_name(e), i); 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)) { switch (RARRAY_LEN(v)) {
default: default:

Просмотреть файл

@ -45,10 +45,10 @@ describe "Hash.[]" do
it "ignores elements that are not arrays" do it "ignores elements that are not arrays" do
-> { -> {
Hash[[:a]].should == {} Hash[[:a]].should == {}
}.should complain(/ignoring wrong elements/) }.should raise_error(ArgumentError)
-> { -> {
Hash[[:nil]].should == {} Hash[[:nil]].should == {}
}.should complain(/ignoring wrong elements/) }.should raise_error(ArgumentError)
end end
it "raises an ArgumentError for arrays of more than 2 elements" do it "raises an ArgumentError for arrays of more than 2 elements" do

Просмотреть файл

@ -149,10 +149,9 @@ class TestHash < Test::Unit::TestCase
assert_equal(nil, h['b']) assert_equal(nil, h['b'])
assert_equal(300, h['c']) assert_equal(300, h['c'])
h = @cls[[["a", 100], "b", ["c", 300]]] assert_raise(ArgumentError) do
assert_equal(100, h['a']) @cls[[["a", 100], "b", ["c", 300]]]
assert_equal(nil, h['b']) end
assert_equal(300, h['c'])
end end
def test_s_AREF_duplicated_key def test_s_AREF_duplicated_key
@ -944,8 +943,8 @@ class TestHash < Test::Unit::TestCase
def test_create def test_create
assert_equal({1=>2, 3=>4}, @cls[[[1,2],[3,4]]]) assert_equal({1=>2, 3=>4}, @cls[[[1,2],[3,4]]])
assert_raise(ArgumentError) { Hash[0, 1, 2] } assert_raise(ArgumentError) { @cls[0, 1, 2] }
assert_warning(/wrong element type Integer at 1 /) {@cls[[[1, 2], 3]]} assert_raise(ArgumentError) { @cls[[[0, 1], 2]] }
bug5406 = '[ruby-core:39945]' bug5406 = '[ruby-core:39945]'
assert_raise(ArgumentError, bug5406) { @cls[[[1, 2], [3, 4, 5]]] } assert_raise(ArgumentError, bug5406) { @cls[[[1, 2], [3, 4, 5]]] }
assert_equal({1=>2, 3=>4}, @cls[1,2,3,4]) assert_equal({1=>2, 3=>4}, @cls[1,2,3,4])