Skip to content

Commit 9b804a7

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

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
. "internal/types/errors"
1212
"slices"
13+
"sort"
1314
)
1415

1516
// initOrder computes the Info.InitOrder for package variables.
@@ -139,7 +140,16 @@ func findPath(objMap map[Object]*declInfo, from, to Object, seen map[Object]bool
139140
}
140141
seen[from] = true
141142

143+
// sort deps for deterministic result
144+
var deps []Object
142145
for d := range objMap[from].deps {
146+
deps = append(deps, d)
147+
}
148+
sort.Slice(deps, func(i, j int) bool {
149+
return deps[i].order() < deps[j].order()
150+
})
151+
152+
for _, d := range deps {
143153
if d == to {
144154
return []Object{d}
145155
}
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 /* ERRORx "initialization cycle for A\\s+.*A refers to B\\s+.*B refers to A" */ = 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+
)

src/go/types/initorder.go

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

0 commit comments

Comments
 (0)