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/134915
Reviewed-by: Ian Lance Taylor <iant@golang.org>
}
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
}
--- /dev/null
+[short] skip
+
+env GO111MODULE=on
+
+go mod init golang.org/issue/27584
+
+go build .
+
+-- main.go --
+package main
+
+type string = []int
+
+func main() {}