-
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
spec: function type inference ignores type parameter constraints #50272
Comments
@griesemer @findleyr The error is on the This does seem like it should be inferable, so maybe a better error message if not possible? |
I don't think we consider the structural type of a type parameter type argument during type inference, In this example,
EDIT: the comments above about whether this is allowed in the spec were inaccurate. It is possible that we could infer these types by considering structural types. |
@findleyr Don't get it This doesn't compile with very 'helpful' func a[T any, S ~[]T](s S) int {
return b(s)
}
func b[T any, S ~[]T](s S) int {
return len(s)
}
func main() {
a(make([]int, 0))
} This compiles: func a[T any, S ~[]T](s S) int {
return b[T, S](s)
}
func b[T any, S ~[]T](s S) int {
return len(s)
}
func main() {
a(make([]int, 0))
} |
@xaoctech sorry, you are right; I thought that we had broken this type of inference by the recent change to the definition of the underlying of type parameters. But in retrospect of course this instantiation must work. So perhaps we can make this inference work as well, or at least produce a better error message. |
Change https://golang.org/cl/373414 mentions this issue: |
CL 373414 "makes this work" as a proof of concept but is not principled. It will take a bit more thought to see if/how this fits within our specification of type inference (in progress in https://golang.org/cl/367954). |
The suggested type inference definitely does not work today. This code is basically saying that because the constraint of the type parameter I do not think that we should change the type inference rules at all for 1.18 at this point, so I'm advancing the milestone to 1.19. I think the right way forward is to make a language change proposal describing the exact changes to the inference rules. It's not really obvious to me at a quick glance. It would be something about adding a new rule for handling inference for a value whose type is or contains a type parameter. |
We've been using core types of unbound type parameters since Go 1.19 at least. All these examples work now. Closing as fixed. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
No, generics are required
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Compile this code:
What did you expect to see?
The code should compile successfully
What did you see instead?
Compilation failed with
type S of s does not match []T (cannot infer T)
error messageThis code compiles fine:
The text was updated successfully, but these errors were encountered: