Commit 3b732d07

3b732d077e8dMerged

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

Description

fix(codegen): poly s.gsub(regex, hash) routes to str_str_hash helper

Closes #503. Sibling to #485. The typed-receiver path already routes s.gsub(regex, str_str_hash) through the hash-aware sp_re_gsub_str_str_hash runtime helper. The poly-receiver path (SP_TAG_STR arm of compile_poly_method_call) still emitted sp_re_gsub(pat, recv.v.s, hash_ptr) and smuggled the hash pointer through the const char *rep slot — C compile warned -Wincompatible-pointer-types and the binary ran but interpreted the hash pointer's bytes as a replacement string (same garbage as the pre-#485 typed path).

Mirror the typed-receiver branch on the poly SP_TAG_STR arm: when mname == "gsub" and infer_type(rep) == "str_str_hash", emit sp_re_gsub_str_str_hash(pat, recv.v.s, rep). The sub arm and the literal-string replacement path stay on the existing sp_re_gsub / sp_re_sub.

Test fixture passes an A-or-B poly value (A#body string, B#body int) into a function whose param widens to poly, then calls gsub with a regex pattern and a frozen str_str_hash literal. Output now matches CRuby instead of the pre-fix garbage.

Local: 475 tests pass.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
3b732d077e8d
Parent
98377fcacc87

spinel_codegen.rbmodifiedView file

@@ -18734,7 +18734,17 @@
1873418734 str_fn_gs = mname == "gsub" ? "sp_str_gsub" : "sp_str_sub"
1873518735 rep_gs = compile_expr(a_gs[1])
1873618736 if rpat_gs != ""
18737- call_gs = fn_gs + "(" + rpat_gs + ", " + recv_tmp + ".v.s, " + rep_gs + ")"
18737+ # gsub(regex, hash) on the SP_TAG_STR poly arm. Issue #503:
18738+ # the typed-receiver path (compile_string_method_expr) routes
18739+ # str_str_hash replacements through `sp_re_gsub_str_str_hash`;
18740+ # the poly arm previously fell through to `sp_re_gsub` and
18741+ # smuggled the hash pointer through where `const char *rep`
18742+ # is expected. Mirror the typed-receiver branch here.
18743+ if mname == "gsub" && infer_type(a_gs[1]) == "str_str_hash"
18744+ call_gs = "sp_re_gsub_str_str_hash(" + rpat_gs + ", " + recv_tmp + ".v.s, " + rep_gs + ")"
18745+ else
18746+ call_gs = fn_gs + "(" + rpat_gs + ", " + recv_tmp + ".v.s, " + rep_gs + ")"
18747+ end
1873818748 else
1873918749 call_gs = str_fn_gs + "(" + recv_tmp + ".v.s, " + compile_expr(a_gs[0]) + ", " + rep_gs + ")"
1874018750 end