-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/compile: Compiler Type Inferencing on Methods With Narrowing Types #60975
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
Comments
Thanks for reporting. Does this also happen in Go 1.20 and/or 1.19, or did something change at Go tip only? |
This doesn't mean only apply when the type is func (t Thing[int]) PrintThing2(inputParam int) {}
// is equivalent to
func (t Thing[T]) PrintThing2(inputParam T) {} |
see also #48123 |
@seankhliao Any sense for whether we have seen this confusion enough for a vet check? |
There's also a faq entry https://go.dev/doc/faq#types_in_method_declaration There are some reports But doesn't appear to be common in committed code: So maybe it is more of an education problem, since you're unlikely to do it once you know it doesn't work? |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes, on go tip
What did you do?
There are two scenarios here.
Scenario 1:
https://go.dev/play/p/z_69dFItT4D?v=gotip
Scenario 2:
https://go.dev/play/p/PNQ2bWh3BO-?v=gotip
What did you expect to see?
For scenario 1: A compiler error saying
PrintThing2
cannot be called using variabletwo
because typesThing[int]
andThing[string]
do not match. I can understand how this scenario would be an unsupported edge case because of the complications of having to dynamically filter method bindings upon struct creation, but if methods really are just functions, then I could also see this being a reasonable feature to have implemented. If this feature is allowed, then there is a secondary issue where theinputParam
argument changes type.For scenario 2: This builds off of scenario 1. In this case the compiler throws a rather confusing error saying that an int is not an int. I do not see why the
inputParam
variable would be constrained by any when it is strictly declared as inint
. I would expect to be able to callintAsArg
without issue. Of note is the same error regarding theinputParam
changing its type is present.The text was updated successfully, but these errors were encountered: