Skip to content

Commit 04de0fb

Browse files
committed
go/types, types2: ensure that the reportCycle has a deterministic output
Fixes #71254
1 parent 6da1601 commit 04de0fb

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

src/cmd/compile/internal/types2/initorder.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"container/heap"
1010
"fmt"
1111
. "internal/types/errors"
12+
"maps"
1213
"slices"
1314
)
1415

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

142-
for d := range objMap[from].deps {
143-
if d == to {
144-
return []Object{d}
145-
}
143+
if _, found := objMap[from].deps[to]; found {
144+
return []Object{to}
145+
}
146+
147+
// findPath is only called in cases where a cycle is indicated,
148+
// so performance is not critical.
149+
// Sort `deps` here in order to return a deterministic path,
150+
// thereby guaranteeing consistent output in the `reportCycle`.
151+
deps := slices.SortedFunc(maps.Keys(objMap[from].deps), func(x, y Object) int {
152+
return cmp.Compare(x.order(), y.order())
153+
})
154+
for _, d := range deps {
146155
if P := findPath(objMap, d, to, seen); P != nil {
147156
return append(P, d)
148157
}

src/go/types/initorder.go

Lines changed: 13 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)