-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: cmd/vet: warn if a method receiver uses a known type-name as the name of a type parameter #48123
Comments
(#47657 (comment) also hints at the possibility of method-specialization at some point down the road, in which case this sort of definition would be even more confusing.) |
The 'frequency' criterion for vet would imply we'd need to see this problem in the wild before deciding whether vet should catch it. This preemption might be unnecessary. And also unlikely to be needed, it seems to me. |
How would you measure occurrences in the wild? I would expect that the vast majority would be caught during testing and code review, but only after a non-trivial amount of time trying to debug the problem. The non-trivial debugging step is what I would hope to eliminate by adding the |
If |
@jimmyfrasche, it cannot be used as the syntax for specialization later. (Specialization, if added, would need to have a different syntax, but I think that is a matter for a separate proposal.) |
Vet has never aimed to catch things that testing will catch anyway, but to find the things that testing is likely to miss. |
@bcmills I don't know why |
@bjorndm |
It should be allowed since |
@bjorndm, I don't disagree but I think that is a matter for a separate proposal. (This proposal is, “given that this is allowed today, we should warn about the confusing cases”.) |
@mattn, if the original declaration were something like type T[int constraints.Numeric] struct{}
func (T[int]) m0() {
…
} then I think it would be much less confusing: if the type parameter is consistently named The part that makes it confusing in the original |
@bcmills to nitpick the previous example a bit, I suspect this will still be mildly surprising to a decent number of people reading this code. |
@zephyrtronium, @mattn I didn't know you could do that at all! Very confusing indeed. But stepping back, I can see that this boat has sailed, and that arguably, being able to redefine a built in type, could be useful in some cases. However this is not applicable to this issue. Generics are still new. |
I agree that this seems like a premature check. |
This proposal has been added to the active column of the proposals project |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
Consider the program in https://go2goplay.golang.org/p/O9E46VGCBcx, inspired by @griesemer's comment in #46477 (comment):
Today (
go1.18-717f337d1
), this method compiles, binding the type parameterP
(which is renamed toint
in them0
method) tostring
, and then them0
method printsx is a string
(https://go2goplay.golang.org/p/O9E46VGCBcx).However, as a reader of this code — and especially coming from a background of working with languages like Rust and C++ that allow method specialization — the behavior of this program is somewhat surprising.
Ideally, as a reader I would like to see the same type parameter name (
P
) in all method definitions, in much the same way that I would like to see the same receiver name in all method definitions. I can understand if we don't want to require that in general for generic definitions, but I think it would at least be useful forvet
to reject definitions that look like specializations but aren't.Specifically, I propose that
vet
should emit a warning when both:So this code would receive a warning:
but this would not:
(CC @timothy-king @ianlancetaylor @mdempsky)
The text was updated successfully, but these errors were encountered: