Skip to content

A class bound to a constant in its own body is not its own lexical parent - #421

Merged
tannevaled merged 1 commit into
mainfrom
fix/constant-bound-to-its-own-scope
Aug 21, 2026
Merged

tannevaled merged 1 commit into
mainfrom
fix/constant-bound-to-its-own-scope

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

assignConstIn gives an unnamed class the qualified name of the constant it is bound to, and the lexical parent of the scope it is bound in. In

class << o
  CONST = self
end

the scope and the value are the same singleton class, and a singleton class is unnamed — so it was recorded as its own lexical parent. Nothing is nested in itself, and every later bare-constant read in that scope then walked a ring:

class << o
  x = CONST     # 17 GB. ruby: instant
end

language/singleton_class_spec.rb allocated 71.7 GB. It is the largest of the five reasons rubyspec-ratchet kept dying, and the one still killing it after the other four were fixed (#415, #416, #417, #418; #412 is the watcher that found them).

before 71.7 GB
after 78 MB, and the spec reports pass=40 fail=9 error=4

Two changes, because two things were wrong

  • assignConstIn does not record a lexical parent that would make the class its own ancestor.
  • nesting stops at a repeat. The lexical chain is shaped by the program and cannot be assumed acyclic — ancestors has guarded itself this way all along, and this is the same guard for the same reason. A ring arriving by any other route now costs a short walk rather than the machine.

Fixing only the cause would have left the walk defenceless; fixing only the walk would have left the ring in the object graph.

Checked against ruby 3

The four shapes the spec reads the constant in; the same shape one level in, on a real class's singleton; a non-class constant; a class bound outside its own body, which still takes its lexical parent and qualified name (M::Inner); and a missing constant still raising NameError.

Full internal/vm suite green.

🤖 Generated with Claude Code

…rent

assignConstIn gives an unnamed class the qualified name of the constant
it is bound to, and the lexical parent of the scope it is bound in. In

	class << o
	  CONST = self
	end

the scope and the value are the same singleton class, and a singleton
class is unnamed — so it was recorded as its own lexical parent. Nothing
is nested in itself, and every later bare-constant read in that scope
then walked a ring:

	class << o
	  x = CONST     # 17 GB. ruby: instant
	end

language/singleton_class_spec.rb allocated 71.7 GB, which is the largest
of the five reasons the rubyspec-ratchet lane kept dying, and the one
still killing it after the other four were fixed.

Two changes, because two things were wrong:

  - assignConstIn does not record a lexical parent that would make the
    class its own ancestor.
  - nesting stops at a repeat. The lexical chain is shaped by the program
    and cannot be assumed acyclic; ancestors has guarded itself this way
    all along, and this is the same guard for the same reason. A ring
    from any other route now costs a short walk rather than the machine.

  before  71.7 GB
  after   78 MB, and the spec reports pass=40 fail=9 error=4

Checked against ruby 3: the four shapes the spec reads the constant in,
the same shape one level in on a real class's singleton, a non-class
constant, a class bound outside its own body (which still takes its
lexical parent and qualified name), and a missing constant still raising.
@tannevaled
tannevaled merged commit 1c8aff2 into main Aug 21, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant