]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix isStaticCompositeLiteral reports wrong for struct field
authorLE Manh Cuong <cuong.manhle.vn@gmail.com>
Wed, 1 May 2019 20:57:29 +0000 (03:57 +0700)
committerMatthew Dempsky <mdempsky@google.com>
Fri, 3 May 2019 03:49:11 +0000 (03:49 +0000)
golang.org/cl/174498 add ONAME case to isStaticCompositeLiteral, to
detect global variable as compile-time constant.

It does report wrong for struct field, e.g:

o := one{i: two{i: 42}.i}

field i in two{i: 42} was reported as static composite literal, while it
should not.

In general, adding ONAME case for isStaticCompositeLiteral is probably
wrong.

Fixes #31782

Change-Id: Icde7d43bbb002b75df5c52b948b7126a4265e07b
Reviewed-on: https://go-review.googlesource.com/c/go/+/174837
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/compile/internal/gc/sinit.go
test/fixedbugs/issue31782.go [new file with mode: 0644]
test/fixedbugs/issue31782.out [new file with mode: 0644]

index 92cf51e345298dcf3d91b048f533b968fa3c1470..aa0c06c5642a10b947bc316d44c9e1f90b19b941 100644 (file)
@@ -650,8 +650,6 @@ func getdyn(n *Node, top bool) initGenType {
 // isStaticCompositeLiteral reports whether n is a compile-time constant.
 func isStaticCompositeLiteral(n *Node) bool {
        switch n.Op {
-       case ONAME:
-               return n.Class() == PEXTERN && n.Name != nil && n.Name.Readonly()
        case OSLICELIT:
                return false
        case OARRAYLIT:
diff --git a/test/fixedbugs/issue31782.go b/test/fixedbugs/issue31782.go
new file mode 100644 (file)
index 0000000..a42001e
--- /dev/null
@@ -0,0 +1,24 @@
+// 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.
+
+// Check static composite literal reports wrong for struct
+// field.
+
+package main
+
+type one struct {
+       i interface{}
+}
+
+type two struct {
+       i interface{}
+       s []string
+}
+
+func main() {
+       o := one{i: two{i: 42}.i}
+       println(o.i.(int))
+}
diff --git a/test/fixedbugs/issue31782.out b/test/fixedbugs/issue31782.out
new file mode 100644 (file)
index 0000000..d81cc07
--- /dev/null
@@ -0,0 +1 @@
+42