]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix ICE when transforming loopvar
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Thu, 22 May 2025 11:06:27 +0000 (18:06 +0700)
committerGopher Robot <gobot@golang.org>
Thu, 22 May 2025 16:51:56 +0000 (09:51 -0700)
When transforming for loop variables, the compiler does roughly
following steps:

(1) prebody = {z := z' for z in leaked}
        ...
        (4) init' = (init : s/z/z' for z in leaked)

However, the definition of z is not updated to `z := z'` statement,
causing ReassignOracle incorrectly use the new init statement with z'
instead of z, trigger the ICE.

Fixing this by updating the correct/new definition statement for z
during the prebody initialization.

Fixes #73823

Change-Id: Ice2a6741be7478506c58f4000f591d5582029136
Reviewed-on: https://go-review.googlesource.com/c/go/+/675475
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: David Chase <drchase@google.com>
src/cmd/compile/internal/loopvar/loopvar.go
test/fixedbugs/issue73823.go [new file with mode: 0644]

index 030fc04c1369d8da7c1e946dfcc64e709dca2a3d..5a4590d2998d0c9380d5dc044f077b736c340887 100644 (file)
@@ -305,6 +305,7 @@ func ForCapture(fn *ir.Func) []VarAndLoop {
                                                as := ir.NewAssignStmt(x.Pos(), z, tz)
                                                as.Def = true
                                                as.SetTypecheck(1)
+                                               z.Defn = as
                                                preBody.Append(as)
                                                dclFixups[z] = as
 
diff --git a/test/fixedbugs/issue73823.go b/test/fixedbugs/issue73823.go
new file mode 100644 (file)
index 0000000..2f66266
--- /dev/null
@@ -0,0 +1,58 @@
+// compile
+
+// Copyright 2025 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 p
+
+type Backend interface {
+       Hash(ignores func(bucketName, keyName []byte) bool) (uint32, error)
+}
+
+type backend struct {
+}
+
+func first() (key []byte, value []byte) {
+       return
+}
+
+func (b *backend) View(fn func() error) error {
+       return nil
+}
+
+func (b *backend) Hash(ignores func(bucketName, keyName []byte) bool) (uint32, error) {
+       err := b.View(func() error {
+               for next, _ := first(); next != nil; next, _ = first() {
+                       _ = next
+               }
+               return nil
+       })
+       return 0, err
+}
+
+func defragdb() error {
+       for next, _ := first(); next != nil; next, _ = first() {
+               _ = f(next)
+               ForEach(func(k, v []byte) error {
+                       _ = next
+                       return nil
+               })
+       }
+
+       return nil
+}
+
+func ForEach(fn func(k, v []byte) error) error {
+       for k, v := first(); k != nil; k, v = first() {
+               if err := fn(k, v); err != nil {
+                       return err
+               }
+       }
+       return nil
+}
+
+//go:noinline
+func f(any) string {
+       return ""
+}