Bug #9581


`=~` defined on a subclass of `String` is sometimes ignored, and `String#=~` is called instead

Added by sawa (Tsuyoshi Sawada) over 10 years ago. Updated over 10 years ago.

Target version:


As is reported on StackOverflow ( by Gabriel, overridden =~ on a subclass of String is sometimes ignored, and the original String#=~ is called. Particularly, when we have:

class MyString < String
  def =~ re; :foo end
s ="abc")

these give the correct result:

r = /abc/; s =~ r   # => :foo
s.send(:=~, r)      # => :foo
s.send(:=~, /abc/)  # => :foo

but in this case, MyString#=~ is ignored, and String#=~ is called instead:

s =~ /abc/          # => 0

Updated by srawlins (Sam Rawlins) over 10 years ago

Three functions relate directly to this bug:

  • match_op_gen() in parse.y [1], which compiles two expressions separated by a =~ into some kind of MATCH node. When it sees that the right side is a literal Regexp, it returns a NODE_MATCH3 node.
  • iseq_compile_each() in compile.c [2], which upon seeing NODE_MATCH3 flips the receiver and the value. Then, if the InstructionSequence option specialized_instruction is true [3] (which it is, by default), the code falls to line 4804, which creates an instruction for:
  • opt_regexpmatch2(), in insns.def [4]. In this function, we optimize the instruction to just rb_reg_match(obj1,obj2) after finding that RB_TYPE_P(obj2, T_STRING). Ah ha!

The solution is a different test to answer "is obj2 a String?":

diff --git a/insns.def b/insns.def
index 7942804..7ef4c4c 100644
--- a/insns.def
+++ b/insns.def
@@ -2154,7 +2154,7 @@ opt_regexpmatch2
 (VALUE obj2, VALUE obj1)
 (VALUE val)
-    if (RB_TYPE_P(obj2, T_STRING) &&
+    if (CLASS_OF(obj2) == rb_cString &&
        val = rb_reg_match(obj1, obj2);

I don't think this represents a great slowdown.

[3] The bug can be temporarily remedied with today's Ruby using:

RubyVM::InstructionSequence.compile_option = { specialized_instruction: false }
s =~ /abc/  #=> :foo


Updated by normalperson (Eric Wong) over 10 years ago

Thanks both, I will commit the following once others have reviewed:
(git:// bug9581)

Actions #3

Updated by Anonymous over 10 years ago

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

Applied in changeset r45318.

insns.def (opt_regexpmatch2): respect redefined match op

  • insns.def (opt_regexpmatch2): respect redefined match op
    Thanks to Sam Rawlins for the fix.
  • test/ruby/test_string.rb: test based on Tsuyoshi Sawada's report
    [Bug #9581]

Also available in: Atom PDF