-
Notifications
You must be signed in to change notification settings - Fork 17.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
x/tools/cmd/gorename: Renaming Interface Functions Does Not Affect Implementations #54217
Comments
Is this about cmd/gorename specifically, or more broadly one of the packages that gopls uses? CC @golang/tools-team. |
More discussions here. |
I'm happy to build an implentation for this, however I think I have the shared worry with others that there could be uninteded renames in the unlikely event that a type implements the interface unintentionally. I thing the solution of an |
This is the biggest blocker in this issue. I think any refactoring should be smart enough to only affect structs that are currently being used for the interface. The refactoring tool should only affect structs that are currently being used for the interface. Currently, Go's compiler is smart enough to flag compiler errors when structs no longer implement an interface as a result of a rename, which gives a list of structs that should be affected by said refactoring. |
That is the intended current behavior of gorename: it uses the satisfy package to discover assignments such as And that's exactly the behavior I observe using your test case and the gorename at tip:
|
@adonovan By your post, it seems that the Not sure who to tag from the |
Yes, it could be. Could you try the same renaming from the command line and see if it works? That would tell us for sure. Thanks. |
@adonovan Anyone who can get a proper |
I just tested this renaming in gopls (not the obsolete cmd/gorename) from Emacs and VS Code and it works correctly in both. Closing as not reproducible. |
What version of Go are you using (
go version
)?go version go1.19 windows/amd64
Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I have the below sample code:
I then renamed
animal.makeSound()
toanimal.makeNoise()
.What did you expect to see?
I expected that all users of the interface as well as its implementations would be affected by the rename.
What did you see instead?
The users of the interface were affected by the rename, which is correct:
However, its implementations remained untouched which resulted in a compiler error:
Error:
And because of the above compiler error, I can't perform any further refactorings (e.g. renaming
dog.makeSound()
todog.makeNoise
) to fix the error.Currently, the only workaround is to manually fix all broken function calls across all interface implementations, which is very tedious.
More Details:
GoLand supports renaming interface functions and affecting their implementations as documented here.
If the expected behavior above is not currently supported, I think it would make sense for
gorename
to rename both users and implementations of a renamed interface function because the current behavior breaks existing code.It would make more sense for the rename to affect everything it needs to so no code gets broken.
This way, from the programmer's perspective, renaming an interface function will be as simple as renaming a class method, where
gorename
doesn't need to be told what to affect and what to leave alone.It renames everything it needs to prevent compiler errors.
I also posted this on Stack Overflow, and there are some discussions here.
gorename
should probably check where the interface is used so the rename can affect all types passed as that interface.Alternatively, if there's no easy solution to this problem, then perhaps a fully-optional
implements
keyword can be added to Go so renaming interface functions will be as straightforward as in other languages.The text was updated successfully, but these errors were encountered: