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: using generics with -buildmode=plugin results in compiler crash #52937

Closed
ryanjoneil opened this issue May 16, 2022 · 7 comments
Closed
Labels
NeedsFix
Milestone

Comments

@ryanjoneil
Copy link

@ryanjoneil ryanjoneil commented May 16, 2022

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

$ go version
go version go1.18.2 darwin/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
GOARCH="amd64"
GOOS="darwin"
GOVERSION="go1.18.2"

What did you do?

I can compile this plugin:

package main

func Foo(bar any) any {
	return bar
}

func main() {}
foo % go build --buildmode plugin
foo % ls -hl foo.so 
-rw-r--r--  1 ryan  staff   1.2M May 16 14:17 foo.so

But if I change Foo to Foo[T any] I get a compiler error:

package main

func Foo[T any](bar T) T {
	return bar
}

func main() {}
foo % go build -buildmode=plugin
# foo
./main.go:4:9: internal compiler error: writeType func[Foo.T](Foo.T) Foo.T

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

What did you expect to see?

I expected it to compile a foo.so plugin file.

What did you see instead?

./main.go:4:9: internal compiler error: writeType func[Foo.T](Foo.T) Foo.T

@ryanjoneil ryanjoneil changed the title go/build: using generics with -buildmode=plugin results in go/build: using generics with -buildmode=plugin results in compiler error May 16, 2022
@ianlancetaylor ianlancetaylor changed the title go/build: using generics with -buildmode=plugin results in compiler error cmd/compile: using generics with -buildmode=plugin results in compiler crash May 16, 2022
@ianlancetaylor ianlancetaylor added the NeedsInvestigation label May 16, 2022
@ianlancetaylor ianlancetaylor added this to the Go1.19 milestone May 16, 2022
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented May 16, 2022

CC @randall77 @mdempsky

# command-line-arguments
../../foo10.go:4:9: internal compiler error: writeType func[Foo.T](Foo.T) Foo.T

goroutine 1 [running]:
runtime/debug.Stack()
	../../go/src/runtime/debug/stack.go:24 +0x65
cmd/compile/internal/base.FatalfAt({0x0?, 0x0?}, {0xd34328, 0xc}, {0xc000461b20, 0x1, 0x1})
	../../go/src/cmd/compile/internal/base/print.go:227 +0x1d7
cmd/compile/internal/base.Fatalf(...)
	../../go/src/cmd/compile/internal/base/print.go:196
cmd/compile/internal/reflectdata.writeType(0xc00013cecc?)
	../../go/src/cmd/compile/internal/reflectdata/reflect.go:947 +0x105
cmd/compile/internal/reflectdata.WriteTabs()
	../../go/src/cmd/compile/internal/reflectdata/reflect.go:1370 +0x125
cmd/compile/internal/gc.dumpdata()
	../../go/src/cmd/compile/internal/gc/obj.go:119 +0x73
cmd/compile/internal/gc.Main(0xd60548)
	../../go/src/cmd/compile/internal/gc/main.go:319 +0x1165
main.main()
	../../go/src/cmd/compile/main.go:55 +0xdd

@mdempsky
Copy link
Member

@mdempsky mdempsky commented May 16, 2022

I assume we don't expect package plugin to be able to access generic functions?

If so, seems like this should be an easy fix.

@mdempsky mdempsky self-assigned this May 16, 2022
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented May 17, 2022

Certainly the plugin package is not going to be able to access an uninstantiated generic function.

It's likely that it also shouldn't be able to access an instantiated version of a generic function.

@gopherbot
Copy link

@gopherbot gopherbot commented May 17, 2022

Change https://go.dev/cl/406358 mentions this issue: cmd/compile: skip exporting generic functions for -buildmode=plugin

@mdempsky
Copy link
Member

@mdempsky mdempsky commented May 17, 2022

FWIW, the workaround for this in Go 1.18 is to not declare exported generic functions in your main package. E.g., rename Foo to _Foo.

I don't think this needs a backport to Go 1.18. It's something that has never worked (generics didn't exist prior to Go 1.18), only affects plugins (which are relatively unused/unsupported anyway), and there's an easy workaround (rename affected functions in a single package).

If anyone else disagrees though, feel free to request a backport. The CL should cleanly apply to the 1.18 release branch.

@dmitshur dmitshur added NeedsFix and removed NeedsInvestigation labels May 17, 2022
@ryanjoneil
Copy link
Author

@ryanjoneil ryanjoneil commented May 17, 2022

I was actually hoping on a lark that this would work inside a plugin, but it makes sense why it wouldn't. It's easy enough to use any inside the plugin and wrap the appropriate functions with generics in the code that loads it. The biggest thing was that the error was opaque.

@gopherbot
Copy link

@gopherbot gopherbot commented May 17, 2022

Change https://go.dev/cl/406774 mentions this issue: test: fix builders that do not support plugin

gopherbot pushed a commit that referenced this issue May 17, 2022
CL 406358 added test that use -buildmode=plugin. But plugin mode only
supports on some os/arch pairs, so this CL moving the test to
misc/cgo/testplugin directory instead.

Updates #52937

Change-Id: Iad049443c1f6539f6af1988bebd4dff56c6e1bf9
Reviewed-on: https://go-review.googlesource.com/c/go/+/406774
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix
Projects
None yet
Development

No branches or pull requests

5 participants