mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/compile: fix package initialization ordering
This CL rewrites cmd/compile's package-level initialization ordering algorithm to be compliant with the Go spec. See documentation in initorder.go for details. Incidentally, this CL also improves fidelity of initialization loop diagnostics by including referenced functions in the emitted output like go/types does. Fixes #22326. Change-Id: I7c9ac47ff563df4d4f700cf6195387a0f372cc7b Reviewed-on: https://go-review.googlesource.com/c/go/+/170062 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
e883d000f4
commit
5d0d87ae16
6 changed files with 439 additions and 238 deletions
|
|
@ -12,6 +12,7 @@ import (
|
|||
"cmd/compile/internal/types"
|
||||
"cmd/internal/obj"
|
||||
"cmd/internal/src"
|
||||
"sort"
|
||||
)
|
||||
|
||||
// A Node is a single node in the syntax tree.
|
||||
|
|
@ -970,3 +971,30 @@ func (q *nodeQueue) popLeft() *Node {
|
|||
q.head++
|
||||
return n
|
||||
}
|
||||
|
||||
// NodeSet is a set of Nodes.
|
||||
type NodeSet map[*Node]struct{}
|
||||
|
||||
// Has reports whether s contains n.
|
||||
func (s NodeSet) Has(n *Node) bool {
|
||||
_, isPresent := s[n]
|
||||
return isPresent
|
||||
}
|
||||
|
||||
// Add adds n to s.
|
||||
func (s *NodeSet) Add(n *Node) {
|
||||
if *s == nil {
|
||||
*s = make(map[*Node]struct{})
|
||||
}
|
||||
(*s)[n] = struct{}{}
|
||||
}
|
||||
|
||||
// Sorted returns s sorted according to less.
|
||||
func (s NodeSet) Sorted(less func(*Node, *Node) bool) []*Node {
|
||||
var res []*Node
|
||||
for n := range s {
|
||||
res = append(res, n)
|
||||
}
|
||||
sort.Slice(res, func(i, j int) bool { return less(res[i], res[j]) })
|
||||
return res
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue