Skip to content

Regexp/MatchData: advance MRI 4.0.6 conformance (union, quote/escape, to_s hoisting, match coercion/block, aliases, to_int/to_str, match globals) - #546

Merged
tannevaled merged 8 commits into
mainfrom
feat/agent-regexp
Sep 1, 2026
Merged

tannevaled merged 8 commits into
mainfrom
feat/agent-regexp

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Advance MRI-Ruby-4.0.6 conformance for the Regexp / MatchData / match-globals cluster. Measured against the rubyspec corpus (core/regexp/*, core/matchdata/*) and byte-for-byte against MRI; the corpus total rose from ~261 to 321 passing examples.

Regexp

  • Regexp.union restructured to MRI's recursion: a lone Regexp/#to_regexp is returned verbatim, an Array recurses, a lone String/Symbol becomes one quoted pattern, and a multi-pattern union joins each Regexp's #to_s (options ride along) with #to_str-quoted operands — a multi-arg Symbol (no #to_str) raises TypeError.
  • Regexp.quote/escape now escape -, space, # and the control chars (\n \r \f \t \v) exactly as rb_reg_quote, accept a Symbol, and share one method record so Regexp.method(:escape) == Regexp.method(:quote).
  • Regexp#inspect/#to_s escape unescaped / in the source; #to_s hoists a single whole-pattern option/non-capturing group, e.g. /(?i:.)/ → "(?i-mx:.)", /(?:x)/ → "(?-mix:x)".
  • Regexp#match: nil subject resets $~ and returns nil; Symbol/#to_str subjects coerce; a block yields the MatchData and returns its value. Regexp#=== coerces a string-like operand via #to_str.
  • Regexp.new/compile coerce a non-String/Regexp source via #to_str; Regexp.last_match coerces its index via #to_int.
  • Regexp#initialize is private and refuses re-initialization (FrozenError on a literal, TypeError otherwise); Regexp#eql? is a genuine alias of #==. Uninitialized-Regexp receivers (Regexp.allocate) raise TypeError.

MatchData

  • #match(n|name) and #match_length(n|name) (Ruby 3.4); #begin/#end/#offset (and byte variants) coerce a non-name argument via #to_int.
  • #inspect shows only named groups when the pattern has any; #deconstruct_keys raises ArgumentError with no argument.
  • #length, #deconstruct, #eql? are genuine aliases of #size, #captures, #==; MatchData.allocate is undefined (NoMethodError).

Out of scope / not addressed

The remaining corpus failures are encoding tracking (US-ASCII / UTF-16 / ISO-8859 / EUC / SJIS), engine limits in the regexp backend (duplicate named groups, \u/\N escapes, error-message wording), and native-subclass new→initialize dispatch — none reachable from regexp.go alone.

Verification

  • go test ./internal/vm/ and go test ./... green; GOOS=wasip1 GOARCH=wasm go build ./... OK; gofmt -l clean on changed files.
  • Every function changed in regexp.go is at 100% coverage (a white-box internal test covers the defensive branches a valid compiled Regexp cannot reach); repo total held at 99.9%.

🤖 Generated with Claude Code

tannevaled and others added 8 commits September 1, 2026 09:27
- 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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
… 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 <noreply@anthropic.com>
…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 <noreply@anthropic.com>
…ys 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 <noreply@anthropic.com>
- 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 <noreply@anthropic.com>
/(?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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 68801b4 into main Sep 1, 2026
7 checks passed
@tannevaled
tannevaled deleted the feat/agent-regexp branch September 1, 2026 08:57
tannevaled added a commit that referenced this pull request Sep 1, 2026
Locks in #544 (Complex + Rational), #545 (Hash methods), #546 (Regexp,
MatchData and match globals). Measured 18102 passing on the pinned corpus,
up from 17983.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant