From: Matthew Dempsky Date: Thu, 12 May 2022 22:46:20 +0000 (-0700) Subject: cmd/compile/internal/typecheck: remove iexport assumption of LocalPkg.Path == "" X-Git-Tag: go1.19beta1~250 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=e3661d5f0820820d8d5e5e357244c95f788820e2;p=gostls13.git cmd/compile/internal/typecheck: remove iexport assumption of LocalPkg.Path == "" 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 Run-TryBot: Matthew Dempsky Reviewed-by: David Chase --- diff --git a/src/cmd/compile/internal/typecheck/iexport.go b/src/cmd/compile/internal/typecheck/iexport.go index d6a7eade03..b12ddc9782 100644 --- a/src/cmd/compile/internal/typecheck/iexport.go +++ b/src/cmd/compile/internal/typecheck/iexport.go @@ -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) {