mruby-regexp: raise TypeError for a String argument to String#=~ - #6988
Merged
Conversation
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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthrough
ChangesString regexp operand validation
Estimated code review effort: 1 (Trivial) | ~5 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
Merged
This was referenced Aug 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
String#=~defined bymruby-regexpdelegates tore =~ self. When the argument is aString, that dispatches back to
String#=~, and the two recurse until the stack isexhausted.
CRuby's
rb_str_match()branches on the argument type:StringraisesTypeError,Regexpmatches, and anything else is delegated to that object's=~. Rejectingeverything that is not a
Regexpwould therefore be too strict -- it would break theduck-typed argument that CRuby accepts. Only the
Stringcase needs an explicit guard;delegation already produces CRuby-compatible behaviour for the rest.
StringTypeErrorSystemStackErrorTypeErrorRegexpnil42NoMethodError(Object#=~is gone in 4.0)Kernel#!~is!(self =~ y), so"abc" !~ "b"now raisesTypeErroras well, matchingCRuby.
Tests for both the matching case and the
TypeErrorare added tomrbgems/mruby-regexp/test/regexp.rb.One related divergence is left alone:
"abc" =~ nilreturnsnilon CRuby (viaNilClass#=~) but raisesNoMethodErroron mruby. That is aNilClassquestion ratherthan a
String#=~one, so it is out of scope here.Summary by CodeRabbit
String#=~to raiseTypeErrorwhen passed a string, matching standard Ruby behavior.String#=~andString#!~.