]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix static init of literal contains dynamic exprs
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Wed, 4 May 2022 11:39:26 +0000 (18:39 +0700)
committerCuong Manh Le <cuong.manhle.vn@gmail.com>
Thu, 5 May 2022 01:33:11 +0000 (01:33 +0000)
Fixes #52673

Change-Id: Ib2faa5a669c05778fc6beb38c3e63d558af9b2be
Reviewed-on: https://go-review.googlesource.com/c/go/+/403995
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: David Chase <drchase@google.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>

src/cmd/compile/internal/walk/complit.go
test/fixedbugs/issue52673.go [new file with mode: 0644]

index df6d811d6242ce2cb5301fc6ed960d4ffbd55cd4..748a53df33c0eb1ab597bbeb924f3fa0269c525a 100644 (file)
@@ -235,7 +235,16 @@ func fixedlit(ctxt initContext, kind initKind, n *ir.CompLitExpr, var_ ir.Node,
                case ir.OSLICELIT:
                        value := value.(*ir.CompLitExpr)
                        if (kind == initKindStatic && ctxt == inNonInitFunction) || (kind == initKindDynamic && ctxt == inInitFunction) {
-                               slicelit(ctxt, value, a, init)
+                               var sinit ir.Nodes
+                               slicelit(ctxt, value, a, &sinit)
+                               if kind == initKindStatic {
+                                       // When doing static initialization, init statements may contain dynamic
+                                       // expression, which will be initialized later, causing liveness analysis
+                                       // confuses about variables lifetime. So making sure those expressions
+                                       // are ordered correctly here. See issue #52673.
+                                       orderBlock(&sinit, map[string][]*ir.Name{})
+                               }
+                               init.Append(sinit...)
                                continue
                        }
 
diff --git a/test/fixedbugs/issue52673.go b/test/fixedbugs/issue52673.go
new file mode 100644 (file)
index 0000000..1540363
--- /dev/null
@@ -0,0 +1,16 @@
+// compile
+
+// Copyright 2022 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
+
+func f() {
+       var x string
+       func() [10][]bool {
+               return [10][]bool{
+                       []bool{bool(x < "")},
+                       []bool{}, []bool{}, []bool{}, []bool{}}
+       }()
+}