]> Cypherpunks repositories - gostls13.git/commitdiff
go/internal/gcimporter: restore Go 1.19 Package.SetImports behavior
authorMatthew Dempsky <mdempsky@google.com>
Mon, 13 Feb 2023 22:40:01 +0000 (14:40 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Mon, 13 Feb 2023 23:55:24 +0000 (23:55 +0000)
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 <adonovan@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/go/internal/gcimporter/ureader.go

index ffd8402202e79c264279c4e1f7b399fc6c350709..ac85a415b1d0fe9e8d7822d5a86049acbf6c9fbe 100644 (file)
@@ -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 {