aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorJohn Hawthorn <john@hawthorn.email>2022-07-28 16:41:46 -0700
committerJohn Hawthorn <john@hawthorn.email>2022-08-04 14:48:47 -0700
commit70b60d24b9c3859a859853ddb2e17c603bd3485b (patch)
tree118155ed5c7e4d832ae51b48ca57867e08718ee5 /test
parent1e7a2415a4c69aa64c9c2a561197bf9cfc5a91f8 (diff)
downloadruby-70b60d24b9c3859a859853ddb2e17c603bd3485b.tar.gz
Fix inconsistency with opt_aref_with
opt_aref_with is an optimized instruction for accessing a Hash using a non-frozen string key (ie. from a file without frozen_string_literal). It attempts to avoid allocating the string, and instead silently using a frozen string (hash string keys are always fstrings). Because this is just an optimization, it should be invisible to the user. However, previously this optimization was could be seen via hashes with default procs. For example, previously: h = Hash.new { |h, k| k.frozen? } str = "foo" h[str] # false h["foo"] # true when optimizations enabled This commit checks that the Hash doesn't have a default proc when using opt_aref_with.
Diffstat (limited to 'test')
-rw-r--r--test/ruby/test_hash.rb14
1 files changed, 14 insertions, 0 deletions
diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb
index 91423f81ea..83d16d462e 100644
--- a/test/ruby/test_hash.rb
+++ b/test/ruby/test_hash.rb
@@ -304,6 +304,20 @@ class TestHash < Test::Unit::TestCase
assert_equal before, ObjectSpace.count_objects[:T_STRING]
end
+ def test_AREF_fstring_key_default_proc
+ assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}")
+ begin;
+ h = Hash.new do |h, k|
+ k.frozen?
+ end
+
+ str = "foo"
+ refute str.frozen? # assumes this file is frozen_string_literal: false
+ refute h[str]
+ refute h["foo"]
+ end;
+ end
+
def test_ASET_fstring_key
a, b = {}, {}
assert_equal 1, a["abc"] = 1