|
8 | 8 | "go/token"
|
9 | 9 | "go/types"
|
10 | 10 | "internal/pkgbits"
|
| 11 | + "sort" |
11 | 12 | )
|
12 | 13 |
|
13 | 14 | // 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
|
83 | 84 | iface.Complete()
|
84 | 85 | }
|
85 | 86 |
|
| 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 | + |
86 | 97 | pkg.MarkComplete()
|
87 | 98 | return pkg
|
88 | 99 | }
|
@@ -222,45 +233,9 @@ func (r *reader) doPkg() *types.Package {
|
222 | 233 | pkg := types.NewPackage(path, name)
|
223 | 234 | r.p.imports[path] = pkg
|
224 | 235 |
|
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 |
| - |
237 | 236 | return pkg
|
238 | 237 | }
|
239 | 238 |
|
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 |
| - |
264 | 239 | // @@@ Types
|
265 | 240 |
|
266 | 241 | func (r *reader) typ() types.Type {
|
|
0 commit comments