Skip to content

Commit

Permalink
go/internal/gcimporter: restore Go 1.19 Package.SetImports behavior
Browse files Browse the repository at this point in the history
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 golang#54096.
Updates golang#58296.

Change-Id: I70d252a19cada3333ed59b16d1df2abc5a4cff73
Reviewed-on: https://go-review.googlesource.com/c/go/+/467896
Reviewed-by: Alan Donovan <adonovan@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
mdempsky committed Feb 13, 2023
1 parent 93f10b8 commit 0cd309e
Showing 1 changed file with 11 additions and 36 deletions.
47 changes: 11 additions & 36 deletions src/go/internal/gcimporter/ureader.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"go/token"
"go/types"
"internal/pkgbits"
"sort"
)

// A pkgReader holds the shared state for reading a unified IR package
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0cd309e

Please sign in to comment.