From 0cd309e12818f988693bf8e4d9f1453331dcf9f2 Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Mon, 13 Feb 2023 14:40:01 -0800 Subject: [PATCH] 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 Run-TryBot: Matthew Dempsky TryBot-Result: Gopher Robot --- src/go/internal/gcimporter/ureader.go | 47 +++++++-------------------- 1 file changed, 11 insertions(+), 36 deletions(-) diff --git a/src/go/internal/gcimporter/ureader.go b/src/go/internal/gcimporter/ureader.go index ffd8402202..ac85a415b1 100644 --- a/src/go/internal/gcimporter/ureader.go +++ b/src/go/internal/gcimporter/ureader.go @@ -8,6 +8,7 @@ import ( "go/token" "go/types" "internal/pkgbits" + "sort" ) // 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 iface.Complete() } + // Imports() of pkg are all of the transitive packages that were loaded. + var imps []*types.Package + for _, imp := range pr.pkgs { + if imp != nil && imp != pkg { + imps = append(imps, imp) + } + } + sort.Sort(byPath(imps)) + pkg.SetImports(imps) + pkg.MarkComplete() return pkg } @@ -222,45 +233,9 @@ func (r *reader) doPkg() *types.Package { pkg := types.NewPackage(path, name) r.p.imports[path] = pkg - imports := make([]*types.Package, r.Len()) - for i := range imports { - imports[i] = r.pkg() - } - - // The documentation for (*types.Package).Imports requires - // flattening the import graph when reading from export data, as - // obviously incorrect as that is. - // - // TODO(mdempsky): Remove this if go.dev/issue/54096 is accepted. - pkg.SetImports(flattenImports(imports)) - return pkg } -// flattenImports returns the transitive closure of all imported -// packages rooted from pkgs. -func flattenImports(pkgs []*types.Package) []*types.Package { - var res []*types.Package - seen := make(map[*types.Package]struct{}) - for _, pkg := range pkgs { - if _, ok := seen[pkg]; ok { - continue - } - seen[pkg] = struct{}{} - res = append(res, pkg) - - // pkg.Imports() is already flattened. - for _, pkg := range pkg.Imports() { - if _, ok := seen[pkg]; ok { - continue - } - seen[pkg] = struct{}{} - res = append(res, pkg) - } - } - return res -} - // @@@ Types func (r *reader) typ() types.Type { -- 2.50.0