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
9 changes: 8 additions & 1 deletion internal/vm/object_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,14 @@ func (vm *VM) scopedConst(cls *RClass, name string) object.Value {
// terminates the lexParent chain) is not part of nesting.
func (vm *VM) nesting(cref *RClass) []*RClass {
var out []*RClass
for c := cref; c != nil && c != vm.cObject; c = c.lexParent {
// Stop at a repeat as well as at the top. A lexical chain is shaped by the
// program — `class << o; CONST = self; end` used to make a class its own
// lexical parent — and a walk with no end here allocated 17 GB and killed a
// CI runner. ancestors has guarded itself this way all along; this is the
// same guard for the same reason.
seen := map[*RClass]bool{}
for c := cref; c != nil && c != vm.cObject && !seen[c]; c = c.lexParent {
seen[c] = true
out = append(out, c)
}
return out
Expand Down
91 changes: 91 additions & 0 deletions internal/vm/singleton_const_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) the go-embedded-ruby/ruby authors
//
// SPDX-License-Identifier: BSD-3-Clause

package vm

import "testing"

// TestAConstantBoundToItsOwnScope covers a class assigned to a constant inside
// its own body.
//
// class << o
// CONST = self
// end
//
// The scope and the value are the same singleton class, and a singleton class
// is unnamed, so assignConstIn used to record it as its own lexical parent.
// Every later bare-constant read in that scope then walked a ring: 17 GB for
// six lines, and a CI runner killed under language/singleton_class_spec.rb.
//
// Verified against ruby 3.
func TestAConstantBoundToItsOwnScope(t *testing.T) {
cases := []struct{ src, want string }{
// The reading that used to take the machine, in the four shapes
// language/singleton_class_spec.rb reads it.
{`o = Object.new
class << o
CONST = self
end
class << o
p CONST == self
end`, `true`},
{`o = Object.new
class << o
CONST = self
p CONST.equal?(self)
end`, `true`},
{`o = Object.new
class << o
CONST = self
end
class << o
p self::CONST == self
end`, `true`},
{`o = Object.new
class << o
CONST = self
end
class << o
p const_get(:CONST) == self
end`, `true`},
// The same shape one level in, where the scope is a real class's
// singleton rather than an object's.
{`class Outer
class << self
INNER = self
end
def self.read; class << self; INNER; end; end
end
p Outer.read == Outer.singleton_class`, `true`},
// A constant that is not a class is unaffected, and so is a class bound
// somewhere that is not its own body — it still takes the lexical parent
// and the qualified name it always did.
{`o = Object.new
class << o
CONST = 42
p CONST
end`, `42`},
{`module M
Inner = Class.new
end
p M::Inner.name`, `"M::Inner"`},
{`module Outer2
module Inner2
THING = 7
end
p Inner2::THING
end`, `7`},
// A missing constant still raises rather than being swallowed by any of
// this.
{`o = Object.new
class << o
begin; NOPE; rescue NameError; p :raised; end
end`, `:raised`},
}
for _, c := range cases {
if got := eval(t, c.src); got != c.want+"\n" {
t.Errorf("src=%q got=%q want=%q", c.src, got, c.want+"\n")
}
}
}
27 changes: 26 additions & 1 deletion internal/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2108,10 +2108,35 @@ func (vm *VM) assignConstIn(scope *RClass, name string, val object.Value) {
if c, ok := val.(*RClass); ok && !c.named {
c.name = scopedNameFor(scope, name)
c.named = true
c.lexParent = lexParentFor(scope)
// Unless that would make c its own lexical ancestor. A class bound to a
// constant inside its own body does exactly that:
//
// class << o
// CONST = self # scope and value are the same singleton class
// end
//
// Nothing is nested in itself, and recording it that way made every
// later bare-constant read in that scope walk a ring.
if p := lexParentFor(scope); !lexicallyReaches(p, c) {
c.lexParent = p
}
}
}

// lexicallyReaches reports whether c is start or one of its lexical parents —
// i.e. whether recording start as c's lexParent would close a ring. It stops at
// a repeat, because it may be asked about a chain that already has one.
func lexicallyReaches(start, c *RClass) bool {
seen := map[*RClass]bool{}
for s := start; s != nil && !seen[s]; s = s.lexParent {
if s == c {
return true
}
seen[s] = true
}
return false
}

// lexParentFor returns the lexParent to record for a class/module nested in
// scope: scope itself, except Object (the top level), which terminates the
// chain as nil.
Expand Down