Commit 82d5292a
82d5292a9f99Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · ~3 modified
Description
fix(analyze,codegen,runtime): $/$&/$`/$' + = return CRuby-compatible values
Two related observability fixes for the regex globals and the match operator:
-
$~,$&,$\``,$'-- Prism categorizes these asGlobalVariableReadNode(notBackReferenceReadNode), so the existing back-ref arm in compile_expr never fired and the codegen instead tried to declarestatic mrb_int gv_~ = 0;which the C parser rejects (` formats correctly. $~ === $& (no MatchData wrapper), consistent with the existing back-ref arm.isn't an identifier character). Add a sharedis_special_gvarpredicate in spinel_analyze.rb that skips these names in@gvar_namesregistration, and mirror thesp_re_match_*dispatch in spinel_codegen.rb's GlobalVariableReadNode arm. infer_type returns "string" soputs $ -
=~returns sp_RbVal nil/Integer instead of -1/Integer. Addsp_re_match_polyruntime wrapper next to the existingsp_re_rindex_poly. Both call sites (regex =~ str/str =~ /pattern/) route through it; analyze marks the return "poly".("abc" =~ /xyz/).nil?answers true andputs(s =~ /xyz/)prints an empty line, matching CRuby.
The legacy regex_match_call_node? truthy specialization is now
preceded by the t == "poly" arm in compile_cond_expr /
truthy_node_expr so if (s =~ /pat/) reaches sp_poly_truthy and
correctly treats nil as falsy. The specialization stays as a
safety net.
The raw sp_re_match (returning -1) keeps its place for any
internal caller that needs the sentinel; none today.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 82d5292a9f99
- Parent
- fab6afd98f97
lib/sp_runtime.hmodifiedView file
static sp_RbVal sp_str_index_from_poly(const char *s, const char *sub, mrb_int start) { mrb_int n = sp_str_index_from(s, sub, start); return n < 0 ? sp_box_nil() : sp_box_int(n); }static sp_RbVal sp_str_rindex_poly(const char *s, const char *sub) { mrb_int n = sp_str_rindex(s, sub); return n < 0 ? sp_box_nil() : sp_box_int(n); }static sp_RbVal sp_re_rindex_poly(mrb_regexp_pattern *pat, const char *str) { mrb_int n = sp_re_rindex(pat, str); return n < 0 ? sp_box_nil() : sp_box_int(n); }/* CRuby-compatible =~ wrapper: SP_TAG_INT(pos) on match, SP_TAG_NIL on miss. Codegen routes the `=~` operator here so `("abc" =~ /xyz/).nil?` answers true and `puts("abc" =~ /xyz/)` prints an empty line, matching CRuby. The raw `sp_re_match` (returning -1) stays available for internal callers needing the sentinel form. */static sp_RbVal sp_re_match_poly(mrb_regexp_pattern *pat, const char *str) { mrb_int n = sp_re_match(pat, str); return n < 0 ? sp_box_nil() : sp_box_int(n); }static sp_RbVal sp_box_nullable_obj(void *p, int cls_id) { return p ? sp_box_obj(p, cls_id) : sp_box_nil(); }/* Built-in pointer boxes — share SP_TAG_OBJ with a reserved negative cls_id so the dispatch path is uniform. */spinel_analyze.rbmodifiedView file
Loading diff…
spinel_codegen.rbmodifiedView file
Loading diff…