Skip to content

mruby-regexp: raise TypeError for a String argument to String#=~ - #6988

Merged
matz merged 1 commit into
mruby:masterfrom
takumin:string-match-op-type-error
Aug 2, 2026
Merged

mruby-regexp: raise TypeError for a String argument to String#=~#6988
matz merged 1 commit into
mruby:masterfrom
takumin:string-match-op-type-error

Conversation

@takumin

@takumin takumin commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

String#=~ defined by mruby-regexp delegates to re =~ self. When the argument is a
String, that dispatches back to String#=~, and the two recurse until the stack is
exhausted.

"abc" =~ "b"
# CRuby: TypeError (type mismatch: String given)
# mruby: SystemStackError

CRuby's rb_str_match() branches on the argument type: String raises TypeError,
Regexp matches, and anything else is delegated to that object's =~. Rejecting
everything that is not a Regexp would therefore be too strict -- it would break the
duck-typed argument that CRuby accepts. Only the String case needs an explicit guard;
delegation already produces CRuby-compatible behaviour for the rest.

argument CRuby 4.0.6 mruby (before) mruby (after)
String TypeError SystemStackError TypeError
Regexp match position / nil same same
other, e.g. 42 NoMethodError (Object#=~ is gone in 4.0) same same

Kernel#!~ is !(self =~ y), so "abc" !~ "b" now raises TypeError as well, matching
CRuby.

Tests for both the matching case and the TypeError are added to
mrbgems/mruby-regexp/test/regexp.rb.

One related divergence is left alone: "abc" =~ nil returns nil on CRuby (via
NilClass#=~) but raises NoMethodError on mruby. That is a NilClass question rather
than a String#=~ one, so it is out of scope here.

Summary by CodeRabbit

  • Bug Fixes
    • Updated String#=~ to raise TypeError when passed a string, matching standard Ruby behavior.
    • Preserved pattern matching for supported inputs.
  • Tests
    • Added coverage for matching, non-matching, and invalid string arguments to String#=~ and String#!~.

String#=~ delegated unconditionally to `re =~ self`. When the argument
was a String, that dispatched back to String#=~ and the two recursed
until the stack was exhausted, so `"abc" =~ "b"` raised SystemStackError.

CRuby's rb_str_match() branches on the argument type: a String raises
TypeError, a Regexp matches, and anything else is delegated to its own
=~. Only the String case needs handling here -- delegation already
covers the rest, so reject a String up front and leave duck-typed
arguments working.

Kernel#!~ is defined as `!(self =~ y)`, so `"abc" !~ "b"` now raises the
same TypeError as CRuby too.
@takumin
takumin requested a review from matz as a code owner August 2, 2026 05:30
@coderabbitai

coderabbitai Bot commented Aug 2, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 9d8706aa-359b-4177-bc6b-a6b69e960684

📥 Commits

Reviewing files that changed from the base of the PR and between d36d48f and 5bd8a26.

📒 Files selected for processing (2)
  • mrbgems/mruby-regexp/mrblib/string_regexp.rb
  • mrbgems/mruby-regexp/test/regexp.rb

📝 Walkthrough

Walkthrough

String#=~ now raises TypeError for string operands before pattern dispatch. Tests cover matching, non-matching, and string-operand behavior for String#=~ and String#!~.

Changes

String regexp operand validation

Layer / File(s) Summary
Operand validation and regression coverage
mrbgems/mruby-regexp/mrblib/string_regexp.rb, mrbgems/mruby-regexp/test/regexp.rb
String#=~ rejects string operands with TypeError and delegates other operands. Tests cover matching, non-matching, and rejection behavior for String#=~ and String#!~.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Suggested reviewers: matz

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: raising TypeError for a String argument to String#=~.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@matz
matz merged commit 11ff450 into mruby:master Aug 2, 2026
3 checks passed
@takumin
takumin deleted the string-match-op-type-error branch August 2, 2026 10:01
@takumin takumin mentioned this pull request Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants