summaryrefslogtreecommitdiff
path: root/test/ruby
diff options
context:
space:
mode:
authorTakashi Kokubun <takashikkbn@gmail.com>2024-11-04 14:43:39 -0800
committerTakashi Kokubun <takashikkbn@gmail.com>2024-11-04 14:43:39 -0800
commit6db39f4677ff50aacfe54bd9dda052c09e1c6ab0 (patch)
tree2b593fef991f691f462c9b735d82bffaf0b74e27 /test/ruby
parentedeb0319f7a95dfe3f9b895bcf32371dd8514726 (diff)
merge revision(s) 29c480dd6fca993590c82078ba797e2c4e876ac7: [Backport #20853]
[Bug #20853] Fix Proc#hash to not change after compaction The hash value of a Proc must remain constant after a compaction, otherwise it may not work as the key in a hash table.
Diffstat (limited to 'test/ruby')
-rw-r--r--test/ruby/test_proc.rb18
1 files changed, 18 insertions, 0 deletions
diff --git a/test/ruby/test_proc.rb b/test/ruby/test_proc.rb
index 8d6ebf5dcb..19fc89357a 100644
--- a/test/ruby/test_proc.rb
+++ b/test/ruby/test_proc.rb
@@ -168,6 +168,24 @@ class TestProc < Test::Unit::TestCase
assert_operator(procs.map(&:hash).uniq.size, :>=, 500)
end
+ def test_hash_does_not_change_after_compaction
+ # [Bug #20853]
+ [
+ "proc {}", # iseq backed proc
+ "{}.to_proc", # ifunc backed proc
+ ":hello.to_proc", # symbol backed proc
+ ].each do |proc|
+ assert_separately([], <<~RUBY)
+ p1 = #{proc}
+ hash = p1.hash
+
+ GC.verify_compaction_references(expand_heap: true, toward: :empty)
+
+ assert_equal(hash, p1.hash, "proc is `#{proc}`")
+ RUBY
+ end
+ end
+
def test_block_par
assert_equal(10, Proc.new{|&b| b.call(10)}.call {|x| x})
assert_equal(12, Proc.new{|a,&b| b.call(a)}.call(12) {|x| x})