You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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"
)
funcIntToBytes[T constraints.Integer](iT) []byte {
return (*(*[8]byte)(unsafe.Pointer(&i)))[:unsafe.Sizeof(i)]
}
// This function is the one that is not working.funcBytesToInt[T constraints.Integer](b []byte) T {
return*(*T)(unsafe.Pointer(&b[0]))
}
funcmain() {
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"
)
funcIntToBytes[T constraints.Integer](iT) []byte {
return (*(*[8]byte)(unsafe.Pointer(&i)))[:unsafe.Sizeof(i)]
}
// I was expecting something like this by the compilerfuncBytesToInt(b []byte) int64 {
return*(*int64)(unsafe.Pointer(&b[0]))
}
funcmain() {
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:
cannot infer T
The text was updated successfully, but these errors were encountered:
shoriwe
changed the title
affected/package:
affected/package: Generics not properly working when been used with unsafe casts
Feb 7, 2022
shoriwe
changed the title
affected/package: Generics not properly working when been used with unsafe casts
Generics not properly working when been used with unsafe casts
Feb 7, 2022
shoriwe
changed the title
Generics not properly working when been used with unsafe casts
cmd/compile: Generics not properly working when been used with unsafe casts
Feb 7, 2022
The compiler cannot infer T for BytesToInt because none of the parameters are of type T. Note that the T for BytesToInt is not the same T as the one for IntToBytes. The compiler is correct to give this error. You can do it differently like in the example below.
What version of Go are you using (
go version
)?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
OutputWhat 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:
The text was updated successfully, but these errors were encountered: