Skip to content
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: gotip generic slice to array conversation no longer compiles #49295

Closed
renthraysk opened this issue Nov 2, 2021 · 9 comments
Closed
Labels
NeedsInvestigation release-blocker

Comments

@renthraysk
Copy link

@renthraysk renthraysk commented Nov 2, 2021

What version of Go are you using (go version)?

$ go version
go version devel go1.18-088bb4bf4a Tue Nov 2 06:25:39 2021 +0000 linux/amd64

What did you do?

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
}

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

@ALTree
Copy link
Member

@ALTree ALTree commented Nov 2, 2021

Possibly due to https://go-review.googlesource.com/c/go/+/360396/ ?

@thanm thanm added the NeedsInvestigation label Nov 2, 2021
@thanm
Copy link
Contributor

@thanm thanm commented Nov 2, 2021

@griesemer @findleyr

@griesemer
Copy link
Contributor

@griesemer griesemer commented Nov 2, 2021

I think the type checker was wrong before. When converting a value x to a type T, T(x), where T is a type parameter (and x is not of type parameter), the conversion is valid if x can be converted to any type in the type set of T.

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.

@renthraysk
Copy link
Author

@renthraysk renthraysk commented Nov 2, 2021

The non generic version compiles fine. so don't see why the generic version wouldn't.

func ReadToken(r *Reader) (t Token, err error) {
	if n := len(t); len(r.buf) >= n {
		t = *(*Token)(r.buf[:n])
		r.buf = r.buf[n:]
		return
	}
	err = io.EOF
	return
}

@griesemer
Copy link
Contributor

@griesemer griesemer commented Nov 2, 2021

The generic version compiles fine, too, if the *Token is part of the type set of the type parameter. Again, the rule is: In a conversion T(x) where T is a type parameter, the conversion is valid if x can be converted to each possible type in T. What you are doing in your code is converting a slice r.buf[:n] of type []byte to *T, and *T is a pointer type (not a generic type), and *T is not a pointer to an array, it's a pointer to a type parameter (which happens to be constrained by an array). There's no rule in existing Go that permits the conversion of a slice to a pointer of a non-array type, hence this is not permitted.

Applying the * to a type parameter doesn't apply it to all the types in the type parameter, there is no transitivity here. This are simply the consequences of the above simple rule for type parameters. Maybe that's not what we want eventually, but that's what we have at the moment. Feel free to open an issue and propose a different rule. Thanks.

@renthraysk
Copy link
Author

@renthraysk renthraysk commented Nov 2, 2021

@griesemer That code doesn't work either.
./main.go:15:8: cannot convert r.buf[:n] (type []byte) to type .shape.*uint8_0

An explicit copy() works.

func Read[T interface{ ~[16]byte }](r *Reader) (t T, err error) {
	if n := len(t); len(r.buf) >= n {
		copy(t[:n], r.buf[:n])
		r.buf = r.buf[n:]
		return
	}
	err = io.EOF
	return
}

@randall77
Copy link
Contributor

@randall77 randall77 commented Nov 2, 2021

./main.go:15:8: cannot convert r.buf[:n] (type []byte) to type .shape.*uint8_0

This looks like a backend failure.
I think this falls over due to how we stencil. We make a single implementation for all pointer type parameters, but this case requires we distinguish ptr-to-array from ptr-to-not-an-array.
This might be a bit tricky to fix.
@danscales

I'll reopen for a fix to the example in #49295 (comment)

@danscales
Copy link
Contributor

@danscales danscales commented Nov 3, 2021

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).

@gopherbot
Copy link

@gopherbot gopherbot commented Nov 3, 2021

Change https://golang.org/cl/361135 mentions this issue: cmd/compile: make pointers to arrays their own shape

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation release-blocker
Projects
None yet
Development

No branches or pull requests

7 participants