Commit 6ce9b20f

6ce9b20fd6deMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

codegen: bang String methods mutate mutable_str / raise on frozen

String#chomp! / chop! / strip! / lstrip! / rstrip! / upcase! / downcase! on a mutable_str (String.new("x")) now mutate in place via sp_String_replace + the non-bang helper. Pre-fix chomp! was aliased to chomp (return new value) and silently dropped the side effect; the others fell through to the unresolved-call path.

On a frozen string literal the same names raise FrozenError (routed through the existing #886 list, now extended with chomp!).

bundle_array_e test updated: "ab\n".chomp! == "ab" would now raise FrozenError; the test exercises the mutable_str case instead.

Refs #859

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

Details

Commit
6ce9b20fd6de
Parent
53a523ba0225

spinel_codegen.rbmodifiedView file

@@ -15994,6 +15994,22 @@
1599415994 helper_ms = (mname == "prepend") ? "sp_String_prepend" : "sp_String_append"
1599515995 return "(" + helper_ms + "(" + rc + ", " + arg0_ms + "), " + rc + ")"
1599615996 end
15997+ # Issue #859: chomp! on mutable_str mutates in place and
15998+ # returns the receiver (CRuby returns nil if no change; spinel
15999+ # always returns self -- close-enough subset behaviour). Same
16000+ # shape for chop! / strip! / lstrip! / rstrip!.
16001+ if mname == "chomp!" || mname == "chop!" || mname == "strip!" || mname == "lstrip!" || mname == "rstrip!" || mname == "upcase!" || mname == "downcase!"
16002+ helper_bang = {
16003+ "chomp!" => "sp_str_chomp",
16004+ "chop!" => "sp_str_chop",
16005+ "strip!" => "sp_str_strip",
16006+ "lstrip!" => "sp_str_lstrip",
16007+ "rstrip!" => "sp_str_rstrip",
16008+ "upcase!" => "sp_str_upcase",
16009+ "downcase!" => "sp_str_downcase",
16010+ }[mname]
16011+ return "(sp_String_replace(" + rc + ", " + helper_bang + "(" + rc + "->data)), " + rc + ")"
16012+ end
1599716013 if mname == "insert"
1599816014 args_id_ms = @nd_arguments[nid]
1599916015 if args_id_ms >= 0
@@ -19041,16 +19057,10 @@
1904119057 end
1904219058
1904319059 def compile_string_method_expr(nid, mname, rc)
19044- # String#chomp! alias for chomp. Returns the chomped string;
19045- # the Ruby `nil if no change` mutator semantic isn't preserved
19046- # (a documented quirk -- spinel strings are immutable so there's
19047- # nothing to mutate in place). Recurse so the local `mname` isn't
19048- # reassigned (writing back to a parameter triggers a body-usage
19049- # widening pass that lands warn_unresolved_call's mname as poly
19050- # in the bootstrap build). Issue #619 puzzle 10.
19051- if mname == "chomp!"
19052- return compile_string_method_expr(nid, "chomp", rc)
19053- end
19060+ # chomp! / chop! / etc. on a frozen string literal raise
19061+ # FrozenError below; the mutable_str arm in compile_call_expr
19062+ # handles the legal case before reaching here. Issues #859,
19063+ # #886.
1905419064 # Comparable#clamp on string. Issue #899.
1905519065 if mname == "clamp"
1905619066 args_id_clp = @nd_arguments[nid]
@@ -19074,7 +19084,7 @@
1907419084 # downstream type inference doesn't break -- the raise is a
1907519085 # non-returning C call inside a stmt-expression.
1907619086 if mname == "insert" || mname == "prepend" || mname == "<<" || mname == "concat" || mname == "replace" || mname == "clear" ||
19077- mname == "chop!" || mname == "upcase!" || mname == "downcase!" || mname == "swapcase!" || mname == "capitalize!" ||
19087+ mname == "chomp!" || mname == "chop!" || mname == "upcase!" || mname == "downcase!" || mname == "swapcase!" || mname == "capitalize!" ||
1907819088 mname == "sub!" || mname == "gsub!" || mname == "squeeze!" || mname == "strip!" || mname == "lstrip!" || mname == "rstrip!" ||
1907919089 mname == "tr!" || mname == "tr_s!" || mname == "delete!" || mname == "reverse!" || mname == "succ!" || mname == "next!"
1908019090 @needs_exc_class_hierarchy = 1