mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/compile: make sure to initialize static entries of slices
If a slice's entries are sparse, we decide to initialize it dynamically instead of statically. That's CL 151319. But if we do initialize it dynamically, we still need to initialize the static entries. Typically we do that, but the bug fixed here is that we don't if the entry's value is itself an array or struct. To fix, use initKindLocalCode to ensure that both static and dynamic entries are initialized via code. Fixes #31987 Change-Id: I1192ffdbfb5cd50445c1206c4a3d8253295201dd Reviewed-on: https://go-review.googlesource.com/c/go/+/176904 Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
This commit is contained in:
parent
2637f1f950
commit
a9e107c85c
2 changed files with 36 additions and 1 deletions
|
|
@ -561,6 +561,13 @@ const (
|
|||
inNonInitFunction
|
||||
)
|
||||
|
||||
func (c initContext) String() string {
|
||||
if c == inInitFunction {
|
||||
return "inInitFunction"
|
||||
}
|
||||
return "inNonInitFunction"
|
||||
}
|
||||
|
||||
// from here down is the walk analysis
|
||||
// of composite literals.
|
||||
// most of the work is to generate
|
||||
|
|
@ -920,7 +927,13 @@ func slicelit(ctxt initContext, n *Node, var_ *Node, init *Nodes) {
|
|||
break
|
||||
|
||||
case OARRAYLIT, OSTRUCTLIT:
|
||||
fixedlit(ctxt, initKindDynamic, value, a, init)
|
||||
k := initKindDynamic
|
||||
if vstat == nil {
|
||||
// Generate both static and dynamic initializations.
|
||||
// See issue #31987.
|
||||
k = initKindLocalCode
|
||||
}
|
||||
fixedlit(ctxt, k, value, a, init)
|
||||
continue
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue