aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeith Randall <khr@google.com>2019-05-13 13:43:49 -0700
committerFilippo Valsorda <filippo@golang.org>2019-05-14 21:16:32 +0000
commitecf7695c7dcfc04becde0beec0e1c5f583b42e9e (patch)
treecfcb73d6ab344e04b53749deae6369696c9d1cfc
parentcdb776529ea7a4beac4c8a4d6a9ce149045bd6b5 (diff)
downloadgo-ecf7695c7dcfc04becde0beec0e1c5f583b42e9e.tar.gz
go-ecf7695c7dcfc04becde0beec0e1c5f583b42e9e.zip
[release-branch.go1.12] 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 #32013 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> (cherry picked from commit a9e107c85cf69d735ac81c29f4a354643e40b2b5) Reviewed-on: https://go-review.googlesource.com/c/go/+/177040 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
-rw-r--r--src/cmd/compile/internal/gc/sinit.go15
-rw-r--r--test/fixedbugs/issue31987.go22
2 files changed, 36 insertions, 1 deletions
diff --git a/src/cmd/compile/internal/gc/sinit.go b/src/cmd/compile/internal/gc/sinit.go
index de0298b746..92b1e0f88d 100644
--- a/src/cmd/compile/internal/gc/sinit.go
+++ b/src/cmd/compile/internal/gc/sinit.go
@@ -555,6 +555,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
@@ -913,7 +920,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
}
diff --git a/test/fixedbugs/issue31987.go b/test/fixedbugs/issue31987.go
new file mode 100644
index 0000000000..372289b52d
--- /dev/null
+++ b/test/fixedbugs/issue31987.go
@@ -0,0 +1,22 @@
+// run
+
+// Copyright 2019 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
+
+import "fmt"
+
+type container struct {
+ Value string
+}
+
+func main() {
+ s := []container{
+ 7: {Value: "string value"},
+ }
+ if s[7].Value != "string value" {
+ panic(fmt.Errorf("wanted \"string value\", got \"%s\"", s[7].Value))
+ }
+}