ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:72186] [Ruby trunk - Bug #11826] [Open] In 'prepended', 'class_eval' rewriting a method take no effect
       [not found] <redmine.issue-11826.20151216141830@ruby-lang.org>
@ 2015-12-16 14:18 ` cichol
  2015-12-16 15:29 ` [ruby-core:72188] [Ruby trunk - Bug #11826] After prepending a module, rewrite Hash#[] takes no effect for calls like Hash.new[:a] cichol
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 4+ messages in thread
From: cichol @ 2015-12-16 14:18 UTC (permalink / raw
  To: ruby-core

Issue #11826 has been reported by cichol tsai.

----------------------------------------
Bug #11826: In 'prepended', 'class_eval' rewriting a method take no effect
https://bugs.ruby-lang.org/issues/11826

* Author: cichol tsai
* Status: Open
* Priority: Normal
* Assignee: 
* ruby -v: 
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
~~~
module M
  def self.prepended(clz)
    clz.class_eval do
      def [](k)
        p 1
      end
    end
  end
end

module N
  def self.included(clz)
    clz.class_eval do 
      def []=(k, v)
        p 2
      end
    end
  end
end

class Hash
  prepend M
  include N
end

Hash.new[1]
Hash.new[1]=1
~~~

Running this example gives no output, which is expected to be '1 2'.
I am using 'ruby 2.2.2p95 (2015-04-13 revision 50295) [x64-mingw32]'.
Thanks in advanced.



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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:72188] [Ruby trunk - Bug #11826] After prepending a module, rewrite Hash#[] takes no effect for calls like Hash.new[:a]
       [not found] <redmine.issue-11826.20151216141830@ruby-lang.org>
  2015-12-16 14:18 ` [ruby-core:72186] [Ruby trunk - Bug #11826] [Open] In 'prepended', 'class_eval' rewriting a method take no effect cichol
@ 2015-12-16 15:29 ` cichol
  2015-12-17  1:23 ` [ruby-core:72197] [Ruby trunk - Bug #11826] [Assigned] " shugo
  2015-12-17 22:47 ` [ruby-core:72225] [Ruby trunk - Bug #11826] " shugo
  3 siblings, 0 replies; 4+ messages in thread
From: cichol @ 2015-12-16 15:29 UTC (permalink / raw
  To: ruby-core

Issue #11826 has been updated by cichol tsai.

Subject changed from In 'prepended', 'class_eval' rewriting a method take no effect to After prepending a module, rewrite Hash#[] takes no effect for calls like Hash.new[:a]
ruby -v set to ruby 2.2.2p95 (2015-04-13 revision 50295) [x64-mingw32]

~~~
module M
end

class Hash
  prepend M
end

class Hash
  def [](k)
    p 3
  end

  def map
    p 4
  end
end

h = {a: 1}
h[:a] #=> return 1 and without print 3
h.send(:[], :a) #=> print 3
h.map # => print 4
~~~

A shorter snippet for the issue.
It seems like an issue only for [] method, maybe something about syntax.
But if use 'include' instead of 'prepend', the 'h[:a]' can give an output.
Please help me check if it is a bug, sorry for bothering.

----------------------------------------
Bug #11826: After prepending a module, rewrite Hash#[] takes no effect for calls like Hash.new[:a]
https://bugs.ruby-lang.org/issues/11826#change-55600

* Author: cichol tsai
* Status: Open
* Priority: Normal
* Assignee: 
* ruby -v: ruby 2.2.2p95 (2015-04-13 revision 50295) [x64-mingw32]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
~~~
module M
  def self.prepended(clz)
    clz.class_eval do
      def [](k)
        p 1
      end
    end
  end
end

module N
  def self.included(clz)
    clz.class_eval do 
      def []=(k, v)
        p 2
      end
    end
  end
end

class Hash
  prepend M
  include N
end

Hash.new[1]
Hash.new[1]=1
~~~

Running this example gives no output, which is expected to be '1 2'.
I am using 'ruby 2.2.2p95 (2015-04-13 revision 50295) [x64-mingw32]'.
Thanks in advanced.



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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [ruby-core:72197] [Ruby trunk - Bug #11826] [Assigned] After prepending a module, rewrite Hash#[] takes no effect for calls like Hash.new[:a]
       [not found] <redmine.issue-11826.20151216141830@ruby-lang.org>
  2015-12-16 14:18 ` [ruby-core:72186] [Ruby trunk - Bug #11826] [Open] In 'prepended', 'class_eval' rewriting a method take no effect cichol
  2015-12-16 15:29 ` [ruby-core:72188] [Ruby trunk - Bug #11826] After prepending a module, rewrite Hash#[] takes no effect for calls like Hash.new[:a] cichol
@ 2015-12-17  1:23 ` shugo
  2015-12-17 22:47 ` [ruby-core:72225] [Ruby trunk - Bug #11826] " shugo
  3 siblings, 0 replies; 4+ messages in thread
From: shugo @ 2015-12-17  1:23 UTC (permalink / raw
  To: ruby-core

Issue #11826 has been updated by Shugo Maeda.

Status changed from Open to Assigned
Assignee set to Koichi Sasada

cichol tsai wrote:
> It seems like an issue only for [] method, maybe something about syntax.
> But if use 'include' instead of 'prepend', the 'h[:a]' can give an output.
> Please help me check if it is a bug, sorry for bothering.

It seems that redefinition check of optimized methods doesn't work properly for prepended classes.
Is the following patch correct, ko1?

```diff
diff --git a/vm.c b/vm.c
index a03e068..cf528e8 100644
--- a/vm.c
+++ b/vm.c
@@ -1400,6 +1400,9 @@ static void
 rb_vm_check_redefinition_opt_method(const rb_method_entry_t *me, VALUE klass)
 {
     st_data_t bop;
+    if (RB_TYPE_P(klass, T_ICLASS) && FL_TEST(klass, RICLASS_IS_ORIGIN)) {
+	klass = RBASIC_CLASS(klass);
+    }
     if (me->def->type == VM_METHOD_TYPE_CFUNC) {
 	if (st_lookup(vm_opt_method_table, (st_data_t)me, &bop)) {
 	    int flag = vm_redefinition_check_flag(klass);
```


----------------------------------------
Bug #11826: After prepending a module, rewrite Hash#[] takes no effect for calls like Hash.new[:a]
https://bugs.ruby-lang.org/issues/11826#change-55609

* Author: cichol tsai
* Status: Assigned
* Priority: Normal
* Assignee: Koichi Sasada
* ruby -v: ruby 2.2.2p95 (2015-04-13 revision 50295) [x64-mingw32]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
~~~
module M
  def self.prepended(clz)
    clz.class_eval do
      def [](k)
        p 1
      end
    end
  end
end

module N
  def self.included(clz)
    clz.class_eval do 
      def []=(k, v)
        p 2
      end
    end
  end
end

class Hash
  prepend M
  include N
end

Hash.new[1]
Hash.new[1]=1
~~~

Running this example gives no output, which is expected to be '1 2'.
I am using 'ruby 2.2.2p95 (2015-04-13 revision 50295) [x64-mingw32]'.
Thanks in advanced.



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

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [ruby-core:72225] [Ruby trunk - Bug #11826] After prepending a module, rewrite Hash#[] takes no effect for calls like Hash.new[:a]
       [not found] <redmine.issue-11826.20151216141830@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2015-12-17  1:23 ` [ruby-core:72197] [Ruby trunk - Bug #11826] [Assigned] " shugo
@ 2015-12-17 22:47 ` shugo
  3 siblings, 0 replies; 4+ messages in thread
From: shugo @ 2015-12-17 22:47 UTC (permalink / raw
  To: ruby-core

Issue #11826 has been updated by Shugo Maeda.

Backport changed from 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN to 2.0.0: REQUIRED, 2.1: REQUIRED, 2.2: REQUIRED

Shugo Maeda wrote:
> It seems that redefinition check of optimized methods doesn't work properly for prepended classes.
> Is the following patch correct, ko1?

Committed it with a test in r53173 after checking that `make exam` succeeded.
Revert it if it's wrong.


----------------------------------------
Bug #11826: After prepending a module, rewrite Hash#[] takes no effect for calls like Hash.new[:a]
https://bugs.ruby-lang.org/issues/11826#change-55635

* Author: cichol tsai
* Status: Closed
* Priority: Normal
* Assignee: Koichi Sasada
* ruby -v: ruby 2.2.2p95 (2015-04-13 revision 50295) [x64-mingw32]
* Backport: 2.0.0: REQUIRED, 2.1: REQUIRED, 2.2: REQUIRED
----------------------------------------
~~~
module M
  def self.prepended(clz)
    clz.class_eval do
      def [](k)
        p 1
      end
    end
  end
end

module N
  def self.included(clz)
    clz.class_eval do 
      def []=(k, v)
        p 2
      end
    end
  end
end

class Hash
  prepend M
  include N
end

Hash.new[1]
Hash.new[1]=1
~~~

Running this example gives no output, which is expected to be '1 2'.
I am using 'ruby 2.2.2p95 (2015-04-13 revision 50295) [x64-mingw32]'.
Thanks in advanced.



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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-12-17 22:15 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <redmine.issue-11826.20151216141830@ruby-lang.org>
2015-12-16 14:18 ` [ruby-core:72186] [Ruby trunk - Bug #11826] [Open] In 'prepended', 'class_eval' rewriting a method take no effect cichol
2015-12-16 15:29 ` [ruby-core:72188] [Ruby trunk - Bug #11826] After prepending a module, rewrite Hash#[] takes no effect for calls like Hash.new[:a] cichol
2015-12-17  1:23 ` [ruby-core:72197] [Ruby trunk - Bug #11826] [Assigned] " shugo
2015-12-17 22:47 ` [ruby-core:72225] [Ruby trunk - Bug #11826] " shugo

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