Bug #22022
closedRefinement zsuper problems when referenced method changes
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