Pick most specific parent class during linearization #3572
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
Closes #3513
Since the order of file discovery during indexing does not necessarily match the require order, we may end up discovering a re-opening of an existing class before its initial definition.
In re-openings, it's quite common to omit the parent class. Currently, we're incorrectly just finding the parent class based on the first one we can find, but that means that if the re-opening was indexed first we ignore the real parent class.
Implementation
I changed the loop that we use to find parent classes to prefer specific parent classes that aren't the implicit default (
::Object
). That way, if there's a more specific definition of the class, we find that one - which is the one that's actually setting the parent.Note: if the developer makes a mistake and defines the same class with two different parents, we will linearize based on the first one found. I believe this is acceptable since Ruby will raise when you try to do this.
Automated Tests
Added a test that reproduces the behaviour.