]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.22] cmd/compile: put constants before variables in initialization...
authorKeith Randall <khr@golang.org>
Thu, 28 Mar 2024 15:47:52 +0000 (08:47 -0700)
committerJoedian Reid <joedian@google.com>
Mon, 24 Jun 2024 17:14:34 +0000 (17:14 +0000)
Fixes #67820.

Change-Id: I03f4d4577b88ad0a92b260b2efd0cb9fe5082b2f
Reviewed-on: https://go-review.googlesource.com/c/go/+/575075
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/590395
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
src/cmd/compile/internal/types2/initorder.go
src/go/types/initorder.go
test/fixedbugs/issue66575.go [new file with mode: 0644]
test/fixedbugs/issue66575.out [new file with mode: 0644]

index 6e041721e86ed6e6e019297e9411e19c7fcac5af..841b725b17160a9dfad143814102c958b838deb8 100644 (file)
@@ -310,6 +310,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()
index 9ee176fbdb5e9d83c4a6fc080fc5dc6dbc491274..a8d8f26b2242ef8e3a92f771a74b0b3c16c4e8d7 100644 (file)
@@ -307,6 +307,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 (file)
index 0000000..1ad6ca0
--- /dev/null
@@ -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 (file)
index 0000000..36d1f17
--- /dev/null
@@ -0,0 +1,2 @@
+initv0
+initv1