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:

  1. $~, $&, $\``, $'-- Prism categorizes these asGlobalVariableReadNode(notBackReferenceReadNode), so the existing back-ref arm in compile_expr never fired and the codegen instead tried to declare static mrb_int gv_~ = 0; which the C parser rejects (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 $` formats correctly. $~ === $& (no MatchData wrapper), consistent with the existing back-ref arm.

  2. =~ returns sp_RbVal nil/Integer instead of -1/Integer. Add sp_re_match_poly runtime wrapper next to the existing sp_re_rindex_poly. Both call sites (regex =~ str / str =~ /pattern/) route through it; analyze marks the return "poly". ("abc" =~ /xyz/).nil? answers true and puts(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

@@ -1365,6 +1365,13 @@
13651365 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); }
13661366 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); }
13671367 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); }
1368+/* CRuby-compatible =~ wrapper: SP_TAG_INT(pos) on match, SP_TAG_NIL
1369+ on miss. Codegen routes the `=~` operator here so
1370+ `("abc" =~ /xyz/).nil?` answers true and `puts("abc" =~ /xyz/)`
1371+ prints an empty line, matching CRuby. The raw `sp_re_match`
1372+ (returning -1) stays available for internal callers needing the
1373+ sentinel form. */
1374+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); }
13681375 static sp_RbVal sp_box_nullable_obj(void *p, int cls_id) { return p ? sp_box_obj(p, cls_id) : sp_box_nil(); }
13691376 /* Built-in pointer boxes — share SP_TAG_OBJ with a reserved negative
13701377 cls_id so the dispatch path is uniform. */