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
str_fn_gs = mname == "gsub" ? "sp_str_gsub" : "sp_str_sub" rep_gs = compile_expr(a_gs[1]) if rpat_gs != "" call_gs = fn_gs + "(" + rpat_gs + ", " + recv_tmp + ".v.s, " + rep_gs + ")" # gsub(regex, hash) on the SP_TAG_STR poly arm. Issue #503: # the typed-receiver path (compile_string_method_expr) routes # str_str_hash replacements through `sp_re_gsub_str_str_hash`; # the poly arm previously fell through to `sp_re_gsub` and # smuggled the hash pointer through where `const char *rep` # is expected. Mirror the typed-receiver branch here. if mname == "gsub" && infer_type(a_gs[1]) == "str_str_hash" call_gs = "sp_re_gsub_str_str_hash(" + rpat_gs + ", " + recv_tmp + ".v.s, " + rep_gs + ")" else call_gs = fn_gs + "(" + rpat_gs + ", " + recv_tmp + ".v.s, " + rep_gs + ")" end else call_gs = str_fn_gs + "(" + recv_tmp + ".v.s, " + compile_expr(a_gs[0]) + ", " + rep_gs + ")" endtest/poly_gsub_str_str_hash.rbaddedView file
Loading diff…
test/poly_gsub_str_str_hash.rb.expectedaddedView file
Loading diff…