diff options
-rw-r--r-- | src/cmd/compile/internal/types2/initorder.go | 8 | ||||
-rw-r--r-- | src/go/types/initorder.go | 8 | ||||
-rw-r--r-- | test/fixedbugs/issue66575.go | 31 | ||||
-rw-r--r-- | test/fixedbugs/issue66575.out | 2 |
4 files changed, 49 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/types2/initorder.go b/src/cmd/compile/internal/types2/initorder.go index 0d28495542..86cb9036c4 100644 --- a/src/cmd/compile/internal/types2/initorder.go +++ b/src/cmd/compile/internal/types2/initorder.go @@ -309,6 +309,14 @@ func (a nodeQueue) Swap(i, j int) { func (a nodeQueue) Less(i, j int) bool { x, y := a[i], a[j] + + // Prioritize all constants before non-constants. See go.dev/issue/66575/. + _, xConst := x.obj.(*Const) + _, yConst := y.obj.(*Const) + if xConst != yConst { + return xConst + } + // nodes are prioritized by number of incoming dependencies (1st key) // and source order (2nd key) return x.ndeps < y.ndeps || x.ndeps == y.ndeps && x.obj.order() < y.obj.order() diff --git a/src/go/types/initorder.go b/src/go/types/initorder.go index 70d2a713bd..d238440279 100644 --- a/src/go/types/initorder.go +++ b/src/go/types/initorder.go @@ -311,6 +311,14 @@ func (a nodeQueue) Swap(i, j int) { func (a nodeQueue) Less(i, j int) bool { x, y := a[i], a[j] + + // Prioritize all constants before non-constants. See go.dev/issue/66575/. + _, xConst := x.obj.(*Const) + _, yConst := y.obj.(*Const) + if xConst != yConst { + return xConst + } + // nodes are prioritized by number of incoming dependencies (1st key) // and source order (2nd key) return x.ndeps < y.ndeps || x.ndeps == y.ndeps && x.obj.order() < y.obj.order() diff --git a/test/fixedbugs/issue66575.go b/test/fixedbugs/issue66575.go new file mode 100644 index 0000000000..1ad6ca0b70 --- /dev/null +++ b/test/fixedbugs/issue66575.go @@ -0,0 +1,31 @@ +// run + +// Copyright 2024 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +var ( + v0 = initv0() + v1 = initv1() +) + +const c = "c" + +func initv0() string { + println("initv0") + if c != "" { // have a dependency on c + return "" + } + return "" +} + +func initv1() string { + println("initv1") + return "" +} + +func main() { + // do nothing +} diff --git a/test/fixedbugs/issue66575.out b/test/fixedbugs/issue66575.out new file mode 100644 index 0000000000..36d1f17a68 --- /dev/null +++ b/test/fixedbugs/issue66575.out @@ -0,0 +1,2 @@ +initv0 +initv1 |