Project

General

Profile

Bug #12367

[PATCH] Declaring an already defined class with Object as the new superclass does not raise an error

Added by Eregon (Benoit Daloze) about 4 years ago. Updated about 4 years ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:75446]

Description

For example:

class SuperclassIgnoresObject < A
end
SuperclassIgnoresObject.superclass # A

class SuperclassIgnoresObject < Object # Should raise a superclass mismatch but it doesn't.
end
SuperclassIgnoresObject.superclass # A

This seems an unintended side-effect of using Object as the default superclass in the code and not differentiating given/non-given superclass.
In insns.def defineclass:

if (super == Qnil) {
    super = rb_cObject;
}
...
if (super != rb_cObject) {
    // check is superclass mismatch
}

Proposed patch:

diff --git a/insns.def b/insns.def
index d34a663..ae7f98f 100644
--- a/insns.def
+++ b/insns.def
@@ -865,10 +865,6 @@ defineclass
             rb_obj_class(super));
    }

-   if (super == Qnil) {
-       super = rb_cObject;
-   }
-
    vm_check_if_namespace(cbase);

    /* find klass */
@@ -881,7 +877,7 @@ defineclass
        rb_raise(rb_eTypeError, "%"PRIsVALUE" is not a class", rb_id2str(id));
        }

-       if (super != rb_cObject) {
+       if (VM_DEFINECLASS_HAS_SUPERCLASS_P(flags)) {
        VALUE tmp;
        tmp = rb_class_real(RCLASS_SUPER(klass));

@@ -892,6 +888,9 @@ defineclass
        }
    }
    else {
+       if (!VM_DEFINECLASS_HAS_SUPERCLASS_P(flags)) {
+       super = rb_cObject;
+       }
        /* new class declaration */
        klass = rb_define_class_id(id, super);
        rb_set_class_path_string(klass, cbase, rb_id2str(id));

Can I commit?

Updated by nobu (Nobuyoshi Nakada) about 4 years ago

  • Status changed from Open to Assigned
  • Assignee set to Eregon (Benoit Daloze)

Updated by nobu (Nobuyoshi Nakada) about 4 years ago

  • Backport changed from 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN to 2.1: REQUIRED, 2.2: REQUIRED, 2.3: REQUIRED
#3

Updated by Eregon (Benoit Daloze) about 4 years ago

  • Status changed from Assigned to Closed

Applied in changeset r54970.


  • insns.def (defineclass): Also raise an error when redeclaring the superclass of a class as Object and it has another superclass. [Bug #12367] [ruby-core:75446]
  • test/ruby/test_class.rb: test for above.

Updated by usa (Usaku NAKAMURA) about 4 years ago

  • Backport changed from 2.1: REQUIRED, 2.2: REQUIRED, 2.3: REQUIRED to 2.1: REQUIRED, 2.2: DONE, 2.3: REQUIRED

ruby_2_2 r55351 merged revision(s) 54970.

Updated by nagachika (Tomoyuki Chikanaga) about 4 years ago

  • Backport changed from 2.1: REQUIRED, 2.2: DONE, 2.3: REQUIRED to 2.1: REQUIRED, 2.2: DONE, 2.3: DONE

ruby_2_3 r55402 merged revision(s) 54970.

Also available in: Atom PDF