Skip to content

Commit aaf40f8

Browse files
cuonglmmdempsky
authored andcommitted
cmd/compile: fix maplit init panics for dynamic entry
golang.org/cl/174498 removes dynamic map entry handling in maplit, by filtering the static entry only. It panics if it see a dynamic entry. It relies on order to remove all dynamic entries. But after recursively call order on the statics, some static entries become dynamic, e.g OCONVIFACE node: type i interface { j() } type s struct{} func (s) j() {} type foo map[string]i var f = foo{ "1": s{}, } To fix it, we recursively call order on each static entry, if it changed to dynamic, put entry to dynamic then. Fixes #31777 Change-Id: I1004190ac8f2d1eaa4beb6beab989db74099b025 Reviewed-on: https://go-review.googlesource.com/c/go/+/174777 Run-TryBot: Matthew Dempsky <[email protected]> Reviewed-by: Keith Randall <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 40a6d0e commit aaf40f8

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,18 +1262,24 @@ func (o *Order) expr(n, lhs *Node) *Node {
12621262
if r.Op != OKEY {
12631263
Fatalf("OMAPLIT entry not OKEY: %v\n", r)
12641264
}
1265-
if isStaticCompositeLiteral(r.Left) && isStaticCompositeLiteral(r.Right) {
1266-
statics = append(statics, r)
1267-
} else {
1265+
1266+
if !isStaticCompositeLiteral(r.Left) || !isStaticCompositeLiteral(r.Right) {
1267+
dynamics = append(dynamics, r)
1268+
continue
1269+
}
1270+
1271+
// Recursively ordering some static entries can change them to dynamic;
1272+
// e.g., OCONVIFACE nodes. See #31777.
1273+
r = o.expr(r, nil)
1274+
if !isStaticCompositeLiteral(r.Left) || !isStaticCompositeLiteral(r.Right) {
12681275
dynamics = append(dynamics, r)
1276+
continue
12691277
}
1278+
1279+
statics = append(statics, r)
12701280
}
12711281
n.List.Set(statics)
12721282

1273-
// Note: we don't need to recursively call order on the statics.
1274-
// But do it anyway, just in case that's not true in the future.
1275-
o.exprList(n.List)
1276-
12771283
if len(dynamics) == 0 {
12781284
break
12791285
}

test/fixedbugs/issue31777.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// compile
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+
// Compile with static map literal.
8+
9+
package p
10+
11+
type i interface {
12+
j()
13+
}
14+
15+
type s struct{}
16+
17+
func (s) j() {}
18+
19+
type foo map[string]i
20+
21+
var f = foo{
22+
"1": s{},
23+
"2": s{},
24+
}

0 commit comments

Comments
 (0)