Skip to content

Commit

Permalink
go/types, types2: ensure that the reportCycle has a deterministic o…
Browse files Browse the repository at this point in the history
…utput

Fixes golang#71254
  • Loading branch information
wingrez committed Jan 14, 2025
1 parent 6da1601 commit eb16b9b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
17 changes: 13 additions & 4 deletions src/cmd/compile/internal/types2/initorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"container/heap"
"fmt"
. "internal/types/errors"
"maps"
"slices"
)

Expand Down Expand Up @@ -139,10 +140,18 @@ func findPath(objMap map[Object]*declInfo, from, to Object, seen map[Object]bool
}
seen[from] = true

for d := range objMap[from].deps {
if d == to {
return []Object{d}
}
if _, found := objMap[from].deps[to]; found {
return []Object{to}
}

// findPath is only called in cases where a cycle is indicated,
// so performance is not critical.
// Sort `deps` here in order to return a deterministic path,
// thereby guaranteeing consistent output in the `reportCycle`.
deps := slices.SortedFunc(maps.Keys(objMap[from].deps), func(x Object, y Object) int {
return cmp.Compare(x.order(), y.order())
})
for _, d := range deps {
if P := findPath(objMap, d, to, seen); P != nil {
return append(P, d)
}
Expand Down
17 changes: 13 additions & 4 deletions src/go/types/initorder.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eb16b9b

Please sign in to comment.