Commit 3c63156f

3c63156f2478Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 6 files · ~6 modified

Description

fix(analyze,codegen,runtime): close remaining #555 cases (07 dig, 11 =~ 0-indexed)

Followup to 085fc41 + 3dcb18f. With this commit spinel matches CRuby on all 12 cases of Adam Doppelt's "should be true but they are false" corpus.

Case 11: =~ returns 0-indexed match position with -1 sentinel for no match. Pre-fix it returned pos + 1 (or 0 for no match) -- a truthiness-compatible sentinel that was load-bearing for if (s =~ /pat/) working at position 0, but off-by-one from CRuby's int return semantics. The truthiness check at regex_match_call_node? is updated from != 0 to >= 0 so match-at-position-0 stays truthy. The existing test/regex.rb expected line for m_s2_index + 10 goes from 12 (1+1+10 pre-fix) to 11 (1+10 CRuby).

Case 07: Array#dig over a heterogeneous poly_array of hashes. Two pieces:

  • poly_array.dig(idx, key...) emit: get arr[idx] then walk the remaining keys via emit_dig_step's cls_id-aware Hash dispatch. Mirror of the existing Hash#dig multi-key path. compile_array_method_expr's dig arm previously fell through to "unsupported" for multi-arg.

  • sym-key sym-value hash literals ({foo: :bar}) infer as sym_poly_hash rather than sym_int_hash so the symbol value preserves its SP_TAG_SYM tag through lookup. The sym_int_hash storage stores sym ids as mrb_int and the dispatch's sp_box_int round-trip lost the symbol-ness -- comparing the dug value to :bar failed because poly_eq's tag check saw SP_TAG_INT vs SP_TAG_SYM. The sym_poly_hash path boxes correctly.

  • scan_for_dig_usage pre-scan that arms @needs_sym_int_hash / sym_str_hash / sym_poly_hash / str_poly_hash so emit_dig_step's full arm set has every typedef it references. The compile-side @needs_ flag flips run after the typedef-emit pass.

Verified: 525 tests pass (including the 12-case fixture and the updated regex.rb expected), optcarrot checksum 59662 preserved.

Refs #555

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

Details

Commit
3c63156f2478
Parent
1043795935b5

lib/sp_runtime.hmodifiedView file

@@ -1015,12 +1015,17 @@
10151015 }
10161016 }
10171017
1018+/* `=~` returns the match position (0-indexed) or -1 on miss.
1019+ Codegen's regex truthy check (regex_match_call_node? arm in
1020+ compile_cond_expr) compares against -1 so match-at-position-0
1021+ is correctly truthy. Direct value use lines up with CRuby's
1022+ `String#=~` int semantics: "abc" =~ /b/ -> 1, not 2. */
10181023 static mrb_int sp_re_match(mrb_regexp_pattern *pat, const char *str) {
10191024 int64_t slen = (int64_t)strlen(str);
10201025 int ncaps = 32;
10211026 int n = re_exec(pat, str, slen, 0, sp_re_caps, ncaps);
1022- if (n > 0) { sp_re_set_captures(str, sp_re_caps, n/2); return sp_re_caps[0] + 1; }
1023- return 0;
1027+ if (n > 0) { sp_re_set_captures(str, sp_re_caps, n/2); return sp_re_caps[0]; }
1028+ return -1;
10241029 }
10251030
10261031 /* `s.rindex(regex)` — last match start, in BYTE offset (matches