1
0
mirror of https://github.com/golang/go synced 2024-11-26 04:07:59 -07:00

cmd/compile: fix unexpected type alias crash

OCOMPLIT stores the pre-typechecked type in n.Right, and then moves it
to n.Type. However, it wasn't clearing n.Right, so n.Right continued
to point to the OTYPE node. (Exception: slice literals reused n.Right
to store the array length.)

When exporting inline function bodies, we don't expect to need to save
any type aliases. Doing so wouldn't be wrong per se, but it's
completely unnecessary and would just bloat the export data.

However, reexportdep (whose role is to identify types needed by inline
function bodies) uses a generic tree traversal mechanism, which visits
n.Right even for O{ARRAY,MAP,STRUCT}LIT nodes. This means it finds the
OTYPE node, and mistakenly interpreted that the type alias needs to be
exported.

The straight forward fix is to just clear n.Right when typechecking
composite literals.

Fixes #24173.

Change-Id: Ia2d556bfdd806c83695b08e18b6cd71eff0772fc
Reviewed-on: https://go-review.googlesource.com/97719
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Matthew Dempsky 2018-02-28 11:22:05 -08:00
parent 1e308fbc1a
commit b3f00c6985
2 changed files with 23 additions and 1 deletions

View File

@ -2992,10 +2992,11 @@ func typecheckcomplit(n *Node) *Node {
t.SetNumElem(length)
}
if t.IsSlice() {
n.Right = nodintconst(length)
n.Op = OSLICELIT
n.Right = nodintconst(length)
} else {
n.Op = OARRAYLIT
n.Right = nil
}
case TMAP:
@ -3025,6 +3026,7 @@ func typecheckcomplit(n *Node) *Node {
}
n.Op = OMAPLIT
n.Right = nil
case TSTRUCT:
// Need valid field offsets for Xoffset below.
@ -3126,6 +3128,7 @@ func typecheckcomplit(n *Node) *Node {
}
n.Op = OSTRUCTLIT
n.Right = nil
}
if nerr != nerrors {

View File

@ -0,0 +1,19 @@
// compile
// Copyright 2018 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 p
type arrayAlias = [10]int
type mapAlias = map[int]int
type sliceAlias = []int
type structAlias = struct{}
func Exported() {
_ = arrayAlias{}
_ = mapAlias{}
_ = sliceAlias{}
_ = structAlias{}
}