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/134915
Reviewed-by: Ian Lance Taylor <iant@golang.org>
diff --git a/src/cmd/go/internal/modload/build.go b/src/cmd/go/internal/modload/build.go
index cebb802..06636c4 100644
--- a/src/cmd/go/internal/modload/build.go
+++ b/src/cmd/go/internal/modload/build.go
@@ -232,11 +232,16 @@
}
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
}
diff --git a/src/cmd/go/testdata/script/mod_string_alias.txt b/src/cmd/go/testdata/script/mod_string_alias.txt
new file mode 100644
index 0000000..5c3d428
--- /dev/null
+++ b/src/cmd/go/testdata/script/mod_string_alias.txt
@@ -0,0 +1,14 @@
+[short] skip
+
+env GO111MODULE=on
+
+go mod init golang.org/issue/27584
+
+go build .
+
+-- main.go --
+package main
+
+type string = []int
+
+func main() {}