Commit 07c9db07

07c9db0750fbMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · +1 added

Description

Add regression test for String#replace/prepend/insert/clear on locals

These four methods already mutate a string held in a local variable: the analyzer widens such a local to a mutable_str when it is the receiver of replace/prepend/insert/concat/<< (the #740/#741 promotion), and clear reassigns the local to "". So s = "abc"; s.replace("xyz") and the prepend/insert/clear forms all match CRuby rather than being no-ops, as does the String.new receiver path.

The gap reported in the issue (no dispatch through sp_String_replace) is closed; lock the behaviour in with a test covering literal-local and String.new receivers plus negative-index insert.

Closes #1082

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
07c9db0750fb
Parent
ddc868e21a25

test/string_replace_prepend_insert_clear.rbaddedView file

1+# String#replace / prepend / insert / clear mutate a string held in a
2+# local variable. The analyzer widens such a local to a mutable_str
3+# (or, for clear, reassigns it), so these match CRuby instead of being
4+# no-ops. Both string-literal locals and String.new receivers work.
5+s = "abc"
6+s.replace("xyz")
7+p s
8+
9+s2 = "world"
10+s2.prepend("hello ")
11+p s2
12+
13+s3 = "hlo"
14+s3.insert(1, "el")
15+p s3
16+
17+s4 = "hello"
18+s4.clear
19+p s4.length
20+
21+# String.new receivers mutate through the same paths.
22+m = String.new("abc")
23+m.replace("xyz")
24+m.prepend("> ")
25+p m
26+
27+m2 = String.new("hello")
28+m2.clear
29+p m2.length
30+
31+# insert with a negative index counts from the end.
32+s5 = "abcd"
33+s5.insert(-2, "X")
34+p s5