diff --git a/hash.c b/hash.c index 14f3b038f9..608738aab5 100644 --- a/hash.c +++ b/hash.c @@ -1723,14 +1723,14 @@ tbl_update(VALUE hash, VALUE key, tbl_update_func func, st_data_t optional_arg) .func = func, .hash = hash, .key = key, - .value = (VALUE)optional_arg, + .value = 0 }; int ret = rb_hash_stlike_update(hash, key, tbl_update_modify, (st_data_t)&arg); /* write barrier */ RB_OBJ_WRITTEN(hash, Qundef, arg.key); - RB_OBJ_WRITTEN(hash, Qundef, arg.value); + if (arg.value) RB_OBJ_WRITTEN(hash, Qundef, arg.value); return ret; } diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb index e75cdfe7f9..7b8cf1c6c4 100644 --- a/test/ruby/test_hash.rb +++ b/test/ruby/test_hash.rb @@ -2355,6 +2355,11 @@ class TestHashOnly < Test::Unit::TestCase end end + def test_bug_21357 + h = {x: []}.merge(x: nil) { |_k, v1, _v2| v1 } + assert_equal({x: []}, h) + end + def test_any_hash_fixable 20.times do assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")