Bug #21098
closedrb_alias: confusing error messages when the original method cannot be found and ZSUPER methods are involved
Description
Hi all,
I've noticed that the current implementation of rb_alias
can raise NameError
exceptions with confusing error messages when the original method cannot be found and ZSUPER methods are involved in the inheritance chain traversal.
That's because rb_print_undef
is invoked with the current value of klass
, which is repeatedly reassigned whenever a ZSUPER method is encountered in the search, potentially referencing an ICLASS or FALSE.
The following are 2 examples of this kind of behavior:
module M
private :class
alias_method :xyz, :class # raises 'Module#alias_method': undefined method 'class' for class 'false' (NameError)
end
module M
private :class
end
module N
prepend M
alias_method :xyz, :class # raises 'Module#alias_method': undefined method 'class' for class '#<N:0x0000000124ba7f00>' (NameError)
end
The second example also triggers an assertion failure when they are enabled with -DRUBY_DEBUG
:
../vm_insnhelper.c:2192: Assertion Failed: rb_vm_search_method_slowpath:RB_TYPE_2_P(klass, RUBY_T_CLASS, RUBY_T_ICLASS): klass: T_MODULE
In the following PR I tried to fix the issue by invoking rb_print_undef
with target_klass
as argument, which holds the initial value of klass
.
https://github.com/ruby/ruby/pull/12665
The previous examples become as follows:
module M
private :class
alias_method :xyz, :class # raises 'Module#alias_method': undefined method 'class' for module 'M' (NameError)
end
module M
private :class
end
module N
prepend M
alias_method :xyz, :class # raises 'Module#alias_method': undefined method 'class' for module 'N' (NameError)
end
This is my first contribution to Ruby, so please let me know if I can improve it further.
Cheers!
Updated by alanwu (Alan Wu) about 11 hours ago
- Status changed from Open to Closed