]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/typecheck: remove iexport assumption of LocalPkg.Path == ""
authorMatthew Dempsky <mdempsky@google.com>
Thu, 12 May 2022 22:46:20 +0000 (15:46 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Fri, 13 May 2022 21:12:24 +0000 (21:12 +0000)
The indexed export data format encodes the local package's path as "",
because that's historically how we've represented it within
cmd/compile. The format also requires the local package to be first in
the exported list of packages, and was implicitly relying on ""
sorting before other, non-empty package paths.

We can't change the format without breaking existing importers (e.g.,
go/internal/gcimporter), but we can at least remove the dependency on
LocalPkg.Path being "".

Prep refactoring for CL 393715.

Updates #51734.

Change-Id: I6dd4eafd2d538f4e81376948ef9e92fc44a5462a
Reviewed-on: https://go-review.googlesource.com/c/go/+/406057
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: David Chase <drchase@google.com>
src/cmd/compile/internal/typecheck/iexport.go

index d6a7eade035cc6b08d9e4c3b898055591444c6ab..b12ddc978274c307ad9c9f9736afa153f7f862bf 100644 (file)
@@ -394,12 +394,15 @@ func (w *exportWriter) writeIndex(index map[*types.Sym]uint64, mainIndex bool) {
                pkgs = append(pkgs, pkg)
        }
        sort.Slice(pkgs, func(i, j int) bool {
-               return pkgs[i].Path < pkgs[j].Path
+               return exportPath(pkgs[i]) < exportPath(pkgs[j])
        })
+       if mainIndex {
+               base.Assertf(pkgs[0] == types.LocalPkg, "LocalPkg must be first")
+       }
 
        w.uint64(uint64(len(pkgs)))
        for _, pkg := range pkgs {
-               w.string(pkg.Path)
+               w.string(exportPath(pkg))
                if mainIndex {
                        w.string(pkg.Name)
                        w.uint64(uint64(pkg.Height))
@@ -714,7 +717,18 @@ func (w *exportWriter) pkg(pkg *types.Pkg) {
        // Ensure any referenced packages are declared in the main index.
        w.p.allPkgs[pkg] = true
 
-       w.string(pkg.Path)
+       w.string(exportPath(pkg))
+}
+
+// exportPath returns the path for pkg as it appears in the iexport
+// file format. For historical reasons (before cmd/compile required
+// the -p flag), the local package is represented as the empty string,
+// instead of its actual path.
+func exportPath(pkg *types.Pkg) string {
+       if pkg == types.LocalPkg {
+               return ""
+       }
+       return pkg.Path
 }
 
 func (w *exportWriter) qualifiedIdent(n *ir.Name) {