Commit 21f3d581 authored by Bryan C. Mills's avatar Bryan C. Mills

cmd/go: avoid type names in __debug__modinfo__ variable injected in package main

If we use the name 'string' to refer to the built-in type, that name
can be shadowed by a local declaration. Use a string constant instead,
but keep the init function to populate it so that //go:linkname will
still work properly.

Fixes #27584.

Change-Id: I850cad6663e566f70fd123107d2e4e742c93b450
Reviewed-on: https://go-review.googlesource.com/134915Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent b07f60b9
......@@ -232,11 +232,16 @@ func findModule(target, path string) module.Version {
}
func ModInfoProg(info string) []byte {
// Inject a variable with the debug information as runtime/debug.modinfo,
// but compile it in package main so that it is specific to the binary.
// Populate it in an init func so that it will work with go:linkname,
// but use a string constant instead of the name 'string' in case
// package main shadows the built-in 'string' with some local declaration.
return []byte(fmt.Sprintf(`
package main
import _ "unsafe"
//go:linkname __debug_modinfo__ runtime/debug.modinfo
var __debug_modinfo__ string
var __debug_modinfo__ = ""
func init() {
__debug_modinfo__ = %q
}
......
[short] skip
env GO111MODULE=on
go mod init golang.org/issue/27584
go build .
-- main.go --
package main
type string = []int
func main() {}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment