Skip to content

Commit ec630ca

Browse files
committed
go/types, types2: ensure deterministic output when reporting an init cycle
Fixes #71254
1 parent 6da1601 commit ec630ca

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,21 @@ func findPath(objMap map[Object]*declInfo, from, to Object, seen map[Object]bool
139139
}
140140
seen[from] = true
141141

142+
if objMap[from].deps[to] {
143+
return []Object{to}
144+
}
145+
146+
// sort deps for deterministic result.
147+
// Performance is never critical when we have an error, leave away.
148+
var deps []Object
142149
for d := range objMap[from].deps {
143-
if d == to {
144-
return []Object{d}
145-
}
150+
deps = append(deps, d)
151+
}
152+
slices.SortFunc(deps, func(x, y Object) int {
153+
return cmp.Compare(x.order(), y.order())
154+
})
155+
156+
for _, d := range deps {
146157
if P := findPath(objMap, d, to, seen); P != nil {
147158
return append(P, d)
148159
}

src/go/types/initorder.go

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2025 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package p
6+
7+
const (
8+
B /* ERROR "initialization cycle: B refers to itself" */ = A + B
9+
A /* ERROR "initialization cycle: A refers to itself" */ = A + B
10+
11+
C /* ERRORx "initialization cycle for C\\s+.*C refers to D\\s+.*D refers to C" */ = E + D
12+
D /* ERRORx "initialization cycle for D\\s+.*D refers to C\\s+.*C refers to D" */ = E + C
13+
E = D + C
14+
)

0 commit comments

Comments
 (0)