Skip to content

Commit 0bd7408

Browse files
committed
cmd/compile: fix static init of literal contains dynamic exprs
Fixes #52673 Change-Id: Ib2faa5a669c05778fc6beb38c3e63d558af9b2be Reviewed-on: https://go-review.googlesource.com/c/go/+/403995 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Reviewed-by: David Chase <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]>
1 parent ed462a6 commit 0bd7408

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/cmd/compile/internal/walk/complit.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,16 @@ func fixedlit(ctxt initContext, kind initKind, n *ir.CompLitExpr, var_ ir.Node,
235235
case ir.OSLICELIT:
236236
value := value.(*ir.CompLitExpr)
237237
if (kind == initKindStatic && ctxt == inNonInitFunction) || (kind == initKindDynamic && ctxt == inInitFunction) {
238-
slicelit(ctxt, value, a, init)
238+
var sinit ir.Nodes
239+
slicelit(ctxt, value, a, &sinit)
240+
if kind == initKindStatic {
241+
// When doing static initialization, init statements may contain dynamic
242+
// expression, which will be initialized later, causing liveness analysis
243+
// confuses about variables lifetime. So making sure those expressions
244+
// are ordered correctly here. See issue #52673.
245+
orderBlock(&sinit, map[string][]*ir.Name{})
246+
}
247+
init.Append(sinit...)
239248
continue
240249
}
241250

test/fixedbugs/issue52673.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// compile
2+
3+
// Copyright 2022 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package p
8+
9+
func f() {
10+
var x string
11+
func() [10][]bool {
12+
return [10][]bool{
13+
[]bool{bool(x < "")},
14+
[]bool{}, []bool{}, []bool{}, []bool{}}
15+
}()
16+
}

0 commit comments

Comments
 (0)