-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/compile: instantiation cycle error in method #65366
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
CC @golang/compiler, perhaps @griesemer specifically? |
Simpler case (playground): package p
type T[E any] int
func (T[E]) m(T[[]E]) {} |
This is working as intended. Since the type parameter E is not used, you can simply remove it and the code will compile. If the type parameter were used, this code couldn't be compiled, at least with the current implementation of Go which monomorphizes code (doesn't box generics): say, PS:The monomorphization check doesn't consider whether a type parameter is used or not. |
@mdempsky In case you want to add something to this. |
I agree the error here is expected and the monomorpic checker is working as intended. |
@griesemer Thank for your explain. But I still have some doubts. Is the limitation here caused by the current implementation of generics, or will there always be this limitation. Or as the generics iterate, this limitation will disappear. If this restriction will disappear in the future, when will it happen and whether it will rely on other preparatory work? If this restriction will be lifted in the future, should this issue be kept open? In addition, I have not encountered similar problems in other languages. I am not sure about the implementation in go, but I believe this can be solved. Going back to the original proposition, that is to say, I have no way to implement the chunk method on slice? So is there any compromise way to implement chunks? It doesn't involve implementing a lot of top-level functions. |
I'm not sure I understand the unused E above. The following example uses E, but still reports an error: type slice[E any] struct{ elems []E }
func (s slice[E]) chunk(n int) slice[[]E] {
return slice[[]E]{elems: [][]E{}}
} |
We want to ensure instantiation is guaranteed to reach a finite fixed point. If instantiating type T[X] requires instantiating type T[[]X], then we'll need T[[][]X], and so on. It never stops. |
@leaxoy The limitation could (perhaps) be removed if we'd always box generic values. But because our implementation uses a hybrid approach that monomorphizes code (generates code for each kind of type, for speed), your source would lead to endless code generation as @mdempsky pointed out. There are no plans to change this. Your func chunk[E any](s []E, n int) [][]E {
do something sensible here
} It's not a method but it also doesn't refer to itself anymore. |
But this break method chaining, maybe |
Go version
1.21
Output of
go env
in your module/workspace:What did you do?
https://go.dev/play/p/Rs1R0zaMrys
What did you see happen?
./prog.go:14:12: instantiation cycle:
./prog.go:16:38: E instantiated as []E
What did you expect to see?
no error occurred
The text was updated successfully, but these errors were encountered: