Skip to content

Commit c5c0bb5

Browse files
fxnbyroot
authored andcommitted
Restore the original order of const_added and inherited callbacks
Originally, if a class was defined with the class keyword, the cref had a const_added callback, and the superclass an inherited callback, const_added was called first, and inherited second. This was discussed in https://bugs.ruby-lang.org/issues/21143 and an attempt at changing this order was made. While both constant assignment and inheritance have happened before these callbacks are invoked, it was deemed nice to have the same order as in C = Class.new This was mostly for alignment: In that last use case things happen at different times and therefore the order of execution is kind of obvious, whereas when the class keyword is involved, the order is opaque to the user and it is up to the interpreter. However, soon in https://bugs.ruby-lang.org/issues/21193 Matz decided to play safe and keep the existing order. This reverts commits: de097fb de48e47
1 parent 0d6263b commit c5c0bb5

File tree

9 files changed

+26
-98
lines changed

9 files changed

+26
-98
lines changed

class.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,9 +1004,8 @@ rb_define_class(const char *name, VALUE super)
10041004
}
10051005
klass = rb_define_class_id(id, super);
10061006
rb_vm_register_global_object(klass);
1007-
rb_const_set_raw(rb_cObject, id, klass);
1007+
rb_const_set(rb_cObject, id, klass);
10081008
rb_class_inherited(super, klass);
1009-
rb_const_added(klass, id);
10101009

10111010
return klass;
10121011
}
@@ -1044,10 +1043,8 @@ rb_define_class_id_under_no_pin(VALUE outer, ID id, VALUE super)
10441043
}
10451044
klass = rb_define_class_id(id, super);
10461045
rb_set_class_path_string(klass, outer, rb_id2str(id));
1047-
1048-
rb_const_set_raw(outer, id, klass);
1046+
rb_const_set(outer, id, klass);
10491047
rb_class_inherited(super, klass);
1050-
rb_const_added(outer, id);
10511048

10521049
return klass;
10531050
}

internal/variable.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
#include "shape.h" /* for rb_shape_t */
1717

1818
/* variable.c */
19-
void rb_const_added(VALUE klass, ID const_name);
20-
void rb_const_set_raw(VALUE klass, ID id, VALUE val);
2119
void rb_gc_mark_global_tbl(void);
2220
void rb_gc_update_global_tbl(void);
2321
size_t rb_generic_ivar_memsize(VALUE);

spec/ruby/core/class/fixtures/callback_order.rb

Lines changed: 0 additions & 31 deletions
This file was deleted.

spec/ruby/core/class/inherited_spec.rb

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require_relative '../../spec_helper'
22
require_relative 'fixtures/classes'
3-
require_relative 'fixtures/callback_order'
43

54
describe "Class.inherited" do
65

@@ -99,16 +98,4 @@ class << top; protected :inherited; end
9998
-> { Class.new(top) }.should_not raise_error
10099
end
101100

102-
it "is invoked after the class is named when using class definition syntax" do
103-
CoreClassSpecs::Callbacks::Child::INHERITED_NAME.should == "CoreClassSpecs::Callbacks::Child"
104-
end
105-
106-
ruby_version_is "3.5" do # https://bugs.ruby-lang.org/issues/21143
107-
it "is invoked before `const_added`" do
108-
CoreClassSpecs::Callbacks::ORDER.should == [
109-
[:inherited, CoreClassSpecs::Callbacks::Child, "constant", :location],
110-
[:const_added, :Child, "constant", :location],
111-
]
112-
end
113-
end
114101
end

spec/ruby/optional/capi/class_spec.rb

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -385,17 +385,6 @@
385385
CApiClassSpecs.const_get(cls.name)
386386
}.should raise_error(NameError, /wrong constant name/)
387387
end
388-
389-
ruby_version_is "3.5" do
390-
it "calls .inherited before .const_added" do
391-
ScratchPad.record([])
392-
@s.rb_define_class_id_under(CApiClassSpecs::Callbacks, :Subclass, CApiClassSpecs::Callbacks)
393-
ScratchPad.recorded.should == [
394-
[:inherited, "CApiClassSpecs::Callbacks::Subclass", :location],
395-
[:const_added, :Subclass, :location],
396-
]
397-
end
398-
end
399388
end
400389

401390
describe "rb_define_class_id_under" do

spec/ruby/optional/capi/fixtures/class.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,4 @@ class B
101101
module M
102102
end
103103
end
104-
105-
class Callbacks
106-
def self.inherited(child)
107-
ScratchPad << [:inherited, child.name, Object.const_source_location(child.name) ? :location : :unknown_location]
108-
end
109-
110-
def self.const_added(const_name)
111-
ScratchPad << [:const_added, const_name, const_source_location(const_name) ? :location : :unknown_location]
112-
end
113-
end
114104
end

test/ruby/test_settracefunc.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,14 @@ def test_class
168168
events.shift)
169169
assert_equal(["line", 4, __method__, self.class],
170170
events.shift)
171-
assert_equal(["c-call", 4, :inherited, Class],
172-
events.shift)
173-
assert_equal(["c-return", 4, :inherited, Class],
174-
events.shift)
175171
assert_equal(["c-call", 4, :const_added, Module],
176172
events.shift)
177173
assert_equal(["c-return", 4, :const_added, Module],
178174
events.shift)
175+
assert_equal(["c-call", 4, :inherited, Class],
176+
events.shift)
177+
assert_equal(["c-return", 4, :inherited, Class],
178+
events.shift)
179179
assert_equal(["class", 4, nil, nil],
180180
events.shift)
181181
assert_equal(["line", 5, nil, nil],
@@ -411,10 +411,10 @@ def test_thread_trace
411411

412412
[["c-return", 2, :add_trace_func, Thread],
413413
["line", 3, __method__, self.class],
414-
["c-call", 3, :inherited, Class],
415-
["c-return", 3, :inherited, Class],
416414
["c-call", 3, :const_added, Module],
417415
["c-return", 3, :const_added, Module],
416+
["c-call", 3, :inherited, Class],
417+
["c-return", 3, :inherited, Class],
418418
["class", 3, nil, nil],
419419
["line", 4, nil, nil],
420420
["c-call", 4, :method_added, Module],
@@ -558,10 +558,10 @@ def trace_by_tracepoint *trace_events
558558
[:line, 5, 'xyzzy', self.class, method, self, :inner, :nothing],
559559
[:c_return, 4, "xyzzy", Array, :reverse_each, [1], nil, [1]],
560560
[:line, 7, 'xyzzy', self.class, method, self, :outer, :nothing],
561-
[:c_call, 7, "xyzzy", Class, :inherited, Object, nil, :nothing],
562-
[:c_return, 7, "xyzzy", Class, :inherited, Object, nil, nil],
563561
[:c_call, 7, "xyzzy", Module, :const_added, TestSetTraceFunc, nil, :nothing],
564562
[:c_return, 7, "xyzzy", Module, :const_added, TestSetTraceFunc, nil, nil],
563+
[:c_call, 7, "xyzzy", Class, :inherited, Object, nil, :nothing],
564+
[:c_return, 7, "xyzzy", Class, :inherited, Object, nil, nil],
565565
[:class, 7, "xyzzy", nil, nil, xyzzy.class, nil, :nothing],
566566
[:line, 8, "xyzzy", nil, nil, xyzzy.class, nil, :nothing],
567567
[:line, 9, "xyzzy", nil, nil, xyzzy.class, :XYZZY_outer, :nothing],

variable.c

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,6 +2624,7 @@ rb_autoload(VALUE module, ID name, const char *feature)
26242624
}
26252625

26262626
static void const_set(VALUE klass, ID id, VALUE val);
2627+
static void const_added(VALUE klass, ID const_name);
26272628

26282629
struct autoload_arguments {
26292630
VALUE module;
@@ -2732,7 +2733,7 @@ rb_autoload_str(VALUE module, ID name, VALUE feature)
27322733
VALUE result = rb_mutex_synchronize(autoload_mutex, autoload_synchronized, (VALUE)&arguments);
27332734

27342735
if (result == Qtrue) {
2735-
rb_const_added(module, name);
2736+
const_added(module, name);
27362737
}
27372738
}
27382739

@@ -3603,8 +3604,8 @@ set_namespace_path(VALUE named_namespace, VALUE namespace_path)
36033604
RB_VM_LOCK_LEAVE();
36043605
}
36053606

3606-
void
3607-
rb_const_added(VALUE klass, ID const_name)
3607+
static void
3608+
const_added(VALUE klass, ID const_name)
36083609
{
36093610
if (GET_VM()->running) {
36103611
VALUE name = ID2SYM(const_name);
@@ -3679,17 +3680,11 @@ const_set(VALUE klass, ID id, VALUE val)
36793680
}
36803681
}
36813682

3682-
void
3683-
rb_const_set_raw(VALUE klass, ID id, VALUE val)
3684-
{
3685-
const_set(klass, id, val);
3686-
}
3687-
36883683
void
36893684
rb_const_set(VALUE klass, ID id, VALUE val)
36903685
{
36913686
const_set(klass, id, val);
3692-
rb_const_added(klass, id);
3687+
const_added(klass, id);
36933688
}
36943689

36953690
static struct autoload_data *

vm_insnhelper.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5750,27 +5750,30 @@ vm_check_if_module(ID id, VALUE mod)
57505750
}
57515751
}
57525752

5753+
static VALUE
5754+
declare_under(ID id, VALUE cbase, VALUE c)
5755+
{
5756+
rb_set_class_path_string(c, cbase, rb_id2str(id));
5757+
rb_const_set(cbase, id, c);
5758+
return c;
5759+
}
5760+
57535761
static VALUE
57545762
vm_declare_class(ID id, rb_num_t flags, VALUE cbase, VALUE super)
57555763
{
57565764
/* new class declaration */
57575765
VALUE s = VM_DEFINECLASS_HAS_SUPERCLASS_P(flags) ? super : rb_cObject;
5758-
VALUE c = rb_define_class_id(id, s);
5766+
VALUE c = declare_under(id, cbase, rb_define_class_id(id, s));
57595767
rb_define_alloc_func(c, rb_get_alloc_func(c));
5760-
rb_set_class_path_string(c, cbase, rb_id2str(id));
5761-
rb_const_set_raw(cbase, id, c);
57625768
rb_class_inherited(s, c);
5763-
rb_const_added(cbase, id);
57645769
return c;
57655770
}
57665771

57675772
static VALUE
57685773
vm_declare_module(ID id, VALUE cbase)
57695774
{
5770-
VALUE m = rb_module_new();
5771-
rb_set_class_path_string(m, cbase, rb_id2str(id));
5772-
rb_const_set(cbase, id, m);
5773-
return m;
5775+
/* new module declaration */
5776+
return declare_under(id, cbase, rb_module_new());
57745777
}
57755778

57765779
NORETURN(static void unmatched_redefinition(const char *type, VALUE cbase, ID id, VALUE old));

0 commit comments

Comments
 (0)