Skip to content

Commit 004fb5c

Browse files
cuonglmmdempsky
authored andcommitted
cmd/compile: fix isStaticCompositeLiteral reports wrong for struct field
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 <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 689ee11 commit 004fb5c

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

src/cmd/compile/internal/gc/sinit.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,6 @@ func getdyn(n *Node, top bool) initGenType {
650650
// isStaticCompositeLiteral reports whether n is a compile-time constant.
651651
func isStaticCompositeLiteral(n *Node) bool {
652652
switch n.Op {
653-
case ONAME:
654-
return n.Class() == PEXTERN && n.Name != nil && n.Name.Readonly()
655653
case OSLICELIT:
656654
return false
657655
case OARRAYLIT:

test/fixedbugs/issue31782.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// run
2+
3+
// Copyright 2019 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+
// Check static composite literal reports wrong for struct
8+
// field.
9+
10+
package main
11+
12+
type one struct {
13+
i interface{}
14+
}
15+
16+
type two struct {
17+
i interface{}
18+
s []string
19+
}
20+
21+
func main() {
22+
o := one{i: two{i: 42}.i}
23+
println(o.i.(int))
24+
}

test/fixedbugs/issue31782.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
42

0 commit comments

Comments
 (0)