Go version
go 1.24.0
Output of go env
in your module/workspace:
set AR=ar
set CC=gcc
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_ENABLED=0
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set CXX=g++
set GCCGO=gccgo
set GO111MODULE=on
set GOAMD64=v1
set GOARCH=amd64
set GOAUTH=netrc
set GOBIN=
set GOCACHE=C:\Users\lee\AppData\Local\go-build
set GOCACHEPROG=
set GODEBUG=
set GOENV=C:\Users\lee\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFIPS140=off
set GOFLAGS=
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\lee\AppData\Local\Temp\go-build1694421810=/tmp/go-build -gno-record-gcc-switches
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMOD=D:\Dev\go_demo\go.mod
set GOMODCACHE=C:\Users\lee\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\lee\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=D:/Go
set GOSUMDB=sum.golang.org
set GOTELEMETRY=local
set GOTELEMETRYDIR=C:\Users\lee\AppData\Roaming\go\telemetry
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=D:\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.24.0
set GOWORK=
set PKG_CONFIG=pkg-config
What did you do?
type Set[E comparable] map[E]struct{}
func NewSet[E comparable](cap int) Set[E] {
return make(map[E]struct{}, cap)
}
func (s Set[E]) AppendSelf(element E) Set[E] {
s[element] = struct{}{}
return s
}
type FromIterator[E, ES any] interface {
AppendSelf(E) ES
}
func Map[T, R any, RS FromIterator[R, RS]](ts []T, fn func(T) R, toFn func(int) RS) RS {
rs := toFn(len(ts))
for _, t := range ts {
rs = rs.AppendSelf(fn(t))
}
return rs
}
func main() {
arr := []int{1, 2, 3, 4, 5, 6}
//compile:
//1.23.6 ok
//1.24.0 err
set := Map(arr, func(t int) int {
return t + 1
}, NewSet)
fmt.Println(set)
}
What did you see happen?
compile ok
What did you expect to see?
compile ok
Comment From: seankhliao
I believe the error is correct, there's no way to infer RS from the given args.
Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.
For questions please refer to https://github.com/golang/go/wiki/Questions
Comment From: randall77
This worked in 1.23, we should keep it working in 1.24. @griesemer
Comment From: lee-ext
I believe the error is correct, there's no way to infer RS from the given args.
Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.
For questions please refer to https://github.com/golang/go/wiki/Questions
However, the compilation was successful in go 1.23
Comment From: gabyhelp
Related Issues
- cmd/compile: reflect.Type does not implement comparable #53249 (closed)
- cmd/compile: "internal compiler error: is not shape-identical" crash #66663 (closed)
- cmd/go2go: type parameter used in map key causes compile-time error #41154 (closed)
- S #53999 (closed)
- cmd/compile: internal compiler error: found illegal assignment FUNC-func() T₁ -> main.T₁; #48137 (closed)
- Exmaple #53998 (closed)
- cmd/compile: internal compiler error: Type.Elem TYPEPARAM #48609 (closed)
- cmd/compile: cannot use s (type [][]Typ) as type []go.shape.[]int_0 in assignment #55964 (closed)
- compiler: internal compiler error: assertion failed when using concrete nested type in interface constraint #50520 (closed)
- What it is bug #56563 (closed)
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Comment From: griesemer
Simpler reproducer (playground):
package main
type Set[E comparable] map[E]struct{}
func NewSet[E comparable]() Set[E] { return nil }
func (Set[E]) m(E) Set[E] { return nil }
func Map[R any, RS interface{ m(R) RS }](_ func() R, toFn func() RS) RS {
return toFn()
}
func main() {
Map(func() int { return 0 }, NewSet)
}
Comment From: lee-ext
@seankhliao This issue still exists in the Go 1.25 version.