-
Notifications
You must be signed in to change notification settings - Fork 18k
cmd/compile: gotip generic slice to array conversation no longer compiles #49295
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
Possibly due to https://go-review.googlesource.com/c/go/+/360396/ ? |
I think the type checker was wrong before. When converting a value A conversion of a slice is permitted to a pointer of an array, not a pointer to a type parameter that is constrained by an array. But it works if the array pointer is a type in the type set of the type parameter constraint. This code works: package main
import "io"
type Reader struct {
buf []byte
}
type Token *[16]byte
func Read[T interface{ ~*[16]byte }](r *Reader) (t T, err error) {
if n := len(t); len(r.buf) >= n {
t = T(r.buf[:n])
r.buf = r.buf[n:]
return
}
err = io.EOF
return
}
func main() {
r := &Reader{buf: []byte("0123456789abcdef")}
token, err := Read[Token](r)
_, _ = token, err
} Closing as working as (currently) intended. If this needs to change we need to discuss separately. @ianlancetaylor in case he has any opinions here. |
The non generic version compiles fine. so don't see why the generic version wouldn't.
|
The generic version compiles fine, too, if the Applying the |
@griesemer That code doesn't work either. An explicit copy() works.
|
This looks like a backend failure. I'll reopen for a fix to the example in #49295 (comment) |
Yes, this T(x) type conversion rule does seems like it can limit what we can do in unifying shapes when it is used in a generic function/method. It would be nice to continue to unify pointers of all kinds in the case where T(x) is not used in the instantiation. Possibly we could have another boolean arg to Shapify() that indicates whether T(x) is used in the instantiation for which this shape is being used. If T(x) is used, then we don't use the extra pointer rule (i.e. we only unify pointer types with the same underlying type). |
Change https://golang.org/cl/361135 mentions this issue: |
What version of Go are you using (
go version
)?What did you do?
What did you expect to see?
Successful compilation, pretty sure this was working for some gotip heads.
What did you see instead?
Compiler error
./main.go:13:18: cannot convert r.buf[:n] (value of type []byte) to *T
The text was updated successfully, but these errors were encountered: