-
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
cmd/compile: cannot return a value even its type subjects to type constraints #51501
Comments
type MyInt int
In fact, even if the func foo2[T int]() T {
x := 42
return int(x) // ERROR: cannot use int(x) (value of type int) as type T in return statement
} it still doesn't compile. Please view #51403 for details. |
Sigh, sounds very limited. Though I noticed that we could: func foo2[T ~int]() T {
x := 42
return T(int(x)) // OK
} But use cases like this still can't work: package main
type A struct{}
func (a A) Foo() {}
type B struct{}
func (b B) Foo() {}
type C interface{ Foo() }
func Want[T C]() *T {
var x T
switch (interface{})(x).(type) {
case A:
return (*T)(&A{}) // cannot convert &A{} (value of type *A) to type *T: *A does not implement *T (type *T is pointer to interface, not interface)
case B:
return (*T)(&B{}) // cannot convert &B{} (value of type *B) to type *T: *B does not implement *T (type *T is pointer to interface, not interface)
default:
panic("unknown")
}
}
func main() {
Want[A]()
Want[B]()
} One have to import unsafe: package main
import "unsafe"
type A struct{}
func (a A) Foo() {}
type B struct{}
func (b B) Foo() {}
type C interface{ Foo() }
func Want[T C]() *T {
var x T
switch (interface{})(x).(type) {
case A:
return (*T)(unsafe.Pointer(&A{})) // OK
case B:
return (*T)(unsafe.Pointer(&B{})) // OK
default:
panic("unknown")
}
}
func main() {
Want[A]()
Want[B]()
} |
The pointer conversion problem is the same as: #50815 The key point here is the underlying type of |
Does this issue reproduce with the latest release?
Yes
What did you do?
https://go.dev/play/p/pCzjLWAepvO?v=gotip
What did you expect to see?
Compile pass.
What did you see instead?
Compile error.
The text was updated successfully, but these errors were encountered: