What version of Go are you using (go version)?
$ go version
go version go1.20.2 linux/amd64
Does this issue reproduce with the latest release?
Yes, at lest in go.1.18
What operating system and processor architecture are you using (go env)?
go env Output
$ go env
go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.2"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build674946051=/tmp/go-build -gno-record-gcc-switches"
What did you do?
GO code:
package main
import "fmt"
type values interface {
string | int | bool
}
func checkValue[T values](v T, check func(value T) bool) bool {
return check(v)
}
func main() {
v := 0.1 // float
f := func(value string) bool { return true }
fmt.Print(checkValue(v, f))
}
// the same code in playground: https://go.dev/play/p/DHjAFwEoEnn?v=goprev
What did you expect to see?
Error message about wrong type of the first argument v as float64 is not a part of type values.
What did you see instead?
Compile error:
go build
# demo
./main.go:16:26: type func(value string) bool of f does not match inferred type func(value float64) bool for func(value T) bool
It is wrong and very confusing error message as function has correct type. Type of its parameter compared with the first parameter type instead of comparison to type values.
In opposite: the v argument has incorrect type (according to type values definition) but this is not reported as error.
What version of Go are you using (
go version)?Does this issue reproduce with the latest release?
Yes, at lest in go.1.18
What operating system and processor architecture are you using (
go env)?go envOutputWhat did you do?
GO code:
What did you expect to see?
Error message about wrong type of the first argument
vasfloat64is not a part of typevalues.What did you see instead?
Compile error:
It is wrong and very confusing error message as function has correct type. Type of its parameter compared with the first parameter type instead of comparison to type
values.In opposite: the
vargument has incorrect type (according to typevaluesdefinition) but this is not reported as error.