Commit d3c0a37f

d3c0a37fa320Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · +2 added · ~2 modified

Description

Implement Regexp.last_match(n) capture lookup

Regexp.last_match(n) resolved to the NoMethodError gate and emitted 0 after a successful match. Map it to the same capture slots $1..$9 read: n==0 is the whole match (sp_re_match_str), n in 1..9 are the groups (sp_re_captures[n]), and an out-of-range or non-participating group is NULL, which the nullable String path renders as nil. Analyze types the call as a (nullable) String.

The no-arg form (which returns a MatchData) is unchanged; Spinel has no global MatchData object, the same limitation as $~.

Fixes #1257

Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
d3c0a37fa320
Parent
2e186d18c766

spinel_analyze.rbmodifiedView file

@@ -6916,6 +6916,12 @@
69166916 if mname == "escape" || mname == "quote"
69176917 return "string"
69186918 end
6919+ # `Regexp.last_match(n)` returns the n-th capture of the most
6920+ # recent match (n==0 is the whole match), or nil. Same nullable
6921+ # String slot codegen draws from sp_re_captures / sp_re_match_str.
6922+ if mname == "last_match"
6923+ return "string"
6924+ end
69196925 end
69206926 if rcname == "File"
69216927 if mname == "read" || mname == "binread"