Skip to content

Commit e06c688

Browse files
tannevaledclaude
andcommitted
vm: ObjectSpace._id2ref is deprecated in Ruby 4.0
One of the ten examples the version move needs, and the one the headline figure most misrepresented. `core/objectspace/_id2ref_spec.rb` reads `-10` between 3.4.1 and 4.0.9, which looks like the largest single regression of the move. Its guards say otherwise: ```ruby ruby_version_is "4.0" do # ONE example: "is deprecated" ruby_version_is ""..."4.0" do # TEN examples asserting it works ``` So the −10 is ten examples 4.0 no longer asks for, and the work is the single 4.0 example. Measured on this branch: `pass=0 fail=1` → **`pass=1 fail=0 error=0 skip=0`**. ## Upstream gc.c at v4.0.9, `os_id2ref` (gc.c:2167) opens with ```c rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "ObjectSpace._id2ref is deprecated"); ``` Category-gated, so an ordinary run stays silent: `Warning[:deprecated]` is off by default. ruby/spec reaches it because the conformance shim turns the category on (`spec_helper.rb:60`, with a comment already explaining exactly this), which is also why the spec can assert it with a bare `complain(…)` and no `verbose: true`. ## ⛔ The warning goes AFTER the arity check, and I had it the other way first I wrote the call as the method's first statement, with a comment claiming that was upstream's order. It is not. `rb_define_module_function(rb_mObjSpace, "_id2ref", os_id2ref, 1)` (gc.c:5560) declares a **fixed arity of 1**, so `rb_error_arity` fires in the dispatcher and `os_id2ref` — warning and all — is never entered for a wrong argument count. Warning first would make `ObjectSpace._id2ref` with no argument complain and *then* raise, which MRI does not do. `objectspace_test.go` already pins that ArgumentError, so the order is covered. The declaration site being three thousand lines from the body is why this was worth reading rather than assuming: the arity is not visible where the function is written. ## warnDeprecatedGvar → warnDeprecated The helper was already `rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, …)` in full — the `Gvar` in its name was its first caller, not anything it does. Renamed rather than copied, and its three existing call sites updated, because a shadowing copy is how a method ends up with two bodies and one of them dead (the root cause of #549's coverage failure, per the notes in `scripts/coverage/`). Refs #799 Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
1 parent 1ee9fdb commit e06c688

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

‎internal/vm/globals.go‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func (vm *VM) specialGvar(name string) (object.Value, bool) {
205205
case "$-a", "$-l", "$-p":
206206
return object.Bool(false), true
207207
case "$=":
208-
vm.warnDeprecatedGvar("variable $= is no longer effective")
208+
vm.warnDeprecated("variable $= is no longer effective")
209209
return object.Bool(false), true
210210
}
211211
// $~, $&, $`, $' and $N fall through so the match-data resolver in gvar
@@ -268,11 +268,17 @@ func (vm *VM) warningEnabled() bool {
268268
return truthyValue(vm.verboseSlot("$VERBOSE"))
269269
}
270270

271-
// warnDeprecatedGvar emits a special-variable deprecation warning through
272-
// Warning.warn, but only while Warning[:deprecated] is enabled — error.c v3_4_0
273-
// deprecation_warning_enabled() requires both a non-nil $VERBOSE and the
274-
// category, and the category is off by default, so an ordinary run is silent.
275-
func (vm *VM) warnDeprecatedGvar(msg string) {
271+
// warnDeprecated is error.c's rb_category_warn(RB_WARN_CATEGORY_DEPRECATED,
272+
// …): a deprecation warning through Warning.warn, emitted only while
273+
// Warning[:deprecated] is enabled — deprecation_warning_enabled() requires both
274+
// a non-nil $VERBOSE and the category, and the category is off by default, so
275+
// an ordinary run is silent.
276+
//
277+
// It was `warnDeprecatedGvar`, which named its first caller rather than what it
278+
// does; nothing in it is specific to a global variable. Renamed when
279+
// ObjectSpace._id2ref needed the same thing, rather than copied — a shadowing
280+
// copy is how a method ends up with two bodies and one of them dead.
281+
func (vm *VM) warnDeprecated(msg string) {
276282
if !vm.warnEnabled() {
277283
return
278284
}
@@ -420,7 +426,7 @@ func (vm *VM) checkStrGvar(name string, v object.Value) object.Value {
420426
if _, ok := v.(*object.String); !ok {
421427
raise("TypeError", "value of %s must be String", name)
422428
}
423-
vm.warnDeprecatedGvar("non-nil '" + name + "' is deprecated")
429+
vm.warnDeprecated("non-nil '" + name + "' is deprecated")
424430
return v
425431
}
426432

@@ -443,7 +449,7 @@ func (vm *VM) checkFieldSepGvar(name string, v object.Value) object.Value {
443449
}
444450
raise("TypeError", "value of %s must be String or Regexp", name)
445451
}
446-
vm.warnDeprecatedGvar("non-nil '" + name + "' is deprecated")
452+
vm.warnDeprecated("non-nil '" + name + "' is deprecated")
447453
return v
448454
}
449455

‎internal/vm/objectspace.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,29 @@ func (vm *VM) registerObjectSpace() {
205205
// #object_id: immediate ids decode arithmetically (fixnum 2n+1 -> n, and the
206206
// fixed nil/true/false ids), and reference ids are resolved through the
207207
// object_id memo. An id that maps to no live object raises RangeError, as MRI.
208+
//
209+
// Ruby 4.0 deprecates it. gc.c at v4.0.9, os_id2ref (gc.c:2167), opens with
210+
//
211+
// rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "ObjectSpace._id2ref is deprecated");
212+
//
213+
// so the warning is category-gated and an ordinary run stays silent —
214+
// Warning[:deprecated] is off by default. ruby/spec reaches it because the
215+
// conformance shim turns the category on (spec_helper.rb:60), which is also
216+
// why `core/objectspace/_id2ref_spec.rb` can assert it with a bare
217+
// `complain(…)` and no `verbose: true`.
218+
//
219+
// ⛔ The warning comes AFTER the argument count is checked, and that order is
220+
// upstream's rather than a choice: `rb_define_module_function(rb_mObjSpace,
221+
// "_id2ref", os_id2ref, 1)` (gc.c:5560) declares a FIXED arity of 1, so
222+
// rb_error_arity fires in the dispatcher and os_id2ref — with the warning in
223+
// its first line — is never entered for a wrong count. Warning first would
224+
// make `ObjectSpace._id2ref` with no argument complain and then raise, which
225+
// MRI does not do.
208226
def("_id2ref", func(vm *VM, _ object.Value, args []object.Value, _ *Proc) object.Value {
209227
if len(args) == 0 {
210228
raise("ArgumentError", "wrong number of arguments (given 0, expected 1)")
211229
}
230+
vm.warnDeprecated("ObjectSpace._id2ref is deprecated")
212231
id := intArg(args[0])
213232
if id&1 == 1 { // odd => fixnum id 2n+1
214233
return object.IntValue(id >> 1)

0 commit comments

Comments
 (0)