What version of Go are you using (go version)?
$ go version
go version go1.18beta2 windows/amd64
Does this issue reproduce with the latest release?
It should reproduce with go1.18beta2
What operating system and processor architecture are you using (go env)?
go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\?\AppData\Local\go-build
set GOENV=C:\Users\?\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\?\go\pkg\mod
set GONOPROXY=*
set GONOSUMDB=*
set GOOS=windows
set GOPATH=C:\Users\?\go
set GOPRIVATE=*
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Users\?\sdk\go1.18beta2
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Users\?\sdk\go1.18beta2\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.18beta2
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\?\AppData\Local\Temp\go-build1818620206=/tmp/go-build -gno-record-gcc-switches
What did you do?
I was testing the generics feature to simplify the direct transformation of a slice sequence to an integer type.
Everything works fine in the int to bytes direction but the compiler is unable to properly compile the generic function that transform bytes to int.
package main
import (
"constraints"
"fmt"
"unsafe"
)
func IntToBytes[T constraints.Integer](i T) []byte {
return (*(*[8]byte)(unsafe.Pointer(&i)))[:unsafe.Sizeof(i)]
}
// This function is the one that is not working.
func BytesToInt[T constraints.Integer](b []byte) T {
return *(*T)(unsafe.Pointer(&b[0]))
}
func main() {
bs := IntToBytes(-387)
fmt.Println(int64(BytesToInt(bs))) // The compiler is unable to properly create a generic from this
}
I know that those kind of transformations are possible because this example works fine:
package main
import (
"constraints"
"fmt"
"unsafe"
)
func IntToBytes[T constraints.Integer](i T) []byte {
return (*(*[8]byte)(unsafe.Pointer(&i)))[:unsafe.Sizeof(i)]
}
// I was expecting something like this by the compiler
func BytesToInt(b []byte) int64 {
return *(*int64)(unsafe.Pointer(&b[0]))
}
func main() {
bs := IntToBytes(int64(-387))
fmt.Println(BytesToInt(bs))
}
What did you expect to see?
I expect to see the correct compilation of the program.
What did you see instead?
The compilation error:
What version of Go are you using (
go version)?Does this issue reproduce with the latest release?
It should reproduce with
go1.18beta2What operating system and processor architecture are you using (
go env)?go envOutputWhat did you do?
I was testing the generics feature to simplify the direct transformation of a slice sequence to an integer type.
Everything works fine in the int to bytes direction but the compiler is unable to properly compile the generic function that transform bytes to int.
I know that those kind of transformations are possible because this example works fine:
What did you expect to see?
I expect to see the correct compilation of the program.
What did you see instead?
The compilation error: