From 27b744cd84dc1c2e7c5896163568857968912e61 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 1 Sep 2026 09:27:28 +0200 Subject: [PATCH 1/8] Regexp.union/quote/escape: MRI-faithful operand coercion and escaping - regexpEscapeLiteral now escapes '-', space, '#' and control chars (\n \r \f \t \v) exactly as MRI's rb_reg_quote. - Regexp.union embeds a Regexp operand as its #to_s (options ride along), accepts Symbol/#to_str/#to_regexp operands, and returns a lone Regexp verbatim. - Regexp.quote/escape accept a Symbol and share one Method record so Regexp.method(:escape) == Regexp.method(:quote). Co-Authored-By: Claude Opus 4.8 --- internal/vm/regexp.go | 106 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 87 insertions(+), 19 deletions(-) diff --git a/internal/vm/regexp.go b/internal/vm/regexp.go index 07803cfa..583db3f9 100644 --- a/internal/vm/regexp.go +++ b/internal/vm/regexp.go @@ -587,15 +587,65 @@ func scanRegexp(v object.Value) *Regexp { // matches literally. Only the operators special at top level are escaped (the // engine rejects superfluous escapes such as \-); control and other bytes are // emitted verbatim, which the byte-oriented engine matches literally. +// regexpOperandStr converts a Regexp operand (for Regexp.quote/escape/union of a +// non-Regexp) to its string form the way MRI's reg_operand does: a String is +// taken directly, a Symbol yields its name, and anything else is coerced via +// #to_str (raising TypeError when it has none). +func (vm *VM) regexpOperandStr(v object.Value) string { + switch x := v.(type) { + case *object.String: + return x.Str() + case object.Symbol: + return string(x) + } + if vm.respondsToDynamic(v, "to_str") { + if s, ok := vm.send(v, "to_str", nil, nil).(*object.String); ok { + return s.Str() + } + } + raise("TypeError", "no implicit conversion of %s into String", vm.classOf(v).name) + return "" +} + +// toRegexpOperand reports whether v is a Regexp or converts to one via #to_regexp, +// returning that Regexp. It never raises: a value without #to_regexp is simply not +// a Regexp operand. +func (vm *VM) toRegexpOperand(v object.Value) (*Regexp, bool) { + if re, ok := v.(*Regexp); ok { + return re, true + } + if vm.respondsToDynamic(v, "to_regexp") { + if re, ok := vm.send(v, "to_regexp", nil, nil).(*Regexp); ok { + return re, true + } + } + return nil, false +} + func regexpEscapeLiteral(s string) string { - const meta = `.*+?()[]{}|^$\` + // The metacharacters MRI's rb_reg_quote backslash-escapes; note '-' and ' ' + // and '#' are included and '/' is NOT (it is not special outside a literal). + const meta = `[]{}()|-*.\?+^$ #` var b strings.Builder for i := 0; i < len(s); i++ { c := s[i] - if strings.IndexByte(meta, c) >= 0 { - b.WriteByte('\\') + switch c { + case '\n': + b.WriteString(`\n`) + case '\r': + b.WriteString(`\r`) + case '\f': + b.WriteString(`\f`) + case '\t': + b.WriteString(`\t`) + case '\v': + b.WriteString(`\v`) + default: + if strings.IndexByte(meta, c) >= 0 { + b.WriteByte('\\') + } + b.WriteByte(c) } - b.WriteByte(c) } return b.String() } @@ -1185,13 +1235,15 @@ func (vm *VM) installRegexp() { vm.cRegexp.smethods["compile"] = &Method{name: "compile", owner: vm.cRegexp, native: reNew.native} // Regexp.escape(str) / Regexp.quote(str): the string with regex metacharacters - // escaped so it matches literally. + // escaped so it matches literally. A Symbol is accepted (its name is quoted), + // matching MRI's reg_operand handling. reEscape := &Method{name: "escape", owner: vm.cRegexp, - native: func(_ *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { - return object.NewString(regexpEscapeLiteral(strArg(args[0]))) + native: func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { + return object.NewString(regexpEscapeLiteral(vm.regexpOperandStr(args[0]))) }} vm.cRegexp.smethods["escape"] = reEscape - vm.cRegexp.smethods["quote"] = &Method{name: "quote", owner: vm.cRegexp, native: reEscape.native} + // quote shares escape's *Method so `Regexp.method(:escape) == Regexp.method(:quote)`. + vm.cRegexp.smethods["quote"] = reEscape // Regexp.last_match returns the MatchData of the most recent match ($~), or // with an Integer / name argument the corresponding capture (Regexp.last_match(1) @@ -1209,26 +1261,42 @@ func (vm *VM) installRegexp() { }} // Regexp.union(pat, ...) / Regexp.union([pat, ...]) builds one Regexp matching - // any of the patterns. A Regexp argument contributes its source; a String is - // escaped to match literally. With no arguments it matches nothing, as MRI. + // any of the patterns. A Regexp argument contributes its #to_s (so its options + // ride along); a String/Symbol is escaped to match literally. With no arguments + // it matches nothing, as MRI. vm.cRegexp.smethods["union"] = &Method{name: "union", owner: vm.cRegexp, native: func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { - // A single Array argument is the list of patterns. + if len(args) == 0 { + return vm.regexpNew([]object.Value{object.NewString("(?!)")}) + } + // A single argument is special: a Regexp (or to_regexp) is returned + // verbatim, an Array is treated as the pattern list, and a String/Symbol + // becomes a lone quoted pattern (never wrapped in an alternation). if len(args) == 1 { + if re, ok := vm.toRegexpOperand(args[0]); ok { + return re + } if arr, ok := args[0].(*object.Array); ok { + if len(arr.Elems) == 0 { + return vm.regexpNew([]object.Value{object.NewString("(?!)")}) + } + // A single-element list of one Regexp is still returned verbatim. + if len(arr.Elems) == 1 { + if re, ok := vm.toRegexpOperand(arr.Elems[0]); ok { + return re + } + } args = arr.Elems } } - if len(args) == 0 { - return vm.regexpNew([]object.Value{object.NewString("(?!)")}) - } sources := make([]string, len(args)) for i, a := range args { - switch v := a.(type) { - case *Regexp: - sources[i] = v.source - default: - sources[i] = regexpEscapeLiteral(strArg(a)) + if re, ok := vm.toRegexpOperand(a); ok { + // A Regexp contributes its #to_s so its own options ride along, + // e.g. Regexp.union(/dogs/, /cats/i) => /(?-mix:dogs)|(?i-mx:cats)/. + sources[i] = re.ToS() + } else { + sources[i] = regexpEscapeLiteral(vm.regexpOperandStr(a)) } } return vm.regexpNew([]object.Value{object.NewString(strings.Join(sources, "|"))}) From 4ef2b38d6dff131c8c87786e22878cc62796bb7f Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 1 Sep 2026 09:29:41 +0200 Subject: [PATCH 2/8] Update PBRegexp union test to MRI-faithful #to_s embedding Regexp.union("a.b", /cd/).source is "a\.b|(?-mix:cd)" in MRI, not "a\.b|cd"; the prior expectation encoded the old source-only behavior. Co-Authored-By: Claude Opus 4.8 --- internal/vm/puppet_boot_modified_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/vm/puppet_boot_modified_test.go b/internal/vm/puppet_boot_modified_test.go index 2ca1166a..694e9d1c 100644 --- a/internal/vm/puppet_boot_modified_test.go +++ b/internal/vm/puppet_boot_modified_test.go @@ -241,8 +241,9 @@ func TestPBRegexpNewBranches(t *testing.T) { cases := []struct{ src, want string }{ {`puts Regexp.escape("a.b*c")`, `a\.b\*c`}, {`puts Regexp.quote("a.b*c")`, `a\.b\*c`}, - // String member is escaped, Regexp member contributes its source. - {`puts Regexp.union("a.b", /cd/).source`, `a\.b|cd`}, + // String member is escaped, Regexp member contributes its #to_s (with its + // options), as MRI: Regexp.union("a.b", /cd/).source => "a\\.b|(?-mix:cd)". + {`puts Regexp.union("a.b", /cd/).source`, `a\.b|(?-mix:cd)`}, // Single Array argument is the pattern list. {`puts Regexp.union(["x", "y"]).source`, "x|y"}, // No arguments -> a pattern that matches nothing. From 0fef375f16de0e0df6add6d60be44147ec1a25b2 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 1 Sep 2026 09:44:05 +0200 Subject: [PATCH 3/8] Regexp/MatchData: MRI-faithful inspect slashes, match coercion/block, union recursion, to_int offsets - Regexp#inspect / #to_s escape unescaped '/' in the source. - Regexp#match: nil subject resets $~ and returns nil; Symbol/#to_str subjects are coerced; a block yields the MatchData and returns its value (no yield on a failed match). - reArg raises TypeError on an uninitialized Regexp (Regexp.allocate). - MatchData#match(n|name) (Ruby 3.4): the scalar subset of #[]. - MatchData#begin/#end/#offset (+byte variants) coerce a non-name argument via #to_int (Float, custom #to_int), then range-check -> IndexError. - Regexp.union restructured to MRI's recursion: lone Regexp/#to_regexp verbatim, Array recurses, lone String/Symbol quoted, multi-arg joins #to_s / #to_str-quoted operands (a multi-arg Symbol raises TypeError). Co-Authored-By: Claude Opus 4.8 --- internal/vm/regexp.go | 189 +++++++++++++++++++-------- internal/vm/regexp_matchdata_test.go | 11 +- internal/vm/regexp_residuals_test.go | 78 +++++++++++ 3 files changed, 221 insertions(+), 57 deletions(-) create mode 100644 internal/vm/regexp_residuals_test.go diff --git a/internal/vm/regexp.go b/internal/vm/regexp.go index 583db3f9..bd15c77b 100644 --- a/internal/vm/regexp.go +++ b/internal/vm/regexp.go @@ -76,6 +76,27 @@ func (r *Regexp) optionBits() int64 { return bits } +// escapeForwardSlashes backslash-escapes each unescaped '/' in a regexp source +// for #to_s / #inspect, the way MRI renders it, without double-escaping a '/' +// that is already preceded by a backslash (a backslash escapes the next byte). +func escapeForwardSlashes(src string) string { + var b strings.Builder + for i := 0; i < len(src); i++ { + c := src[i] + if c == '\\' && i+1 < len(src) { + b.WriteByte(c) + b.WriteByte(src[i+1]) + i++ + continue + } + if c == '/' { + b.WriteByte('\\') + } + b.WriteByte(c) + } + return b.String() +} + // encodingName returns the canonical name of the encoding Regexp#encoding // reports: an ASCII-only source is US-ASCII; a source with a non-ASCII byte is // UTF-8, or ASCII-8BIT (BINARY) when the NOENCODING option is set. @@ -115,11 +136,14 @@ func (r *Regexp) ToS() string { if off != "" { off = "-" + off } - return "(?" + on + off + ":" + r.source + ")" + return "(?" + on + off + ":" + escapeForwardSlashes(r.source) + ")" } -func (r *Regexp) Inspect() string { return "/" + r.source + "/" + orderFlags(r.flags) } -func (r *Regexp) Truthy() bool { return true } +// Inspect renders /source/flags, escaping unescaped '/' in the source. +func (r *Regexp) Inspect() string { + return "/" + escapeForwardSlashes(r.source) + "/" + orderFlags(r.flags) +} +func (r *Regexp) Truthy() bool { return true } // orderFlags returns the present flags in Ruby's canonical m, i, x order. func orderFlags(flags string) string { @@ -607,6 +631,54 @@ func (vm *VM) regexpOperandStr(v object.Value) string { return "" } +// regexpUnion implements Regexp.union, following MRI's structure. With no +// arguments it matches nothing (/(?!)/). A single argument is special: a Regexp +// (or #to_regexp) is returned verbatim, an Array recurses as the pattern list, +// and a lone String/Symbol becomes a single quoted pattern. Two or more patterns +// are joined by '|', each Regexp contributing its #to_s and each other operand +// coerced via #to_str only (a Symbol raises TypeError here, unlike the lone case). +func (vm *VM) regexpUnion(args []object.Value) object.Value { + if len(args) == 0 { + return vm.regexpNew([]object.Value{object.NewString("(?!)")}) + } + if len(args) == 1 { + if re, ok := vm.toRegexpOperand(args[0]); ok { + return re + } + if arr, ok := args[0].(*object.Array); ok { + return vm.regexpUnion(arr.Elems) + } + // A lone String/Symbol: one quoted pattern, never an alternation. + return vm.regexpNew([]object.Value{object.NewString(regexpEscapeLiteral(vm.regexpOperandStr(args[0])))}) + } + sources := make([]string, len(args)) + for i, a := range args { + if re, ok := vm.toRegexpOperand(a); ok { + sources[i] = re.ToS() + } else { + sources[i] = regexpEscapeLiteral(vm.regexpUnionStr(a)) + } + } + return vm.regexpNew([]object.Value{object.NewString(strings.Join(sources, "|"))}) +} + +// regexpUnionStr coerces a non-Regexp operand of a multi-pattern Regexp.union to +// a String via #to_str only (MRI's rb_check_string_type): a String is taken +// directly, anything else must supply #to_str, and a Symbol — which has none — +// raises TypeError, unlike the single-argument union or Regexp.quote. +func (vm *VM) regexpUnionStr(v object.Value) string { + if s, ok := v.(*object.String); ok { + return s.Str() + } + if vm.respondsToDynamic(v, "to_str") { + if s, ok := vm.send(v, "to_str", nil, nil).(*object.String); ok { + return s.Str() + } + } + raise("TypeError", "no implicit conversion of %s into String", vm.classOf(v).name) + return "" +} + // toRegexpOperand reports whether v is a Regexp or converts to one via #to_regexp, // returning that Regexp. It never raises: a value without #to_regexp is simply not // a Regexp operand. @@ -1215,7 +1287,15 @@ func groupValue(m *MatchData, i int) object.Value { // installRegexp registers the Regexp and MatchData method tables. It runs at the // end of bootstrap so the classes already exist as constants. func (vm *VM) installRegexp() { - reArg := func(v object.Value) *Regexp { return v.(*Regexp) } + // reArg unwraps the Regexp receiver, raising TypeError for an uninitialized + // one (Regexp.allocate without a call to #initialize), as MRI does. + reArg := func(v object.Value) *Regexp { + r, ok := v.(*Regexp) + if !ok { + raise("TypeError", "uninitialized Regexp") + } + return r + } // Regexp option constants (MRI values): IGNORECASE=1, EXTENDED=2, MULTILINE=4. vm.cRegexp.consts["IGNORECASE"] = object.IntValue(reIgnoreCase) @@ -1266,40 +1346,7 @@ func (vm *VM) installRegexp() { // it matches nothing, as MRI. vm.cRegexp.smethods["union"] = &Method{name: "union", owner: vm.cRegexp, native: func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value { - if len(args) == 0 { - return vm.regexpNew([]object.Value{object.NewString("(?!)")}) - } - // A single argument is special: a Regexp (or to_regexp) is returned - // verbatim, an Array is treated as the pattern list, and a String/Symbol - // becomes a lone quoted pattern (never wrapped in an alternation). - if len(args) == 1 { - if re, ok := vm.toRegexpOperand(args[0]); ok { - return re - } - if arr, ok := args[0].(*object.Array); ok { - if len(arr.Elems) == 0 { - return vm.regexpNew([]object.Value{object.NewString("(?!)")}) - } - // A single-element list of one Regexp is still returned verbatim. - if len(arr.Elems) == 1 { - if re, ok := vm.toRegexpOperand(arr.Elems[0]); ok { - return re - } - } - args = arr.Elems - } - } - sources := make([]string, len(args)) - for i, a := range args { - if re, ok := vm.toRegexpOperand(a); ok { - // A Regexp contributes its #to_s so its own options ride along, - // e.g. Regexp.union(/dogs/, /cats/i) => /(?-mix:dogs)|(?i-mx:cats)/. - sources[i] = re.ToS() - } else { - sources[i] = regexpEscapeLiteral(vm.regexpOperandStr(a)) - } - } - return vm.regexpNew([]object.Value{object.NewString(strings.Join(sources, "|"))}) + return vm.regexpUnion(args) }} vm.cRegexp.define("source", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { @@ -1338,15 +1385,33 @@ func (vm *VM) installRegexp() { } return object.Bool(re.matcher().MatchString(subject)) }) - vm.cRegexp.define("match", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cRegexp.define("match", func(vm *VM, self object.Value, args []object.Value, blk *Proc) object.Value { + re := reArg(self) + // A nil subject never matches and resets $~ to nil (so a later Regexp.last_match + // sees no match), returning nil without yielding to any block. if _, isNil := args[0].(object.Nil); isNil { + vm.lastMatch = object.NilV return object.NilV } - // match(str, pos): start scanning at character offset pos (defaults to 0). + // The subject is coerced like any Regexp operand: a Symbol yields its name, + // anything else is taken via #to_str (Integer/Exception raise TypeError). + subject := vm.regexpOperandStr(args[0]) + var md object.Value if len(args) >= 2 { - return vm.runMatchFrom(reArg(self), strArg(args[0]), intArg(args[1])) + // match(str, pos): start scanning at character offset pos. + md = vm.runMatchFrom(re, subject, intArg(args[1])) + } else { + md = vm.runMatch(re, subject) } - return vm.runMatch(reArg(self), strArg(args[0])) + // With a block, a successful match yields the MatchData and the block's value + // is returned; a failed match returns nil and never yields. + if blk != nil { + if _, isNil := md.(object.Nil); isNil { + return object.NilV + } + return vm.callBlock(blk, []object.Value{md}) + } + return md }) vm.cRegexp.define("=~", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { return vm.regexpMatchIndex(reArg(self), args[0]) @@ -1493,34 +1558,34 @@ func (vm *VM) installRegexp() { } return object.NewArrayFromSlice(out) }) - vm.cMatchData.define("begin", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cMatchData.define("begin", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { m := mdArg(self) - return m.offset(int64(m.indexForKey(args[0])), false) + return m.offset(int64(m.indexForKey(vm, args[0])), false) }) - vm.cMatchData.define("end", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cMatchData.define("end", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { m := mdArg(self) - return m.offset(int64(m.indexForKey(args[0])), true) + return m.offset(int64(m.indexForKey(vm, args[0])), true) }) // MatchData#offset(n_or_name) is the [begin, end] character-offset pair of the // group; [nil, nil] for a group that did not participate. - vm.cMatchData.define("offset", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cMatchData.define("offset", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { m := mdArg(self) - idx := int64(m.indexForKey(args[0])) + idx := int64(m.indexForKey(vm, args[0])) return object.NewArrayFromSlice([]object.Value{m.offset(idx, false), m.offset(idx, true)}) }) // bytebegin / byteend / byteoffset are the byte-index counterparts of begin / // end / offset (Ruby 3.2+): they skip the byte-to-character conversion. - vm.cMatchData.define("bytebegin", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cMatchData.define("bytebegin", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { m := mdArg(self) - return m.byteOffset(int64(m.indexForKey(args[0])), false) + return m.byteOffset(int64(m.indexForKey(vm, args[0])), false) }) - vm.cMatchData.define("byteend", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cMatchData.define("byteend", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { m := mdArg(self) - return m.byteOffset(int64(m.indexForKey(args[0])), true) + return m.byteOffset(int64(m.indexForKey(vm, args[0])), true) }) - vm.cMatchData.define("byteoffset", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + vm.cMatchData.define("byteoffset", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { m := mdArg(self) - idx := int64(m.indexForKey(args[0])) + idx := int64(m.indexForKey(vm, args[0])) return object.NewArrayFromSlice([]object.Value{m.byteOffset(idx, false), m.byteOffset(idx, true)}) }) // MatchData#named_captures maps each capture name to its captured substring @@ -1631,6 +1696,12 @@ func (vm *VM) installRegexp() { } return m.at(args[0]) }) + // MatchData#match(n) (Ruby 3.4) returns the single group by index or name — the + // scalar subset of #[] (no Range or (start, length) form), nil for a group that + // did not participate. + vm.cMatchData.define("match", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + return mdArg(self).at(args[0]) + }) } // regexpMatchIndex implements Regexp#=~ and (via a String receiver) String#=~: @@ -1762,8 +1833,9 @@ func (m *MatchData) byName(name string) object.Value { // indexForKey resolves a #begin/#end/#offset key to a group index: an Integer // out of range raises IndexError; a String/Symbol name is resolved (IndexError -// when unknown); any other type raises TypeError. -func (m *MatchData) indexForKey(key object.Value) int { +// when unknown); any other type is coerced through #to_int (raising TypeError +// when it has none), matching MRI. +func (m *MatchData) indexForKey(vm *VM, key object.Value) int { switch k := key.(type) { case object.Integer: i := int(k) @@ -1776,6 +1848,11 @@ func (m *MatchData) indexForKey(key object.Value) int { case object.Symbol: return m.nameIndex(string(k)) default: + if vm.respondsToDynamic(key, "to_int") { + if iv, ok := vm.send(key, "to_int", nil, nil).(object.Integer); ok { + return m.indexForKey(vm, iv) + } + } raise("TypeError", "no implicit conversion of %s into Integer", classNameOf(key)) return 0 } diff --git a/internal/vm/regexp_matchdata_test.go b/internal/vm/regexp_matchdata_test.go index afe350ff..9ca58320 100644 --- a/internal/vm/regexp_matchdata_test.go +++ b/internal/vm/regexp_matchdata_test.go @@ -165,7 +165,16 @@ func TestMatchDataOffsetErrors(t *testing.T) { {`begin; "ab".match(/(?.)/).offset(:z); rescue => e; p [e.class, e.message]; end`, "[IndexError, \"undefined group name reference: z\"]\n"}, {`begin; "ab".match(/(?.)/).begin(:z); rescue => e; p e.class; end`, "IndexError\n"}, - {`begin; "ab".match(/(.)/).begin(3.5); rescue => e; p e.class; end`, "TypeError\n"}, + // A Float argument is coerced with #to_int (3.5 -> 3), then range-checked: + // index 3 is out of range for a single-group match, so IndexError, as MRI. + {`begin; "ab".match(/(.)/).begin(3.5); rescue => e; p [e.class, e.message]; end`, + "[IndexError, \"index 3 out of matches\"]\n"}, + // An in-range Float coerces and succeeds (1.9 -> 1); group 1 starts at 0. + {`p "ab".match(/(.)(.)/).begin(1.9)`, "0\n"}, + // An object with a #to_int returning an in-range index is accepted. + {`o = Object.new; def o.to_int; 2; end; p "ab".match(/(.)(.)/).begin(o)`, "1\n"}, + // An object with no #to_int (nor String/Symbol) raises TypeError. + {`begin; "ab".match(/(.)/).begin(Object.new); rescue => e; p e.class; end`, "TypeError\n"}, }) } diff --git a/internal/vm/regexp_residuals_test.go b/internal/vm/regexp_residuals_test.go new file mode 100644 index 00000000..1bb6e7ef --- /dev/null +++ b/internal/vm/regexp_residuals_test.go @@ -0,0 +1,78 @@ +package vm_test + +import "testing" + +// TestRegexpUnionResiduals covers Regexp.union operand handling: a lone Regexp +// (or #to_regexp) is returned verbatim, each Regexp in an alternation contributes +// its #to_s (options ride along), Strings/Symbols are quoted, an Array is the +// pattern list, and a multi-argument Symbol (which lacks #to_str) raises +// TypeError while a lone Symbol is accepted. Checked against MRI ruby 4.0. +func TestRegexpUnionResiduals(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`p Regexp.union.source`, "\"(?!)\"\n"}, + {`p Regexp.union([]).source`, "\"(?!)\"\n"}, + {`p Regexp.union(/dogs/, /cats/i).source`, "\"(?-mix:dogs)|(?i-mx:cats)\"\n"}, + {`p Regexp.union("skiing", "sledding").source`, "\"skiing|sledding\"\n"}, + {`p Regexp.union("n", ".").source`, "\"n|\\\\.\"\n"}, + {`p Regexp.union(["+", "-"]).source`, "\"\\\\+|\\\\-\"\n"}, + // A lone Symbol is accepted (its name is quoted); a lone Regexp is verbatim. + {`p Regexp.union(:foo).source`, "\"foo\"\n"}, + {`p Regexp.union(/dogs/i)`, "/dogs/i\n"}, + {`p Regexp.union([/dogs/i])`, "/dogs/i\n"}, + // #to_regexp and #to_str coercion in union. + {`o = Object.new; def o.to_regexp; /foo/; end; p Regexp.union(o)`, "/foo/\n"}, + {`o = Object.new; def o.to_str; "foo"; end; p Regexp.union(o, "bar").source`, "\"foo|bar\"\n"}, + // A Symbol in a multi-argument union has no #to_str: TypeError. + {`begin; Regexp.union(:foo, "bar"); rescue => e; p e.class; end`, "TypeError\n"}, + }) +} + +// TestRegexpQuoteEscapeResiduals covers Regexp.quote/escape: MRI's exact escape +// set (including '-', space, '#' and the control-char escapes) and Symbol input, +// plus the escape/quote Method-identity alias. +func TestRegexpQuoteEscapeResiduals(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`print Regexp.quote("a-b c.d#e")`, "a\\-b\\ c\\.d\\#e"}, + {`print Regexp.escape("\t\n\r\f\v")`, "\\t\\n\\r\\f\\v"}, + {`print Regexp.quote(:symbol)`, "symbol"}, + {`p Regexp.method(:escape) == Regexp.method(:quote)`, "true\n"}, + }) +} + +// TestRegexpInspectSlash covers the forward-slash escaping of #inspect and #to_s, +// without double-escaping an already-escaped slash. +func TestRegexpInspectSlash(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`p Regexp.new("/foo/bar")`, "/\\/foo\\/bar/\n"}, + {`p Regexp.new("//")`, "/\\/\\//\n"}, + {`p(/\/a\/b/)`, "/\\/a\\/b/\n"}, + {`puts Regexp.new("/x").to_s`, "(?-mix:\\/x)\n"}, + }) +} + +// TestRegexpMatchResiduals covers Regexp#match with a Symbol subject, a nil +// subject (which resets $~), a block (yielding the MatchData and returning the +// block's value, or nil with no match), and the uninitialized-Regexp TypeError. +func TestRegexpMatchResiduals(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`p(/(.)(.)/.match(:ab).is_a?(MatchData))`, "true\n"}, + {`/./.match("a"); /1/.match(nil); p $~`, "nil\n"}, + {`p(/./.match("abc") { :res })`, ":res\n"}, + {`p(/z/.match("abc") { :res })`, "nil\n"}, + {`/./.match("abc") { |m| $captured = m }; p $captured.is_a?(MatchData)`, "true\n"}, + {`begin; Regexp.allocate.match("foo"); rescue => e; p e.class; end`, "TypeError\n"}, + {`begin; Regexp.allocate.options; rescue => e; p e.class; end`, "TypeError\n"}, + }) +} + +// TestMatchDataMatchMethod covers MatchData#match(n) / #match(name) (Ruby 3.4): +// the scalar subset of #[], returning nil for a non-participating group. +func TestMatchDataMatchMethod(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`p(/(.)(.)(\d+)(\d)/.match("THX1138.").match(0))`, "\"HX1138\"\n"}, + {`p(/(.)(.)(\d+)(\d)/.match("THX1138.").match(3))`, "\"113\"\n"}, + {`p(/\d+(\w)?/.match("THX1138.").match(1))`, "nil\n"}, + {`m = "haystack".match(/(?t(?ack))/); p m.match(:a)`, "\"ack\"\n"}, + {`m = "haystack".match(/(?t(?ack))/); p m.match(:t)`, "\"tack\"\n"}, + }) +} From 272759ecbee7a9c54591a5dfbc4021c0efca06fc Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 1 Sep 2026 09:49:24 +0200 Subject: [PATCH 4/8] Regexp/MatchData: genuine aliases, match_length, undefined allocate, private initialize - Regexp#eql?, MatchData#eql?, #length, #deconstruct are now genuine built-in aliases (shared method record) of #==, #==, #size, #captures, so instance_method(:alias) == instance_method(:original), as MRI. - MatchData#match_length(n|name) (Ruby 3.4): character length of a group, nil when it did not participate. - MatchData.allocate is undefined -> NoMethodError (bug #16294). - Regexp#initialize is a private method that refuses re-initialization: FrozenError on a frozen literal, TypeError on an already-initialized non-literal or subclass instance. Co-Authored-By: Claude Opus 4.8 --- internal/vm/regexp.go | 55 +++++++++++++++++++++------- internal/vm/regexp_residuals_test.go | 31 ++++++++++++++++ 2 files changed, 72 insertions(+), 14 deletions(-) diff --git a/internal/vm/regexp.go b/internal/vm/regexp.go index bd15c77b..4b37e7d1 100644 --- a/internal/vm/regexp.go +++ b/internal/vm/regexp.go @@ -1314,6 +1314,19 @@ func (vm *VM) installRegexp() { vm.cRegexp.smethods["new"] = reNew vm.cRegexp.smethods["compile"] = &Method{name: "compile", owner: vm.cRegexp, native: reNew.native} + // Regexp#initialize is a private method that a user can never usefully call: a + // Regexp is fully built by Regexp.new / a literal, so re-running #initialize on + // a frozen literal raises FrozenError and on an already-initialized non-literal + // raises TypeError ("already initialized regexp"), matching MRI (< 4.1). + vm.cRegexp.define("initialize", func(vm *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { + if isFrozen(self) { + vm.raiseFrozen(self) + } + raise("TypeError", "already initialized regexp") + return object.NilV + }) + vm.setInstanceVisibility(vm.cRegexp, "initialize", visPrivate) + // Regexp.escape(str) / Regexp.quote(str): the string with regex metacharacters // escaped so it matches literally. A Symbol is accepted (its name is quoted), // matching MRI's reg_operand handling. @@ -1463,7 +1476,8 @@ func (vm *VM) installRegexp() { return object.Bool(a.source == other.source && a.optionBits() == other.optionBits()) } vm.cRegexp.define("==", reEqual) - vm.cRegexp.define("eql?", reEqual) + // #eql? is a genuine alias of #== (shared record). + aliasBuiltin(vm.cRegexp, "eql?", "==") // Regexp#hash is consistent with #== / #eql?: equal Regexps hash equal. vm.cRegexp.define("hash", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { r := reArg(self) @@ -1514,6 +1528,14 @@ func (vm *VM) installRegexp() { mdArg := func(v object.Value) *MatchData { return v.(*MatchData) } + // MatchData.allocate is undefined (a MatchData can only arise from a match), so + // it raises NoMethodError rather than returning an uninitialized object — see + // https://bugs.ruby-lang.org/issues/16294. + vm.cMatchData.smethods["allocate"] = &Method{name: "allocate", owner: vm.cMatchData, + native: func(_ *VM, _ object.Value, _ []object.Value, _ *Proc) object.Value { + raise("NoMethodError", "undefined method 'allocate' for class MatchData") + return object.NilV + }} vm.cMatchData.define("to_s", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return object.NewString(mdArg(self).md.Str(0)) }) @@ -1539,9 +1561,9 @@ func (vm *VM) installRegexp() { vm.cMatchData.define("size", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { return object.IntValue(int64(mdArg(self).md.NGroups() + 1)) }) - vm.cMatchData.define("length", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { - return object.IntValue(int64(mdArg(self).md.NGroups() + 1)) - }) + // MatchData#length is a genuine alias of #size (shared record, so + // MatchData.instance_method(:length) == MatchData.instance_method(:size)). + aliasBuiltin(vm.cMatchData, "length", "size") vm.cMatchData.define("to_a", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { m := mdArg(self) out := make([]object.Value, 0, m.md.NGroups()+1) @@ -1618,15 +1640,9 @@ func (vm *VM) installRegexp() { return object.NewFrozenStringView(mdArg(self).subject) }) // MatchData#deconstruct is the Array of captures (groups 1..n), for array - // pattern matching (`in [a, b]`). - vm.cMatchData.define("deconstruct", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { - m := mdArg(self) - out := make([]object.Value, 0, m.md.NGroups()) - for i := 1; i <= m.md.NGroups(); i++ { - out = append(out, groupValue(m, i)) - } - return object.NewArrayFromSlice(out) - }) + // pattern matching (`in [a, b]`); it is a genuine alias of #captures (shared + // record, so MatchData.instance_method(:deconstruct) == …(:captures)). + aliasBuiltin(vm.cMatchData, "deconstruct", "captures") // MatchData#deconstruct_keys(keys) is the symbol-keyed named captures, for // hash pattern matching (`in {name:}`). keys nil selects them all; otherwise // only the requested Symbol keys are returned, and the walk stops at the first @@ -1659,7 +1675,8 @@ func (vm *VM) installRegexp() { return object.Bool(a.equalTo(other)) } vm.cMatchData.define("==", mdEqual) - vm.cMatchData.define("eql?", mdEqual) + // #eql? is a genuine alias of #== (shared record). + aliasBuiltin(vm.cMatchData, "eql?", "==") vm.cMatchData.define("hash", func(_ *VM, self object.Value, _ []object.Value, _ *Proc) object.Value { m := mdArg(self) return object.IntValue(fnvHash(m.re.source+"\x00"+m.md.Str(0)) ^ int64(m.byteOff+m.md.Begin(0))) @@ -1702,6 +1719,16 @@ func (vm *VM) installRegexp() { vm.cMatchData.define("match", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { return mdArg(self).at(args[0]) }) + // MatchData#match_length(n) (Ruby 3.4) is the character length of that group's + // match, or nil when the group did not participate. + vm.cMatchData.define("match_length", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + v := mdArg(self).at(args[0]) + s, ok := v.(*object.String) + if !ok { + return object.NilV + } + return object.IntValue(int64(utf8.RuneCountInString(s.Str()))) + }) } // regexpMatchIndex implements Regexp#=~ and (via a String receiver) String#=~: diff --git a/internal/vm/regexp_residuals_test.go b/internal/vm/regexp_residuals_test.go index 1bb6e7ef..eb600f45 100644 --- a/internal/vm/regexp_residuals_test.go +++ b/internal/vm/regexp_residuals_test.go @@ -65,6 +65,37 @@ func TestRegexpMatchResiduals(t *testing.T) { }) } +// TestRegexpMatchDataAliases covers the genuine built-in aliases (shared method +// records) and the small Ruby-3.4 accessors added alongside them. +func TestRegexpMatchDataAliases(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`p Regexp.instance_method(:eql?) == Regexp.instance_method(:==)`, "true\n"}, + {`p MatchData.instance_method(:eql?) == MatchData.instance_method(:==)`, "true\n"}, + {`p MatchData.instance_method(:length) == MatchData.instance_method(:size)`, "true\n"}, + {`p MatchData.instance_method(:deconstruct) == MatchData.instance_method(:captures)`, "true\n"}, + // #deconstruct still behaves as #captures. + {`p "hi".match(/(.)(.)/).deconstruct`, "[\"h\", \"i\"]\n"}, + // MatchData#match_length: character length of a group, nil when absent. + {`p(/(.)(.)(\d+)(\d)/.match("THX1138.").match_length(3))`, "3\n"}, + {`p(/\d+(\w)?/.match("THX1138.").match_length(1))`, "nil\n"}, + {`m = "haystack".match(/(?t(?ack))/); p m.match_length(:t)`, "4\n"}, + // MatchData.allocate is undefined. + {`begin; MatchData.allocate; rescue => e; p e.class; end`, "NoMethodError\n"}, + }) +} + +// TestRegexpInitialize covers the private #initialize that always refuses: a +// frozen literal raises FrozenError, an already-initialized non-literal (and a +// subclass instance) raises TypeError, and #initialize is registered private. +func TestRegexpInitialize(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`p Regexp.private_instance_methods(false).include?(:initialize)`, "true\n"}, + {`begin; //.send(:initialize, ""); rescue => e; p e.class; end`, "FrozenError\n"}, + {`begin; Regexp.new("").send(:initialize, ""); rescue => e; p e.class; end`, "TypeError\n"}, + {`r = Class.new(Regexp).new(""); begin; r.send(:initialize, ""); rescue => e; p e.class; end`, "TypeError\n"}, + }) +} + // TestMatchDataMatchMethod covers MatchData#match(n) / #match(name) (Ruby 3.4): // the scalar subset of #[], returning nil for a non-participating group. func TestMatchDataMatchMethod(t *testing.T) { From 70b579e03762bdfe6e4782eeed288d57762c590b Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 1 Sep 2026 09:52:28 +0200 Subject: [PATCH 5/8] Regexp/MatchData: === via #to_str, named-only inspect, deconstruct_keys arity - Regexp#=== coerces a string-like operand through #to_str. - MatchData#inspect shows only named groups (by name) when the pattern has any, omitting unnamed numbered groups, matching MRI. - MatchData#deconstruct_keys raises ArgumentError (given 0, expected 1) with no argument instead of an index panic. Co-Authored-By: Claude Opus 4.8 --- internal/vm/regexp.go | 23 ++++++++++++++++++--- internal/vm/regexp_residuals_test.go | 31 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/internal/vm/regexp.go b/internal/vm/regexp.go index 4b37e7d1..2c3a39a3 100644 --- a/internal/vm/regexp.go +++ b/internal/vm/regexp.go @@ -175,15 +175,21 @@ func (m *MatchData) Inspect() string { return "# 0 for i := 1; i <= m.md.NGroups(); i++ { + nm, named := idxToName[i] + if hasNames && !named { + continue + } b.WriteByte(' ') - if nm, ok := idxToName[i]; ok { + if named { b.WriteString(nm) } else { b.WriteString(strconv.Itoa(i)) @@ -1431,6 +1437,14 @@ func (vm *VM) installRegexp() { }) vm.cRegexp.define("===", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { s, ok := stringLike(args[0]) + if !ok { + // A string-like object is accepted via #to_str (MRI's rb_check_string_type). + if vm.respondsToDynamic(args[0], "to_str") { + if str, isStr := vm.send(args[0], "to_str", nil, nil).(*object.String); isStr { + s, ok = str.Str(), true + } + } + } if !ok { // A non-string operand never matches and clears $~, as MRI does (so a // case/when over a non-string subject leaves no stale last match). @@ -1648,6 +1662,9 @@ func (vm *VM) installRegexp() { // only the requested Symbol keys are returned, and the walk stops at the first // key that is not a capture name (matching MRI). vm.cMatchData.define("deconstruct_keys", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + if len(args) == 0 { + raise("ArgumentError", "wrong number of arguments (given 0, expected 1)") + } return mdArg(self).deconstructKeys(args[0]) }) // MatchData#values_at(*args) selects groups by Integer index, name, or Range, diff --git a/internal/vm/regexp_residuals_test.go b/internal/vm/regexp_residuals_test.go index eb600f45..959a526e 100644 --- a/internal/vm/regexp_residuals_test.go +++ b/internal/vm/regexp_residuals_test.go @@ -96,6 +96,37 @@ func TestRegexpInitialize(t *testing.T) { }) } +// TestRegexpCaseCompareToStr covers Regexp#=== coercing a string-like operand +// through #to_str (and still clearing $~ / returning false for a non-string). +func TestRegexpCaseCompareToStr(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`o = Object.new; def o.to_str; "abc"; end; p(/b/ === o)`, "true\n"}, + {`o = Object.new; def o.to_str; "xyz"; end; p(/b/ === o)`, "false\n"}, + {`p(/b/ === 5)`, "false\n"}, + }) +} + +// TestMatchDataInspectNamed covers MatchData#inspect: when the pattern has named +// groups only those are shown (by name), omitting unnamed numbered groups; +// otherwise every numbered group is shown. +func TestMatchDataInspectNamed(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`p "abc def ghi".match(/(?\w+)\s+(?\w+)\s+(\w+)/).inspect`, + "\"#\"\n"}, + {`p "ab".match(/(.)(.)/).inspect`, + "\"#\"\n"}, + }) +} + +// TestMatchDataDeconstructKeysArity covers the zero-argument ArgumentError of +// MatchData#deconstruct_keys. +func TestMatchDataDeconstructKeysArity(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`begin; "a".match(/(?.)/).deconstruct_keys; rescue ArgumentError => e; p e.message; end`, + "\"wrong number of arguments (given 0, expected 1)\"\n"}, + }) +} + // TestMatchDataMatchMethod covers MatchData#match(n) / #match(name) (Ruby 3.4): // the scalar subset of #[], returning nil for a non-participating group. func TestMatchDataMatchMethod(t *testing.T) { From ca6caa20a5cc6f9ae75a89f5c738c4f32e5e9268 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 1 Sep 2026 09:56:15 +0200 Subject: [PATCH 6/8] Regexp: #to_str source coercion in .new, #to_int index in last_match - Regexp.new/compile coerce a non-String/Regexp source through #to_str, raising TypeError ("can't convert X into String (X#to_str gives Y)") when it yields a non-String. String-building factored into regexpFromString. - Regexp.last_match coerces a non-Integer/String/Symbol index via #to_int. Co-Authored-By: Claude Opus 4.8 --- internal/vm/regexp.go | 49 ++++++++++++++++++++++------ internal/vm/regexp_residuals_test.go | 18 ++++++++++ 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/internal/vm/regexp.go b/internal/vm/regexp.go index 2c3a39a3..7e1922da 100644 --- a/internal/vm/regexp.go +++ b/internal/vm/regexp.go @@ -278,21 +278,38 @@ func (vm *VM) regexpNew(args []object.Value) object.Value { } return r case *object.String: - flags, fixedEnc, noEnc := "", false, false - if len(args) >= 2 { - flags = regexpOptionFlags(args[1]) - fixedEnc, noEnc = regexpEncodingBits(args[1]) - } - r := vm.compileRegexp(src.Str(), flags).(*Regexp) - r.fixedEnc, r.noEnc, r.timeout = fixedEnc, noEnc, timeout - r.srcEnc = src.EncName() - return r + return vm.regexpFromString(src, args, timeout) default: + // A non-String, non-Regexp source is coerced with #to_str, matching MRI. + if vm.respondsToDynamic(args[0], "to_str") { + conv := vm.send(args[0], "to_str", nil, nil) + s, ok := conv.(*object.String) + if !ok { + raise("TypeError", "can't convert %s into String (%s#to_str gives %s)", + classNameOf(args[0]), classNameOf(args[0]), classNameOf(conv)) + } + return vm.regexpFromString(s, args, timeout) + } raise("TypeError", "no implicit conversion of %s into String", classNameOf(args[0])) return object.NilVal() } } +// regexpFromString builds a Regexp from a String source and the optional second +// (options) argument, tagging the FIXEDENCODING/NOENCODING bits, timeout and the +// source encoding. Shared by the direct String path and the #to_str-coerced path. +func (vm *VM) regexpFromString(src *object.String, args []object.Value, timeout object.Value) object.Value { + flags, fixedEnc, noEnc := "", false, false + if len(args) >= 2 { + flags = regexpOptionFlags(args[1]) + fixedEnc, noEnc = regexpEncodingBits(args[1]) + } + r := vm.compileRegexp(src.Str(), flags).(*Regexp) + r.fixedEnc, r.noEnc, r.timeout = fixedEnc, noEnc, timeout + r.srcEnc = src.EncName() + return r +} + // regexpKwHash returns the trailing keyword Hash of a Regexp.new argument list, // or nil when the last argument is not a Hash (positional options). func regexpKwHash(args []object.Value) *object.Hash { @@ -1356,7 +1373,19 @@ func (vm *VM) installRegexp() { if len(args) == 0 { return md } - return vm.send(md, "[]", []object.Value{args[0]}, nil) + // An index argument that is not already an Integer/String/Symbol is + // coerced to an Integer via #to_int, matching MRI's rb_reg_nth_match path. + key := args[0] + switch key.(type) { + case object.Integer, *object.String, object.Symbol: + default: + if vm.respondsToDynamic(key, "to_int") { + if iv, ok := vm.send(key, "to_int", nil, nil).(object.Integer); ok { + key = iv + } + } + } + return vm.send(md, "[]", []object.Value{key}, nil) }} // Regexp.union(pat, ...) / Regexp.union([pat, ...]) builds one Regexp matching diff --git a/internal/vm/regexp_residuals_test.go b/internal/vm/regexp_residuals_test.go index 959a526e..66d35592 100644 --- a/internal/vm/regexp_residuals_test.go +++ b/internal/vm/regexp_residuals_test.go @@ -96,6 +96,24 @@ func TestRegexpInitialize(t *testing.T) { }) } +// TestRegexpNewToStr covers Regexp.new coercing a non-String/Regexp source via +// #to_str, and the TypeError when #to_str yields a non-String. +func TestRegexpNewToStr(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`o = Object.new; def o.to_str; "a.b"; end; p Regexp.new(o).source`, "\"a.b\"\n"}, + {`o = Object.new; def o.to_str; []; end; begin; Regexp.new(o); rescue TypeError => e; p(e.message.include?("can't convert Object into String")); end`, "true\n"}, + {`begin; Regexp.new(1); rescue => e; p e.class; end`, "TypeError\n"}, + }) +} + +// TestRegexpLastMatchToInt covers Regexp.last_match coercing a non-Integer index +// argument through #to_int. +func TestRegexpLastMatchToInt(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`"THX1138".match(/(.)(.)/); o = Object.new; def o.to_int; 1; end; p Regexp.last_match(o)`, "\"T\"\n"}, + }) +} + // TestRegexpCaseCompareToStr covers Regexp#=== coercing a string-like operand // through #to_str (and still clearing $~ / returning false for a non-string). func TestRegexpCaseCompareToStr(t *testing.T) { From 0e4d01ad3368abcf3dd6ae8e7fdd8c6fba60a673 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 1 Sep 2026 09:59:20 +0200 Subject: [PATCH 7/8] Regexp#to_s: hoist a single whole-pattern option/non-capturing group /(?i:.)/ => "(?i-mx:.)", /(?:x)/ => "(?-mix:x)", /(?mmmmix-miiiix:)/ => "(?-mix:)". A group that closes before the end, or a named / look-around / capturing group, stays wrapped, matching MRI. singleWholeGroup respects escapes and character classes; mergeInlineFlags folds the hoisted options. Co-Authored-By: Claude Opus 4.8 --- internal/vm/regexp.go | 86 +++++++++++++++++++++++++++- internal/vm/regexp_residuals_test.go | 20 +++++++ 2 files changed, 103 insertions(+), 3 deletions(-) diff --git a/internal/vm/regexp.go b/internal/vm/regexp.go index 7e1922da..8357399a 100644 --- a/internal/vm/regexp.go +++ b/internal/vm/regexp.go @@ -126,17 +126,97 @@ func (r *Regexp) isFixedEncoding() bool { func (r *Regexp) ToS() string { // Ruby's Regexp#to_s renders the (?on-off:src) form, where the on-set is the // present flags and the off-set is the absent ones, always in m, i, x order. - on := orderFlags(r.flags) + // When the whole pattern is a single option/non-capturing group spanning the + // entire source, MRI hoists that group's options into the outer form and drops + // the wrapping group, e.g. /(?i:.)/ => "(?i-mx:.)" and /(?:x)/ => "(?-mix:x)". + base := r.flags + src := r.source + if gon, goff, body, ok := singleWholeGroup(src); ok { + src = body + base = mergeInlineFlags(r.flags, gon, goff) + } + on := orderFlags(base) off := "" for _, f := range "mix" { - if !strings.ContainsRune(r.flags, f) { + if !strings.ContainsRune(base, f) { off += string(f) } } if off != "" { off = "-" + off } - return "(?" + on + off + ":" + escapeForwardSlashes(r.source) + ")" + return "(?" + on + off + ":" + escapeForwardSlashes(src) + ")" +} + +// singleWholeGroup reports whether src is exactly one option or non-capturing +// group — `(?flags:…)`, `(?flags-flags:…)` or `(?:…)` — whose closing paren is +// the last character, returning the group's on/off inline flag letters and its +// body. Named groups, look-around and capturing groups do not qualify (MRI keeps +// those wrapped), and a group that closes before the end (so more of the pattern +// follows) does not span the whole source. +func singleWholeGroup(src string) (on, off, body string, ok bool) { + if !strings.HasPrefix(src, "(?") { + return "", "", "", false + } + i := 2 + for i < len(src) && (src[i] == 'm' || src[i] == 'i' || src[i] == 'x') { + on += string(src[i]) + i++ + } + if i < len(src) && src[i] == '-' { + i++ + for i < len(src) && (src[i] == 'm' || src[i] == 'i' || src[i] == 'x') { + off += string(src[i]) + i++ + } + } + if i >= len(src) || src[i] != ':' { + return "", "", "", false + } + bodyStart := i + 1 + depth, inClass := 1, false + for j := bodyStart; j < len(src); j++ { + switch src[j] { + case '\\': + j++ // skip the escaped byte + case '[': + if !inClass { + inClass = true + } + case ']': + inClass = false + case '(': + if !inClass { + depth++ + } + case ')': + if !inClass { + depth-- + if depth == 0 { + // Spans the whole source only if this is the final byte. + if j == len(src)-1 { + return on, off, src[bodyStart:j], true + } + return "", "", "", false + } + } + } + } + return "", "", "", false +} + +// mergeInlineFlags folds a hoisted group's inline on/off flag letters into the +// Regexp's own flags: a flag is on when the outer flags or the group's on-set +// enable it and the group's off-set does not disable it. +func mergeInlineFlags(outer, on, off string) string { + out := "" + for _, f := range "mix" { + enabled := strings.ContainsRune(outer, f) || strings.ContainsRune(on, f) + if enabled && !strings.ContainsRune(off, f) { + out += string(f) + } + } + return out } // Inspect renders /source/flags, escaping unescaped '/' in the source. diff --git a/internal/vm/regexp_residuals_test.go b/internal/vm/regexp_residuals_test.go index 66d35592..be6181bb 100644 --- a/internal/vm/regexp_residuals_test.go +++ b/internal/vm/regexp_residuals_test.go @@ -39,6 +39,26 @@ func TestRegexpQuoteEscapeResiduals(t *testing.T) { }) } +// TestRegexpToSCollapse covers Regexp#to_s hoisting a single whole-pattern option +// or non-capturing group into the outer (?on-off:body) form, and leaving patterns +// that are not one spanning group wrapped. +func TestRegexpToSCollapse(t *testing.T) { + runCases(t, []struct{ src, want string }{ + {`puts(/(?i:.)/.to_s)`, "(?i-mx:.)\n"}, + {`puts(/(?i:nothing)/.to_s)`, "(?i-mx:nothing)\n"}, + {`puts(/(?:x)/.to_s)`, "(?-mix:x)\n"}, + {`puts(/(?i-m:a)/.to_s)`, "(?i-mx:a)\n"}, + {`puts(/(?mmmmix-miiiix:)/.to_s)`, "(?-mix:)\n"}, + {`puts(/(?i:a)/i.to_s)`, "(?i-mx:a)\n"}, + // Not one spanning group: the wrapper stays. + {`puts(/(?i:a)b/.to_s)`, "(?-mix:(?i:a)b)\n"}, + {`puts(/(?i:a)(?m:b)/.to_s)`, "(?-mix:(?i:a)(?m:b))\n"}, + {`puts(/(?=5)/.to_s)`, "(?-mix:(?=5))\n"}, + // A group closing inside a character class does not end the span early. + {`puts(/(?:[)])/.to_s)`, "(?-mix:[)])\n"}, + }) +} + // TestRegexpInspectSlash covers the forward-slash escaping of #inspect and #to_s, // without double-escaping an already-escaped slash. func TestRegexpInspectSlash(t *testing.T) { From 93c7e2cd4c648518c7227291cdb7e5d76fe5a28b Mon Sep 17 00:00:00 2001 From: tannevaled Date: Tue, 1 Sep 2026 10:11:24 +0200 Subject: [PATCH 8/8] Tests: cover regexp/matchdata residual branches to 100% Add eval residual tests (union coercion, quote/escape, inspect slashes, match nil/symbol/block, MatchData#match/#match_length, aliases, initialize, === to_str, named-only inspect, deconstruct_keys arity, to_s group hoist, Regexp.new #to_str, last_match #to_int) plus a white-box internal test for the defensive branches of escapeForwardSlashes and singleWholeGroup that a valid compiled Regexp cannot reach. Co-Authored-By: Claude Opus 4.8 --- internal/vm/regexp_cover_internal_test.go | 51 +++++++++++++++++++++++ internal/vm/regexp_residuals_test.go | 7 ++++ 2 files changed, 58 insertions(+) create mode 100644 internal/vm/regexp_cover_internal_test.go diff --git a/internal/vm/regexp_cover_internal_test.go b/internal/vm/regexp_cover_internal_test.go new file mode 100644 index 00000000..e9a39d01 --- /dev/null +++ b/internal/vm/regexp_cover_internal_test.go @@ -0,0 +1,51 @@ +package vm + +import "testing" + +// TestEscapeForwardSlashesInternal exercises escapeForwardSlashes directly, +// including the defensive branch where a backslash is the final byte (a lone +// trailing backslash never survives a compiled Regexp, so it is unreachable +// through the interpreter but must still be handled). +func TestEscapeForwardSlashesInternal(t *testing.T) { + cases := []struct{ in, want string }{ + {"", ""}, + {"a/b", "a\\/b"}, + {"\\/", "\\/"}, // already-escaped slash is not double-escaped + {"\\d/", "\\d\\/"}, // backslash escapes 'd', then the slash is escaped + {"a\\", "a\\"}, // trailing backslash: the i+1 < len guard is false + } + for _, c := range cases { + if got := escapeForwardSlashes(c.in); got != c.want { + t.Errorf("escapeForwardSlashes(%q) = %q, want %q", c.in, got, c.want) + } + } +} + +// TestSingleWholeGroupInternal exercises singleWholeGroup directly, including the +// branches unreachable via the interpreter: an unterminated group (invalid, so it +// never compiles) and an escape inside the group body. +func TestSingleWholeGroupInternal(t *testing.T) { + cases := []struct { + src string + on, off, body string + ok bool + }{ + {"(?i:a)", "i", "", "a", true}, + {"(?i-m:a)", "i", "m", "a", true}, + {"(?:x)", "", "", "x", true}, + {"(?:\\)x)", "", "", "\\)x", true}, // escaped ')' inside the body + {"(?:[)])", "", "", "[)]", true}, // ')' inside a character class + {"(?:(a))", "", "", "(a)", true}, // a nested group raises and lowers depth + {"abc", "", "", "", false}, // no group prefix + {"(?=5)", "", "", "", false}, // look-ahead: no option ':' spec + {"(?i:a)b", "", "", "", false}, // group closes before the end + {"(?i:abc", "", "", "", false}, // unterminated group (defensive path) + } + for _, c := range cases { + on, off, body, ok := singleWholeGroup(c.src) + if ok != c.ok || on != c.on || off != c.off || body != c.body { + t.Errorf("singleWholeGroup(%q) = (%q,%q,%q,%v), want (%q,%q,%q,%v)", + c.src, on, off, body, ok, c.on, c.off, c.body, c.ok) + } + } +} diff --git a/internal/vm/regexp_residuals_test.go b/internal/vm/regexp_residuals_test.go index be6181bb..4a3dd8cf 100644 --- a/internal/vm/regexp_residuals_test.go +++ b/internal/vm/regexp_residuals_test.go @@ -36,6 +36,13 @@ func TestRegexpQuoteEscapeResiduals(t *testing.T) { {`print Regexp.escape("\t\n\r\f\v")`, "\\t\\n\\r\\f\\v"}, {`print Regexp.quote(:symbol)`, "symbol"}, {`p Regexp.method(:escape) == Regexp.method(:quote)`, "true\n"}, + // #to_str coercion of a non-String/Symbol operand, and its failure modes. + {`o = Object.new; def o.to_str; "a.b"; end; print Regexp.quote(o)`, "a\\.b"}, + {`begin; Regexp.quote(Object.new); rescue => e; p e.class; end`, "TypeError\n"}, + {`o = Object.new; def o.to_str; []; end; begin; Regexp.quote(o); rescue => e; p e.class; end`, "TypeError\n"}, + // A source containing a backslash exercises the escape branch of the '/' + // escaping without double-escaping the already-escaped slash. + {`p(/\d\/x/)`, "/\\d\\/x/\n"}, }) }