From: xtkoba+ruby@...
Date: 2021-03-20T06:42:44+00:00
Subject: [ruby-core:102951] [Ruby master Bug#17725] Prepend breaks ability to override optimized methods

Issue #17725 has been updated by xtkoba (Tee KOBAYASHI).


Another example:
```ruby
'' != nil

module Blah
  def == other
    'blah blah'
  end
end
String.prepend(Blah)

p 'a' == 'b' # => false
```

It seems that `rb_vm_check_redefinition_opt_method` is not working correctly in these cases. If the issue were only with `String#==` and `String#+`, a (dirty) workaround would be as follows:
```
--- a/vm.c
+++ b/vm.c
@@ -1854,6 +1854,12 @@ rb_vm_check_redefinition_opt_method(cons
 
 	    ruby_vm_redefined_flag[bop] |= flag;
 	}
+	else if (me->def->body.iseq.iseqptr == (rb_iseq_t *)rb_str_equal) {
+	    ruby_vm_redefined_flag[BOP_EQ] |= STRING_REDEFINED_OP_FLAG;
+	}
+	else if (me->def->body.iseq.iseqptr == (rb_iseq_t *)rb_str_plus) {
+	    ruby_vm_redefined_flag[BOP_PLUS] |= STRING_REDEFINED_OP_FLAG;
+	}
     }
 }
 
```

----------------------------------------
Bug #17725: Prepend breaks ability to override optimized methods
https://bugs.ruby-lang.org/issues/17725#change-91016

* Author: joshuadreed (Josh Reed)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin19]
* Backport: 2.5: DONTNEED, 2.6: DONTNEED, 2.7: DONTNEED, 3.0: REQUIRED
----------------------------------------
Prepending any module to `String` and `Hash` (and possibly other or all classes?) blocks the ability to `alias` 

As an example:
``` ruby
module Dummy; end
# String.prepend(Dummy)
class String
  alias_method(:old_plus, :+)
  def + other
    puts 'blah blah'
    old_plus(other)
  end
end

'a' + 'b'

> blah blah
```

``` ruby
module Dummy; end
String.prepend(Dummy)
class String
  alias_method(:old_plus, :+)
  def + other
    puts 'blah blah'
    old_plus(other)
  end
end

'a' + 'b'

>
```

Prepending after an `alias` does not affect the previous `alias`



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>