]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/internal/gc, cmd/go: fix value of importpath symbol
authorMichael Hudson-Doyle <michael.hudson@canonical.com>
Sun, 5 Apr 2015 02:48:42 +0000 (14:48 +1200)
committerRuss Cox <rsc@golang.org>
Sun, 19 Apr 2015 20:14:59 +0000 (20:14 +0000)
In https://golang.org/cl/7797 I attempted to use myimportpath to set the value
of the go.importpath.$foo. symbol for the module being compiled, but I messed
it up and only set the name (which the linker rewrites anyway). This lead to
the importpath for the module being compiled being "". This was hard to notice,
because all modules that import another define the importpath for their
imported modules correctly -- but main is not imported, and this meant that the
reflect module saw all fields of all types defined in the main module as
exported.

The fix is to do what I meant to do the first time, add a test and change the
go tool to compile main packages with -p main and not -p
command-line-arguments.

Fixes #10332

Change-Id: I5fc6e9b1dc2b26f058641e382f9a56a526eca291
Reviewed-on: https://go-review.googlesource.com/8481
Reviewed-by: Russ Cox <rsc@golang.org>
src/cmd/go/build.go
src/cmd/internal/gc/reflect.go
test/fixedbugs/issue10332.go [new file with mode: 0644]

index 05c68fc4ec12e92ad12980514b6b9224705b8199..6734d53d5b2640c97f0caa367dbb9bd849751353 100644 (file)
@@ -1851,6 +1851,9 @@ func (gcToolchain) gc(b *builder, p *Package, archive, obj string, asmhdr bool,
        }
 
        gcargs := []string{"-p", p.ImportPath}
+       if p.Name == "main" {
+               gcargs[1] = "main"
+       }
        if p.Standard && p.ImportPath == "runtime" {
                // runtime compiles with a special 6g flag to emit
                // additional reflect type data.
index 725f224bf04825639f136ece7131c44d7eab0aef..47e24a5205b56b6c85ca57757796f96af08ae27b 100644 (file)
@@ -484,13 +484,7 @@ func dimportpath(p *Pkg) {
                dimportpath_gopkg.Name = "go"
        }
 
-       var nam string
-       if p == localpkg {
-               // Note: myimportpath != "", or else dgopkgpath won't call dimportpath.
-               nam = "importpath." + pathtoprefix(myimportpath) + "."
-       } else {
-               nam = "importpath." + p.Prefix + "."
-       }
+       nam := "importpath." + p.Prefix + "."
 
        n := Nod(ONAME, nil, nil)
        n.Sym = Pkglookup(nam, dimportpath_gopkg)
@@ -499,7 +493,12 @@ func dimportpath(p *Pkg) {
        n.Xoffset = 0
        p.Pathsym = n.Sym
 
-       gdatastring(n, p.Path)
+       if p == localpkg {
+               // Note: myimportpath != "", or else dgopkgpath won't call dimportpath.
+               gdatastring(n, myimportpath)
+       } else {
+               gdatastring(n, p.Path)
+       }
        ggloblsym(n.Sym, int32(Types[TSTRING].Width), obj.DUPOK|obj.RODATA)
 }
 
diff --git a/test/fixedbugs/issue10332.go b/test/fixedbugs/issue10332.go
new file mode 100644 (file)
index 0000000..e00a8b4
--- /dev/null
@@ -0,0 +1,25 @@
+// run
+
+// Copyright 2015 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// The PkgPath of unexported fields of types defined in package main was incorrectly ""
+
+package main
+
+import (
+       "fmt"
+       "reflect"
+)
+
+type foo struct {
+       bar int
+}
+
+func main() {
+       pkgpath := reflect.ValueOf(foo{}).Type().Field(0).PkgPath
+       if pkgpath != "main" {
+               fmt.Printf("BUG: incorrect PkgPath: %v", pkgpath)
+       }
+}