Commit 0c48839c

0c48839cc3caMerged

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

Description

fix(codegen): emit SP_TAG_STR arm for gsub/sub on poly receiver (#444)

v.gsub(/re/, repl) where v is sp_RbVal (a poly-typed local holding a string at runtime, e.g. from a poly-valued hash lookup) emitted the poly-dispatch shell if (v.tag == SP_TAG_OBJ) { } but with no per-tag arm — the SP_TAG_OBJ block iterates user classes for gsub method definitions and finds none, leaving the result temp at its default mrb_int 0. The downstream const char * assignment then failed C compile with int-to-pointer mismatch.

Two coordinated changes in compile_poly_method_call:

  • SP_TAG_STR arm for gsub / sub at the same level as the existing SP_TAG_INT bit-extract for []. Picks sp_re_gsub / sp_re_sub for regex patterns, sp_str_gsub / sp_str_sub for string-literal patterns.

  • poly_dispatch_return_type returns "string" for gsub / sub so the result temp is const char * instead of mrb_int, letting the per-tag arm assign without C mismatch. Escalates to "poly" if any user class defines the method with a non-string return type (so cross-call-site consumers still see the wider type).

Closes #444

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

Details

Commit
0c48839cc3ca
Parent
120b8f237d16

spinel_codegen.rbmodifiedView file

@@ -17703,6 +17703,31 @@
1770317703 @needs_rb_value = 1
1770417704 return "poly"
1770517705 end
17706+ # Built-in string methods that compile_poly_method_call also
17707+ # lowers via a SP_TAG_STR arm — their result temp needs to be
17708+ # at least string-typed so the per-tag dispatch assignment
17709+ # doesn't try to store const char * into a mrb_int slot. If a
17710+ # user class also defines the method with a different return
17711+ # type, the per-class loop below escalates to "poly".
17712+ if mname == "gsub" || mname == "sub"
17713+ ci_s = 0
17714+ diverges = 0
17715+ while ci_s < @cls_names.length
17716+ if cls_find_method_direct(ci_s, mname) >= 0
17717+ urt = cls_method_return(ci_s, mname)
17718+ if urt != "" && urt != "string"
17719+ diverges = 1
17720+ ci_s = @cls_names.length
17721+ end
17722+ end
17723+ ci_s = ci_s + 1
17724+ end
17725+ if diverges == 1
17726+ @needs_rb_value = 1
17727+ return "poly"
17728+ end
17729+ return "string"
17730+ end
1770617731 # Setters: mname ends with "=" and at least one class has an
1770717732 # attr_writer for the bare name. Return type is the ivar type
1770817733 # (Ruby returns the rhs from `x = v`); without this, the result
@@ -17954,6 +17979,33 @@
1795417979 brhs = is_poly_ret == 1 ? "sp_box_int(" + bit + ")" : bit
1795517980 emit(" if (" + recv_tmp + ".tag == SP_TAG_INT) " + tmp + " = " + brhs + ";")
1795617981 end
17982+ # SP_TAG_STR arm for string methods that have a concrete
17983+ # str-recv lowering. `v.gsub(/re/, repl)` where v is poly
17984+ # (e.g. v came from `poly_hash[k]`) needs a per-tag dispatch
17985+ # arm — otherwise the poly recv falls through to the
17986+ # SP_TAG_OBJ block, which has no String-method handler, and
17987+ # the result temp stays at its default (mrb_int 0). The
17988+ # caller's downstream assignment to a const char * slot then
17989+ # fails C compile.
17990+ if mname == "gsub" || mname == "sub"
17991+ args_id_gs = @nd_arguments[nid]
17992+ if args_id_gs >= 0
17993+ a_gs = get_args(args_id_gs)
17994+ if a_gs.length >= 2
17995+ rpat_gs = regex_pat_c_expr(a_gs[0])
17996+ fn_gs = mname == "gsub" ? "sp_re_gsub" : "sp_re_sub"
17997+ str_fn_gs = mname == "gsub" ? "sp_str_gsub" : "sp_str_sub"
17998+ rep_gs = compile_expr(a_gs[1])
17999+ if rpat_gs != ""
18000+ call_gs = fn_gs + "(" + rpat_gs + ", " + recv_tmp + ".v.s, " + rep_gs + ")"
18001+ else
18002+ call_gs = str_fn_gs + "(" + recv_tmp + ".v.s, " + compile_expr(a_gs[0]) + ", " + rep_gs + ")"
18003+ end
18004+ str_rhs_gs = is_poly_ret == 1 ? "sp_box_str(" + call_gs + ")" : call_gs
18005+ emit(" if (" + recv_tmp + ".tag == SP_TAG_STR) " + tmp + " = " + str_rhs_gs + ";")
18006+ end
18007+ end
18008+ end
1795718009 emit(" if (" + recv_tmp + ".tag == SP_TAG_OBJ) {")
1795818010 # User-class dispatch
1795918011 i = 0