From 906bbf7a1f74e6d87a9abd79f3a2e7ca4049da8e Mon Sep 17 00:00:00 2001 From: tannevaled Date: Sun, 30 Aug 2026 10:20:30 +0200 Subject: [PATCH] Add Module#deprecate_constant MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Module#deprecate_constant(*names) marks existing constants deprecated (a new per-module set); reading one — through Foo::CONST or Module#const_get — warns "constant Foo::CONST is deprecated" when Warning[:deprecated] is enabled, and is silent otherwise. An undefined name raises NameError. Verified against ruby 4.0.6. Whole-package coverage stays at 100%; the ruby/spec floor rises 16280 -> 16282 (core/module/deprecate_constant 0 -> 2, the rest of the spec exercising the warning via the unsupported complain matcher). Co-Authored-By: Claude Opus 4.8 --- internal/vm/module_residuals.go | 11 ++++++++- internal/vm/modulefunc.go | 30 ++++++++++++++++++++++++ internal/vm/object_model.go | 16 ++++++++----- internal/vm/reflection_methods_test.go | 32 ++++++++++++++++++++++++++ scripts/conformance/rubyspec/FLOOR | 2 +- 5 files changed, 83 insertions(+), 8 deletions(-) diff --git a/internal/vm/module_residuals.go b/internal/vm/module_residuals.go index bdd13f7b..6913cc32 100644 --- a/internal/vm/module_residuals.go +++ b/internal/vm/module_residuals.go @@ -25,7 +25,16 @@ func (vm *VM) registerModuleResiduals() { // unresolved name through #const_missing on the module where it was sought. vm.cModule.define("const_get", func(vm *VM, self object.Value, args []object.Value, _ *Proc) object.Value { inherit := len(args) < 2 || args[1].Truthy() - return vm.moduleConstGet(self.(*RClass), args[0], inherit) + mod := self.(*RClass) + v := vm.moduleConstGet(mod, args[0], inherit) + if mod.deprecatedConsts != nil { + if s, ok := args[0].(*object.String); ok && mod.deprecatedConsts[s.Str()] { + vm.warnDeprecatedConst(mod, s.Str()) + } else if sym, ok := args[0].(object.Symbol); ok && mod.deprecatedConsts[string(sym)] { + vm.warnDeprecatedConst(mod, string(sym)) + } + } + return v }) // Module#const_defined?(name, inherit=true): report whether name resolves, diff --git a/internal/vm/modulefunc.go b/internal/vm/modulefunc.go index f067f6a7..bc61223e 100644 --- a/internal/vm/modulefunc.go +++ b/internal/vm/modulefunc.go @@ -170,6 +170,36 @@ func (vm *VM) registerModuleExtras() { } vm.cModule.define("private_constant", constVisibility) vm.cModule.define("public_constant", constVisibility) + + // Module#deprecate_constant(*names): mark existing constants so that reading + // them warns (when Warning[:deprecated] is on). An undefined name is a + // NameError. + vm.cModule.define("deprecate_constant", func(_ *VM, self object.Value, args []object.Value, _ *Proc) object.Value { + mod := self.(*RClass) + for _, a := range args { + name := nameArg(a) + if _, ok := mod.consts[name]; !ok { + raise("NameError", "constant %s not defined", scopedNameFor(mod, name)) + } + if mod.deprecatedConsts == nil { + mod.deprecatedConsts = map[string]bool{} + } + mod.deprecatedConsts[name] = true + } + return mod + }) +} + +// warnDeprecatedConst emits MRI's "constant X::Y is deprecated" warning when a +// deprecated constant is read, but only while Warning[:deprecated] is enabled. +func (vm *VM) warnDeprecatedConst(scope *RClass, name string) { + w := vm.consts["Warning"] + if !vm.send(w, "[]", []object.Value{object.Symbol("deprecated")}, nil).Truthy() { + return + } + vm.send(w, "warn", []object.Value{ + object.NewString("warning: constant " + scopedNameFor(scope, name) + " is deprecated\n"), + }, nil) } // nameArg coerces a method-name argument (a String or Symbol) to its string, diff --git a/internal/vm/object_model.go b/internal/vm/object_model.go index e457d592..455577cc 100644 --- a/internal/vm/object_model.go +++ b/internal/vm/object_model.go @@ -209,12 +209,13 @@ type RClass struct { // named reports whether this class/module has acquired a permanent name yet. // An anonymous class (Class.new / Module.new) starts unnamed; the first time // it is assigned to a constant it takes that constant's qualified name. - named bool - isModule bool - meta *RClass // lazily-created metaclass for `class << SomeClass`; its methods map IS this class's smethods - metaOf *RClass // when this RClass is a metaclass, the class it is the metaclass of (else nil); routes `class << c` visibility directives to c's class methods - isSingleton bool // true for a singleton class — a class metaclass or a per-object singleton (reported by Module#singleton_class?) - funcMode bool // module_function (no-arg) mode: subsequent instance defs are also copied as module/singleton methods + named bool + isModule bool + meta *RClass // lazily-created metaclass for `class << SomeClass`; its methods map IS this class's smethods + metaOf *RClass // when this RClass is a metaclass, the class it is the metaclass of (else nil); routes `class << c` visibility directives to c's class methods + isSingleton bool // true for a singleton class — a class metaclass or a per-object singleton (reported by Module#singleton_class?) + deprecatedConsts map[string]bool // constants marked by Module#deprecate_constant; access warns + funcMode bool // module_function (no-arg) mode: subsequent instance defs are also copied as module/singleton methods // defaultVis is the access level applied to subsequent `def`s in this body, // set by a bare `private` / `protected` / `public` with no args. It resets to // visPublic at the start of each class/module body (a fresh RClass starts at @@ -526,6 +527,9 @@ func (vm *VM) constInAncestors(cls *RClass, name string) (object.Value, bool) { // the flat top-level table, so M::File is distinct from the top-level File. func (vm *VM) scopedConst(cls *RClass, name string) object.Value { if v, ok := vm.constInAncestors(cls, name); ok { + if cls.deprecatedConsts != nil && cls.deprecatedConsts[name] { + vm.warnDeprecatedConst(cls, name) + } return v } // A pending autoload on the receiver or an ancestor: require it and retry. diff --git a/internal/vm/reflection_methods_test.go b/internal/vm/reflection_methods_test.go index a01ff9d9..9c9a96fe 100644 --- a/internal/vm/reflection_methods_test.go +++ b/internal/vm/reflection_methods_test.go @@ -89,3 +89,35 @@ func TestClassSubclasses(t *testing.T) { } } } + +// TestModuleDeprecateConstant covers Module#deprecate_constant: marking existing +// constants (returning self), the NameError for an undefined name, and the +// deprecation warning on read via constant scope and const_get — emitted only +// while Warning[:deprecated] is enabled. Asserted against MRI Ruby 4.0.6. +func TestModuleDeprecateConstant(t *testing.T) { + cases := []struct{ src, want string }{ + {`module DcA; X = 1; deprecate_constant :X; end; p DcA::X`, "1\n"}, + {`module DcB; Y = 2; end; p DcB.deprecate_constant(:Y).equal?(DcB)`, "true\n"}, + // The warning fires on scoped access and const_get when enabled... + {`require "stringio" +module DcC; Z = 3; deprecate_constant :Z; end +Warning[:deprecated] = true; $stderr = StringIO.new +DcC::Z; DcC.const_get(:Z); DcC.const_get("Z") +p $stderr.string.scan("is deprecated").length`, "3\n"}, + // ...and stays silent when disabled. + {`require "stringio" +module DcD; W = 4; deprecate_constant :W; end +Warning[:deprecated] = false; $stderr = StringIO.new +DcD::W +p $stderr.string`, "\"\"\n"}, + } + for _, c := range cases { + if got := eval(t, c.src); got != c.want { + t.Errorf("src=%q got=%q want=%q", c.src, got, c.want) + } + } + if err := runErr(t, `module DcE; deprecate_constant :Nope; end`); err == nil || + !strings.Contains(err.Error(), "constant DcE::Nope not defined") { + t.Errorf("deprecate_constant(undefined): err=%v", err) + } +} diff --git a/scripts/conformance/rubyspec/FLOOR b/scripts/conformance/rubyspec/FLOOR index 8ec23a6b..509c88ee 100644 --- a/scripts/conformance/rubyspec/FLOOR +++ b/scripts/conformance/rubyspec/FLOOR @@ -1 +1 @@ -16280 +16282