From aaf40f8f98ded15d0a039470a552f58b8b71abac Mon Sep 17 00:00:00 2001 From: LE Manh Cuong Date: Wed, 1 May 2019 21:57:52 +0700 Subject: [PATCH] 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 Reviewed-by: Keith Randall TryBot-Result: Gobot Gobot --- src/cmd/compile/internal/gc/order.go | 20 +++++++++++++------- test/fixedbugs/issue31777.go | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 test/fixedbugs/issue31777.go diff --git a/src/cmd/compile/internal/gc/order.go b/src/cmd/compile/internal/gc/order.go index 54e4a15681..64716d8402 100644 --- a/src/cmd/compile/internal/gc/order.go +++ b/src/cmd/compile/internal/gc/order.go @@ -1262,18 +1262,24 @@ func (o *Order) expr(n, lhs *Node) *Node { if r.Op != OKEY { Fatalf("OMAPLIT entry not OKEY: %v\n", r) } - if isStaticCompositeLiteral(r.Left) && isStaticCompositeLiteral(r.Right) { - statics = append(statics, r) - } else { + + if !isStaticCompositeLiteral(r.Left) || !isStaticCompositeLiteral(r.Right) { dynamics = append(dynamics, r) + continue } + + // Recursively ordering some static entries can change them to dynamic; + // e.g., OCONVIFACE nodes. See #31777. + r = o.expr(r, nil) + if !isStaticCompositeLiteral(r.Left) || !isStaticCompositeLiteral(r.Right) { + dynamics = append(dynamics, r) + continue + } + + statics = append(statics, r) } n.List.Set(statics) - // Note: we don't need to recursively call order on the statics. - // But do it anyway, just in case that's not true in the future. - o.exprList(n.List) - if len(dynamics) == 0 { break } diff --git a/test/fixedbugs/issue31777.go b/test/fixedbugs/issue31777.go new file mode 100644 index 0000000000..839e242c95 --- /dev/null +++ b/test/fixedbugs/issue31777.go @@ -0,0 +1,24 @@ +// compile + +// 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. + +// Compile with static map literal. + +package p + +type i interface { + j() +} + +type s struct{} + +func (s) j() {} + +type foo map[string]i + +var f = foo{ + "1": s{}, + "2": s{}, +}