Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion internal/vm/module_residuals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 30 additions & 0 deletions internal/vm/modulefunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 10 additions & 6 deletions internal/vm/object_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
32 changes: 32 additions & 0 deletions internal/vm/reflection_methods_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion scripts/conformance/rubyspec/FLOOR
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16280
16282