Skip to content

Commit ab6dbf9

Browse files
committed
go/internal/gcimporter: use empty string for the top-level package path
I made a mistake with the initial port of iexport.go in that I left the original package path of the top-level package in the export data. The package path for the top-level package should have been empty so that it can be changed when the package is loaded. Updates golang/go#28260 Change-Id: I781e63317a54eaf59385f25d18609e73ff97d572 Reviewed-on: https://go-review.googlesource.com/c/tools/+/201097 Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 0bb5a05 commit ab6dbf9

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

go/internal/gcimporter/iexport.go

+19-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ import (
2626
const iexportVersion = 0
2727

2828
// IExportData returns the binary export data for pkg.
29+
//
2930
// If no file set is provided, position info will be missing.
31+
// The package path of the top-level package will not be recorded,
32+
// so that calls to IImportData can override with a provided package path.
3033
func IExportData(fset *token.FileSet, pkg *types.Package) (b []byte, err error) {
3134
defer func() {
3235
if e := recover(); e != nil {
@@ -46,6 +49,7 @@ func IExportData(fset *token.FileSet, pkg *types.Package) (b []byte, err error)
4649
stringIndex: map[string]uint64{},
4750
declIndex: map[types.Object]uint64{},
4851
typIndex: map[types.Type]uint64{},
52+
localpkg: pkg,
4953
}
5054

5155
for i, pt := range predeclared() {
@@ -71,7 +75,7 @@ func IExportData(fset *token.FileSet, pkg *types.Package) (b []byte, err error)
7175
// Append indices to data0 section.
7276
dataLen := uint64(p.data0.Len())
7377
w := p.newWriter()
74-
w.writeIndex(p.declIndex, pkg)
78+
w.writeIndex(p.declIndex)
7579
w.flush()
7680

7781
// Assemble header.
@@ -93,14 +97,14 @@ func IExportData(fset *token.FileSet, pkg *types.Package) (b []byte, err error)
9397
// we're writing out the main index, which is also read by
9498
// non-compiler tools and includes a complete package description
9599
// (i.e., name and height).
96-
func (w *exportWriter) writeIndex(index map[types.Object]uint64, localpkg *types.Package) {
100+
func (w *exportWriter) writeIndex(index map[types.Object]uint64) {
97101
// Build a map from packages to objects from that package.
98102
pkgObjs := map[*types.Package][]types.Object{}
99103

100104
// For the main index, make sure to include every package that
101105
// we reference, even if we're not exporting (or reexporting)
102106
// any symbols from it.
103-
pkgObjs[localpkg] = nil
107+
pkgObjs[w.p.localpkg] = nil
104108
for pkg := range w.p.allPkgs {
105109
pkgObjs[pkg] = nil
106110
}
@@ -119,12 +123,12 @@ func (w *exportWriter) writeIndex(index map[types.Object]uint64, localpkg *types
119123
}
120124

121125
sort.Slice(pkgs, func(i, j int) bool {
122-
return pkgs[i].Path() < pkgs[j].Path()
126+
return w.exportPath(pkgs[i]) < w.exportPath(pkgs[j])
123127
})
124128

125129
w.uint64(uint64(len(pkgs)))
126130
for _, pkg := range pkgs {
127-
w.string(pkg.Path())
131+
w.string(w.exportPath(pkg))
128132
w.string(pkg.Name())
129133
w.uint64(uint64(0)) // package height is not needed for go/types
130134

@@ -141,6 +145,8 @@ type iexporter struct {
141145
fset *token.FileSet
142146
out *bytes.Buffer
143147

148+
localpkg *types.Package
149+
144150
// allPkgs tracks all packages that have been referenced by
145151
// the export data, so we can ensure to include them in the
146152
// main index.
@@ -193,6 +199,13 @@ type exportWriter struct {
193199
prevLine int64
194200
}
195201

202+
func (w *exportWriter) exportPath(pkg *types.Package) string {
203+
if pkg == w.p.localpkg {
204+
return ""
205+
}
206+
return pkg.Path()
207+
}
208+
196209
func (p *iexporter) doDecl(obj types.Object) {
197210
w := p.newWriter()
198211
w.setPkg(obj.Pkg(), false)
@@ -302,7 +315,7 @@ func (w *exportWriter) pkg(pkg *types.Package) {
302315
// Ensure any referenced packages are declared in the main index.
303316
w.p.allPkgs[pkg] = true
304317

305-
w.string(pkg.Path())
318+
w.string(w.exportPath(pkg))
306319
}
307320

308321
func (w *exportWriter) qualifiedIdent(obj types.Object) {

go/internal/gcimporter/iimport.go

+11-17
Original file line numberDiff line numberDiff line change
@@ -147,24 +147,14 @@ func IImportData(fset *token.FileSet, imports map[string]*types.Package, data []
147147
errorf("no packages found for %s", path)
148148
panic("unreachable")
149149
}
150-
var localpkg *types.Package
151-
for _, pkg := range pkgList {
152-
if pkg.Path() == path {
153-
localpkg = pkg
154-
break
155-
}
156-
}
157-
if localpkg == nil {
158-
localpkg = pkgList[0]
159-
}
160-
161-
names := make([]string, 0, len(p.pkgIndex[localpkg]))
162-
for name := range p.pkgIndex[localpkg] {
150+
p.ipkg = pkgList[0]
151+
names := make([]string, 0, len(p.pkgIndex[p.ipkg]))
152+
for name := range p.pkgIndex[p.ipkg] {
163153
names = append(names, name)
164154
}
165155
sort.Strings(names)
166156
for _, name := range names {
167-
p.doDecl(localpkg, name)
157+
p.doDecl(p.ipkg, name)
168158
}
169159

170160
for _, typ := range p.interfaceList {
@@ -174,17 +164,18 @@ func IImportData(fset *token.FileSet, imports map[string]*types.Package, data []
174164
// record all referenced packages as imports
175165
list := append(([]*types.Package)(nil), pkgList[1:]...)
176166
sort.Sort(byPath(list))
177-
localpkg.SetImports(list)
167+
p.ipkg.SetImports(list)
178168

179169
// package was imported completely and without errors
180-
localpkg.MarkComplete()
170+
p.ipkg.MarkComplete()
181171

182172
consumed, _ := r.Seek(0, io.SeekCurrent)
183-
return int(consumed), localpkg, nil
173+
return int(consumed), p.ipkg, nil
184174
}
185175

186176
type iimporter struct {
187177
ipath string
178+
ipkg *types.Package
188179
version int
189180

190181
stringData []byte
@@ -236,6 +227,9 @@ func (p *iimporter) pkgAt(off uint64) *types.Package {
236227
return pkg
237228
}
238229
path := p.stringAt(off)
230+
if path == p.ipath {
231+
return p.ipkg
232+
}
239233
errorf("missing package %q in %q", path, p.ipath)
240234
return nil
241235
}

0 commit comments

Comments
 (0)