Skip to content

Fix superclass resolution for class Foo < Foo and add circular inheritance diagnostic#392

Merged
mame merged 1 commit intoruby:masterfrom
mame:fix-superclass-self-reference
Feb 25, 2026
Merged

Fix superclass resolution for class Foo < Foo and add circular inheritance diagnostic#392
mame merged 1 commit intoruby:masterfrom
mame:fix-superclass-self-reference

Conversation

@mame
Copy link
Member

@mame mame commented Feb 25, 2026

When a class is defined with a superclass of the same name (e.g., class Foo < Foo inside a module), the superclass constant was incorrectly resolving to the class being defined itself, because TypeProf registers all definitions simultaneously. In Ruby, the superclass expression is evaluated before the class constant is created, so it naturally finds the outer definition.

Fix this by using lenv.cref.outer for the superclass constant lookup when the superclass is a bare constant with the same name as the class being defined.

Also add a diagnostic for circular inheritance detection during install, covering both direct self-reference (class A < A) and mutual recursion (class A < B; class B < A).

Fixes #373

…ritance diagnostic

When a class is defined with a superclass of the same name (e.g.,
`class Foo < Foo` inside a module), the superclass constant was
incorrectly resolving to the class being defined itself, because
TypeProf registers all definitions simultaneously. In Ruby, the
superclass expression is evaluated before the class constant is
created, so it naturally finds the outer definition.

Fix this by using `lenv.cref.outer` for the superclass constant
lookup when the superclass is a bare constant with the same name
as the class being defined.

Also add a diagnostic for circular inheritance detection during
install, covering both direct self-reference (`class A < A`) and
mutual recursion (`class A < B; class B < A`).

Fixes ruby#373

Co-Authored-By: pvcresin <pvcresin0730@gmail.com>
@mame mame merged commit 941d090 into ruby:master Feb 25, 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