Project

General

Profile

Actions

Bug #22022

closed

Refinement zsuper problems when referenced method changes

Bug #22022: Refinement zsuper problems when referenced method changes
1

Added by jeremyevans0 (Jeremy Evans) 22 days ago. Updated 20 days ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 4.1.0dev (2026-05-03T23:57:21Z master d8d2ed5dc9) +PRISM [x86_64-openbsd7.8]
[ruby-core:125398]

Description

I've found many problems with refinement zsuper methods. Here's one example:

module M
  private def a = :a
  alias a a
end

class A
  include M
end

class B < A
end

module R
  refine B do
    public :a
  end
end
using R
B.new.a
# false() function is unimplemented on this machine (NotImplementedError)

Here's another:

class A
  private def a = :a
end

module R
  refine A do
    public :a
  end
end
using R
A.new.a
# => :a

module M
  def a = :b
end
A.prepend M
A.new.a
# SystemStackError: stack level too deep

The problematic cases are in general can be broken down into:

  • Method being redefined (in same or closer ancestor)
  • Method being removed
  • Method being undef-ed (in same or closer ancestor)
  • Method being overridden by method in included module
  • Method being overridden by method in prepended module
  • Method being defined in an included/prepended module at time of refinement

In addition to the NotImplementedError and SystemStackError, there are other cases where incorrect results are given.

One possible approach for fixing this is to turn the zsuper methods into regular methods that call super . This fixes all cases that I found. This is not a perfect solution, due to mildly worse performance (I'm guessing) as well as arity/parameters for the method not as helpful.

It may possible to avoid these issues by clearing method caches in more cases. However, I think that would require a lot of extra work, since you cannot just clear the method cache for the current class. You would need to clear it for all subclasses, if this is a module, do the same for all classes that include/prepend the module, as well as any subclasses of those classes.

Considering the need for refinement zsuper methods is very rare, the performance and arity/parameters issues seem acceptable (to me).

Pull request: https://github.com/ruby/ruby/pull/16844

Actions

Also available in: PDF Atom