ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: xtkoba+ruby@gmail.com
To: ruby-core@ruby-lang.org
Subject: [ruby-core:102951] [Ruby master Bug#17725] Prepend breaks ability to override optimized methods
Date: Sat, 20 Mar 2021 06:42:44 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-91016.20210320064243.50739@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-17725.20210316172645.50739@ruby-lang.org

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/

  parent reply	other threads:[~2021-03-20  6:42 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-16 17:26 [ruby-core:102884] [Ruby master Bug#17725] Prepend Breaks Ability to Alias josh
2021-03-16 17:34 ` [ruby-core:102885] " josh
2021-03-16 18:15 ` [ruby-core:102886] " josh
2021-03-16 18:47 ` [ruby-core:102887] " josh
2021-03-17  1:09 ` [ruby-core:102895] [Ruby master Bug#17725] Prepend breaks ability to override optimized methods nobu
2021-03-17  1:15 ` [ruby-core:102896] " nobu
2021-03-17  1:24 ` [ruby-core:102897] " nobu
2021-03-18  4:35 ` [ruby-core:102911] " xtkoba+ruby
2021-03-20  6:42 ` xtkoba+ruby [this message]
2021-04-09 22:10 ` [ruby-core:103358] " merch-redmine
2021-08-01  9:27 ` [ruby-core:104738] " nagachika00
2021-11-24 15:04 ` [ruby-core:106266] " mk
2021-12-24  5:54 ` [ruby-core:106801] " nagachika (Tomoyuki Chikanaga)
2021-12-24  9:05 ` [ruby-core:106805] " nagachika (Tomoyuki Chikanaga)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-91016.20210320064243.50739@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).