ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: "Eregon (Benoit Daloze)" <noreply@ruby-lang.org>
To: ruby-core@ruby-lang.org
Subject: [ruby-core:108377] [Ruby master Bug#18729] Method#owner and UnboundMethod#owner are incorrect after using Module#public/protected/private
Date: Sat, 23 Apr 2022 11:34:15 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-97409.20220423113413.772@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-18729.20220414143806.772@ruby-lang.org

Issue #18729 has been updated by Eregon (Benoit Daloze).


On the PR, there is a discussion what should be the return value of `super_method` for a more complex example:
```ruby
class C
  def foo
    puts caller(0), nil
    ['C']
  end
end

module Mod
  private def foo
    ['.'] + super
  end
end

obj = C.new
obj.extend(Mod)

class << obj
  public :foo
end

p obj.foo # => [".", "C"]

p obj.singleton_class.ancestors # => [sclass, Mod, C, Object, Kernel, BasicObject]
p obj.method(:foo).owner # => should be sclass, NOT Mod
p obj.method(:foo).super_method # => should be C#foo, NOT Mod#foo
```

So (public) sclass#foo actually super's into C#foo (because only 1 dot printed), and never goes into private Mod#foo.
Hence `Method#super_method` should reflect that as much as possible.
But the PR instead changes the last result to Mod#foo, which seems incorrect.

----------------------------------------
Bug #18729: Method#owner and UnboundMethod#owner are incorrect after using Module#public/protected/private
https://bugs.ruby-lang.org/issues/18729#change-97409

* Author: Eregon (Benoit Daloze)
* Status: Open
* Priority: Normal
* ruby -v: ruby 3.1.1p18 (2022-02-18 revision 53f5fc4236) [x86_64-linux]
* Backport: 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
The #owner should be "the class or module that defines the method".
Or in other words, the owner is the module which has the method table containing that method.

This generally holds, and it seems very likely this assumption is relied upon (e.g., when decorating a method, undefining it, etc).
But the returned value on CRuby is incorrect for this case:
```ruby
class A
  protected def foo
    :A
  end
end

class B < A
  p [instance_method(:foo), instance_method(:foo).owner, instance_methods(false), A.instance_methods(false)]
  public :foo
  p [instance_method(:foo), instance_method(:foo).owner, instance_methods(false), A.instance_methods(false)]
end
```

It gives:
```
[#<UnboundMethod: B(A)#foo() owner.rb:2>, A, [], [:foo]]
[#<UnboundMethod: B(A)#foo() owner.rb:2>, A, [:foo], [:foo]]
```

So `UnboundMethod#owner` says `A`, but clearly there is a :foo method entry in B created by `public :foo`, and that is shown through `B.instance_methods(false)`.

The expected output is:

```
[#<UnboundMethod: B(A)#foo() owner.rb:2>, A, [], [:foo]]
[#<UnboundMethod: B#foo() owner.rb:2>, B, [:foo], [:foo]]
```



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

  parent reply	other threads:[~2022-04-23 11:34 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-14 14:38 [ruby-core:108235] [Ruby master Bug#18729] Method#owner and UnboundMethod#owner are incorrect after using Module#public/protected/private Eregon (Benoit Daloze)
2022-04-14 14:44 ` [ruby-core:108236] " Eregon (Benoit Daloze)
2022-04-14 23:00 ` [ruby-core:108244] " jeremyevans0 (Jeremy Evans)
2022-04-15 10:37 ` [ruby-core:108260] " Eregon (Benoit Daloze)
2022-04-15 14:51 ` [ruby-core:108265] " jeremyevans0 (Jeremy Evans)
2022-04-19 18:30 ` [ruby-core:108297] " Eregon (Benoit Daloze)
2022-04-22  2:36 ` [ruby-core:108363] " mame (Yusuke Endoh)
2022-04-22  9:56 ` [ruby-core:108369] " Eregon (Benoit Daloze)
2022-04-22 16:11 ` [ruby-core:108374] " jeremyevans0 (Jeremy Evans)
2022-04-23 11:34 ` Eregon (Benoit Daloze) [this message]
2022-04-23 13:09 ` [ruby-core:108382] " Eregon (Benoit Daloze)
2022-04-25 19:45 ` [ruby-core:108401] " jeremyevans0 (Jeremy Evans)
2022-04-27 18:22 ` [ruby-core:108422] " Eregon (Benoit Daloze)
2022-04-27 19:00 ` [ruby-core:108423] " jeremyevans0 (Jeremy Evans)
2022-05-17 10:09 ` [ruby-core:108582] " Eregon (Benoit Daloze)
2022-06-09  9:35 ` [ruby-core:108828] " mame (Yusuke Endoh)
2022-06-09 10:33 ` [ruby-core:108834] " Eregon (Benoit Daloze)
2022-06-16  9:11 ` [ruby-core:108956] " matz (Yukihiro Matsumoto)
2022-06-16  9:16 ` [ruby-core:108957] " matz (Yukihiro Matsumoto)
2022-06-16 10:06 ` [ruby-core:108958] " Eregon (Benoit Daloze)
2022-06-16 10:09 ` [ruby-core:108959] " Eregon (Benoit Daloze)
2022-06-16 14:15 ` [ruby-core:108960] " matz (Yukihiro Matsumoto)
2022-06-16 14:21 ` [ruby-core:108961] " matz (Yukihiro Matsumoto)
2022-06-16 16:56 ` [ruby-core:108962] " Eregon (Benoit Daloze)
2022-08-15 11:01 ` [ruby-core:109480] " Eregon (Benoit Daloze)
2022-08-15 11:03 ` [ruby-core:109481] " Eregon (Benoit Daloze)
2022-08-15 13:08 ` [ruby-core:109485] " Eregon (Benoit Daloze)
2022-08-18  9:23 ` [ruby-core:109537] " Eregon (Benoit Daloze)
2022-08-18  9:35 ` [ruby-core:109541] " Eregon (Benoit Daloze)
2022-08-18 11:40 ` [ruby-core:109548] " Eregon (Benoit Daloze)
2022-09-22  3:46 ` [ruby-core:109987] " matz (Yukihiro Matsumoto)
2022-09-28 16:26 ` [ruby-core:110132] " Eregon (Benoit Daloze)
2022-09-28 17:26 ` [ruby-core:110134] " Eregon (Benoit Daloze)
2022-09-28 19:08 ` [ruby-core:110138] " Eregon (Benoit Daloze)

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-97409.20220423113413.772@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).