Project

General

Profile

Actions

Bug #17806

closed

Bad interaction between method cache, prepend, and refinements

Added by alanwu (Alan Wu) over 3 years ago. Updated over 3 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [x86_64-darwin19]
[ruby-core:103469]

Description

I'm running into a couple of issues with Ruby 3's new method cache and
refinements.

The first script raises SystemStackError unexpectedly:

module R1
  refine Hash do
    def foo; :r1; end
  end
end
class Hash
  prepend(Module.new)
end
class Hash
  def foo; end
end
{}.method(:foo) # put it on pCMC
module R2
  refine Hash do
    def foo; :r2; end
  end
end
{}.foo # SystemStackError

The second script calls the wrong method:

klass = Class.new { def foo; end }
_refinement = Module.new do
  refine(klass) { def foo; :refined; end }
end
klass.prepend(Module.new)
klass.new.foo # cache foo
klass.define_method(:foo) { :second }
p klass.new.foo # prints nil. False caching.

I submitted a GitHub PR to fix the issue: https://github.com/ruby/ruby/pull/4386

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0