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 ofmrb_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
@needs_rb_value = 1 return "poly" end # Built-in string methods that compile_poly_method_call also # lowers via a SP_TAG_STR arm — their result temp needs to be # at least string-typed so the per-tag dispatch assignment # doesn't try to store const char * into a mrb_int slot. If a # user class also defines the method with a different return # type, the per-class loop below escalates to "poly". if mname == "gsub" || mname == "sub" ci_s = 0 diverges = 0 while ci_s < @cls_names.length if cls_find_method_direct(ci_s, mname) >= 0 urt = cls_method_return(ci_s, mname) if urt != "" && urt != "string" diverges = 1 ci_s = @cls_names.length end end ci_s = ci_s + 1 end if diverges == 1 @needs_rb_value = 1 return "poly" end return "string" end # Setters: mname ends with "=" and at least one class has an # attr_writer for the bare name. Return type is the ivar type # (Ruby returns the rhs from `x = v`); without this, the result brhs = is_poly_ret == 1 ? "sp_box_int(" + bit + ")" : bit emit(" if (" + recv_tmp + ".tag == SP_TAG_INT) " + tmp + " = " + brhs + ";") end # SP_TAG_STR arm for string methods that have a concrete # str-recv lowering. `v.gsub(/re/, repl)` where v is poly # (e.g. v came from `poly_hash[k]`) needs a per-tag dispatch # arm — otherwise the poly recv falls through to the # SP_TAG_OBJ block, which has no String-method handler, and # the result temp stays at its default (mrb_int 0). The # caller's downstream assignment to a const char * slot then # fails C compile. if mname == "gsub" || mname == "sub" args_id_gs = @nd_arguments[nid] if args_id_gs >= 0 a_gs = get_args(args_id_gs) if a_gs.length >= 2 rpat_gs = regex_pat_c_expr(a_gs[0]) fn_gs = mname == "gsub" ? "sp_re_gsub" : "sp_re_sub" 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 + ")" else call_gs = str_fn_gs + "(" + recv_tmp + ".v.s, " + compile_expr(a_gs[0]) + ", " + rep_gs + ")" end str_rhs_gs = is_poly_ret == 1 ? "sp_box_str(" + call_gs + ")" : call_gs emit(" if (" + recv_tmp + ".tag == SP_TAG_STR) " + tmp + " = " + str_rhs_gs + ";") end end end emit(" if (" + recv_tmp + ".tag == SP_TAG_OBJ) {") # User-class dispatch i = 0test/poly_gsub_str_arm.rbaddedView file
Loading diff…
test/poly_gsub_str_arm.rb.expectedaddedView file
Loading diff…