Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a cyclic prepend not modify ancestors for the receiver #4165

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions class.c
Expand Up @@ -1024,6 +1024,18 @@ include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super)
struct rb_id_table *const klass_m_tbl = RCLASS_M_TBL(klass_origin);
VALUE original_klass = klass;

if (klass_m_tbl) {
VALUE original_module = module;

while (module) {
if (klass_m_tbl == RCLASS_M_TBL(module))
return -1;
module = RCLASS_SUPER(module);
}

module = original_module;
}

while (module) {
int c_seen = FALSE;
int superclass_seen = FALSE;
Expand All @@ -1032,8 +1044,6 @@ include_modules_at(const VALUE klass, VALUE c, VALUE module, int search_super)
if (klass == c) {
c_seen = TRUE;
}
if (klass_m_tbl && klass_m_tbl == RCLASS_M_TBL(module))
return -1;
if (klass_origin != c || search_super) {
/* ignore if the module included already in superclasses for include,
* ignore if the module included before origin class for prepend
Expand Down
7 changes: 7 additions & 0 deletions test/ruby/test_module.rb
Expand Up @@ -478,6 +478,13 @@ def test_include_with_no_args
assert_raise(ArgumentError) { Module.new { include } }
end

def test_prepend_self
m = Module.new
assert_equal([m], m.ancestors)
m.prepend(m) rescue nil
assert_equal([m], m.ancestors)
end

def test_prepend_works_with_duped_classes
m = Module.new
a = Class.new do
Expand Down