Repository navigation
Don't report an included module's instance variable as duplicated - #3273
Open
hasannadeem wants to merge 1 commit into
Open
hasannadeem wants to merge 1 commit into
hasannadeem wants to merge 1 commit into
Conversation
When a class gets the same module more than once, DefinitionBuilder inserts that module's instance variable declarations once per path, and validate_variable then sees the same declaration twice from the same module and raises InstanceVariableDuplicationError. That happens for valid Ruby such as a subclass including a module its superclass includes, or a class including both M and a module that includes M, not only for a repeated `include M`. Skip a declaration already seen on the variable's chain, so a declaration reached twice is not compared with itself. Two different declarations of the same variable in one module or class still raise. Fixes ruby#2386
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #2386.
When a class gets the same module more than once,
DefinitionBuilderinserts that module's instance variables once per path.validate_variablethen sees the same declaration twice, both declared in the module, and raisesInstanceVariableDuplicationError. This isn't limited to the repeatedinclude Min the issue. It also happens with valid Ruby where nothing is redundant:Each of these reports the error for
@m, even thoughMdeclares it once.This changes
validate_variableto skip a declaration it has already seen on the variable's chain, compared by identity. The same declaration reached twice is then no longer compared with itself. Two different declarations of the same variable in one module or class still raise, including when that module is included twice. The validation itself stays, so this is narrower than disabling it, which was mentioned in the issue.test_variable_of_module_included_more_than_oncecovers the direct, superclass and nested-module cases, plus both cases that must still raise. It fails without the change indefinition_builder.rb.rake test,rake validate, rubocop andbin/steep checkpass locally on Ruby 3.4.4.Not changed here: RBS's ancestor list also contains the module more than once in these cases (
rbs ancestors Childprints::Child ::M ::Parent ::M ::Object), while Ruby lists it once. Fixing that would change method resolution, so I've left it for a separate discussion.