[RFC] Compiler: error on method redefinition in same scope #10071
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.
I wanted to do this for a long time, and this blog post finally encouraged me to do it.
With this PR the compiler will now error with code like this:
Or code like this:
The idea is to detect cases where you redefine a method very close to where it was defined. This means you redefined it without first closing the type and reopening, or going to a different file.
I believe whenever you do that it's most surely a mistake: you didn't realize you already had a method definition and now you are redefining it.
In fact, this spotted a bug in the Log library. The second method redefines the first one:
The reason is that no keyword arguments is also caught by the second overload. The special rule in the language to prevent this is to put an
: Object
type restriction which means "at least one keyword argument must be passed":Methods defined through macros, or macro hooks, don't count in this check because it's often the case that such redefinitions are valid and out of the user's control.
I didn't add tests for this but I wanted to see first if you think this is something useful before spending more time on it.