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

go/types,types2: internal error when function as generic argument #52378

Closed
peterq opened this issue Apr 16, 2022 · 4 comments
Closed

go/types,types2: internal error when function as generic argument #52378

peterq opened this issue Apr 16, 2022 · 4 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@peterq
Copy link

peterq commented Apr 16, 2022

What version of Go are you using (go version)?

$ go version
go version go1.18 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/peterq/.cache/go-build"
GOENV="/home/peterq/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS="-mod=mod"
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/peterq/go/pkg/mod"
GOOS="linux"
GOPATH="/home/peterq/go:/usr/local/lib/tinygo"
GOPROXY="https://goproxy.cn,direct"
GOROOT="/home/peterq/dev/env/go_multi_version/go1.18"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/peterq/dev/env/go_multi_version/go1.18/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/peterq/dev/projects/go/github.com/peterq/maicai/test/go.mod"
GOWORK=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2372424038=/tmp/go-build -gno-record-gcc-switches"

What did you do?

run following code

package main

type StateMachine[T any] struct {
	data *T
}

func NewStateMachine[T any](data *T) *StateMachine[T] {
	return &StateMachine[T]{data: data}
}

func UpdateState[T any](m *StateMachine[T], fn func(data *T)) {
	fn(m.data)
}

type StateFoo[T any] struct {
	Bar      string
	SubState T
}

func NewFoo[T any]() *Foo[T] {
	return &Foo[T]{
		StateMachine: NewStateMachine(&StateFoo[T]{
			Bar: "",
		}),
	}
}

type Foo[T any] struct {
	StateMachine *StateMachine[StateFoo[T]]
}

func (f *Foo[T]) Update(fn func(*StateFoo[T])) {
	UpdateState(f.StateMachine, fn)
}

func main() {
	type SubState struct {
		X string
	}
	f := NewFoo[SubState]()
	f.Update(func(foo *StateFoo[SubState]) {})
}

//internal compiler error: assigning m.data (type *go.shape.struct { Bar string; SubState struct { X string } }_0) to parameter <S> (type *StateFoo["".SubState·1])
//internal compiler error: assigning m.data (type *go.shape.struct { Bar string; SubState struct { X string } }_0) to parameter foo (type *StateFoo["".SubState·1])

What did you expect to see?

compile without error

What did you see instead?

./main.go:41:10: internal compiler error: assigning m.data (type *go.shape.struct { Bar string; SubState struct { X string } }_0) to parameter foo (type *StateFoo["".SubState·1])

Please file a bug report including a short program that triggers the error.
https://go.dev/issue/new

Compilation finished with exit code 2

@peterq
Copy link
Author

peterq commented Apr 16, 2022

I found a way to avoid this error, but it seems weird.
change:

func (f *Foo[T]) Update(fn func(*StateFoo[T])) {
	UpdateState(f.StateMachine, fn)
}

to:

func (f *Foo[T]) Update(fn func(*StateFoo[T])) {
	var any any = fn
	_ = any
	UpdateState(f.StateMachine, fn)
}

@mengzhuo mengzhuo changed the title run some generic code, got this: Please file a bug report including a short program that triggers the error. go/types,types2: internal error when function as generic argument Apr 16, 2022
@mengzhuo
Copy link
Contributor

cc @griesemer

@ianlancetaylor
Copy link
Contributor

CC @randall77 @mdempsky

@ianlancetaylor ianlancetaylor added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Apr 17, 2022
@ianlancetaylor ianlancetaylor added this to the Go1.19 milestone Apr 17, 2022
@ianlancetaylor
Copy link
Contributor

Thanks for the report. This appears to be fixed now, so closing.

@golang golang locked and limited conversation to collaborators Jun 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
Status: Done
Development

No branches or pull requests

4 participants