Commit 21bd139e

21bd139e2e44Merged

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

Description

codegen,analyze: Regexp#source and #options for literal recv

/hello/.source returns "hello"; /x/i.options returns the CRuby flag bitmask (1=IGNORECASE, 2=EXTENDED, 4=MULTILINE). Both were falling through to the int-recv dispatch and emitting 0 because the literal infers as int by default.

  • analyze: RegularExpressionNode infers as "regexp"; source/options on a literal recv get string/int respectively.
  • codegen: literal recv -> string literal for .source, prism flag bits remapped to CRuby's mask for .options.

named_captures deferred -- would need a Hash + per-named-group index array (runtime doesn't expose this yet).

Refs #868

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

Details

Commit
21bd139e2e44
Parent
f4fe6e5f68e2

spinel_analyze.rbmodifiedView file

@@ -1993,6 +1993,12 @@
19931993 if t == "FloatNode"
19941994 return "float"
19951995 end
1996+ # Issue #868: regex literal as a value (`/x/.source` etc.).
1997+ # Without this the bare literal infers as int (unresolved default)
1998+ # and accessor methods fall through to the int dispatch.
1999+ if t == "RegularExpressionNode"
2000+ return "regexp"
2001+ end
19962002 if t == "StringNode"
19972003 return "string"
19982004 end
@@ -3926,6 +3932,14 @@
39263932 end
39273933 return "string"
39283934 end
3935+ # Issue #868: Regexp#source returns the pattern string;
3936+ # Regexp#options returns the engine flag bitmask as int.
3937+ if mname == "source" && recv >= 0 && @nd_type[recv] == "RegularExpressionNode"
3938+ return "string"
3939+ end
3940+ if mname == "options" && recv >= 0 && @nd_type[recv] == "RegularExpressionNode"
3941+ return "int"
3942+ end
39293943 if mname == "inspect"
39303944 return "string"
39313945 end