Skip to content

Commit 0cd309e

Browse files
committed
go/internal/gcimporter: restore Go 1.19 Package.SetImports behavior
This CL is a port of go.dev/cl/465936 from the x/tools importer, which changes the unified importer to (1) only call Package.SetImports on the main package being imported (not any transitively imported packages), and (2) to only populate it with any packages that were referenced by the exported API. With these changes, it should behave identically to how the indexed importer worked in Go 1.19. It will also allow eventually dropping the serialized import DAG from the export data format, which should help with export data file sizes somewhat. Updates #54096. Updates #58296. Change-Id: I70d252a19cada3333ed59b16d1df2abc5a4cff73 Reviewed-on: https://go-review.googlesource.com/c/go/+/467896 Reviewed-by: Alan Donovan <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 93f10b8 commit 0cd309e

File tree

1 file changed

+11
-36
lines changed

1 file changed

+11
-36
lines changed

src/go/internal/gcimporter/ureader.go

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"go/token"
99
"go/types"
1010
"internal/pkgbits"
11+
"sort"
1112
)
1213

1314
// A pkgReader holds the shared state for reading a unified IR package
@@ -83,6 +84,16 @@ func readUnifiedPackage(fset *token.FileSet, ctxt *types.Context, imports map[st
8384
iface.Complete()
8485
}
8586

87+
// Imports() of pkg are all of the transitive packages that were loaded.
88+
var imps []*types.Package
89+
for _, imp := range pr.pkgs {
90+
if imp != nil && imp != pkg {
91+
imps = append(imps, imp)
92+
}
93+
}
94+
sort.Sort(byPath(imps))
95+
pkg.SetImports(imps)
96+
8697
pkg.MarkComplete()
8798
return pkg
8899
}
@@ -222,45 +233,9 @@ func (r *reader) doPkg() *types.Package {
222233
pkg := types.NewPackage(path, name)
223234
r.p.imports[path] = pkg
224235

225-
imports := make([]*types.Package, r.Len())
226-
for i := range imports {
227-
imports[i] = r.pkg()
228-
}
229-
230-
// The documentation for (*types.Package).Imports requires
231-
// flattening the import graph when reading from export data, as
232-
// obviously incorrect as that is.
233-
//
234-
// TODO(mdempsky): Remove this if go.dev/issue/54096 is accepted.
235-
pkg.SetImports(flattenImports(imports))
236-
237236
return pkg
238237
}
239238

240-
// flattenImports returns the transitive closure of all imported
241-
// packages rooted from pkgs.
242-
func flattenImports(pkgs []*types.Package) []*types.Package {
243-
var res []*types.Package
244-
seen := make(map[*types.Package]struct{})
245-
for _, pkg := range pkgs {
246-
if _, ok := seen[pkg]; ok {
247-
continue
248-
}
249-
seen[pkg] = struct{}{}
250-
res = append(res, pkg)
251-
252-
// pkg.Imports() is already flattened.
253-
for _, pkg := range pkg.Imports() {
254-
if _, ok := seen[pkg]; ok {
255-
continue
256-
}
257-
seen[pkg] = struct{}{}
258-
res = append(res, pkg)
259-
}
260-
}
261-
return res
262-
}
263-
264239
// @@@ Types
265240

266241
func (r *reader) typ() types.Type {

0 commit comments

Comments
 (0)