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: failed type inference #71769

Open
lee157953 opened this issue Feb 15, 2025 · 5 comments
Open

cmd/compile: failed type inference #71769

lee157953 opened this issue Feb 15, 2025 · 5 comments
Assignees
Labels
BugReport Issues describing a possible bug in the Go implementation. compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@lee157953
Copy link

lee157953 commented Feb 15, 2025

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

@seankhliao

This comment has been minimized.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Feb 15, 2025
@randall77
Copy link
Contributor

This worked in 1.23, we should keep it working in 1.24.
@griesemer

@randall77 randall77 reopened this Feb 15, 2025
@lee157953
Copy link
Author

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

@seankhliao seankhliao changed the title A compilation error cmd/compile: failed type inference Feb 15, 2025
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Feb 15, 2025
@gabyhelp gabyhelp added the BugReport Issues describing a possible bug in the Go implementation. label Feb 15, 2025
@mknyszek mknyszek added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Feb 18, 2025
@mknyszek mknyszek added this to the Backlog milestone Feb 18, 2025
@griesemer
Copy link
Contributor

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

@griesemer griesemer self-assigned this Feb 18, 2025
@griesemer griesemer modified the milestones: Backlog, Go1.25 Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BugReport Issues describing a possible bug in the Go implementation. compiler/runtime Issues related to the Go compiler and/or runtime. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
Development

No branches or pull requests

7 participants