Skip to content

Commit b3f00c6

Browse files
committed
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 <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 1e308fb commit b3f00c6

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2992,10 +2992,11 @@ func typecheckcomplit(n *Node) *Node {
29922992
t.SetNumElem(length)
29932993
}
29942994
if t.IsSlice() {
2995-
n.Right = nodintconst(length)
29962995
n.Op = OSLICELIT
2996+
n.Right = nodintconst(length)
29972997
} else {
29982998
n.Op = OARRAYLIT
2999+
n.Right = nil
29993000
}
30003001

30013002
case TMAP:
@@ -3025,6 +3026,7 @@ func typecheckcomplit(n *Node) *Node {
30253026
}
30263027

30273028
n.Op = OMAPLIT
3029+
n.Right = nil
30283030

30293031
case TSTRUCT:
30303032
// Need valid field offsets for Xoffset below.
@@ -3126,6 +3128,7 @@ func typecheckcomplit(n *Node) *Node {
31263128
}
31273129

31283130
n.Op = OSTRUCTLIT
3131+
n.Right = nil
31293132
}
31303133

31313134
if nerr != nerrors {

test/fixedbugs/issue24173.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// compile
2+
3+
// Copyright 2018 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+
package p
8+
9+
type arrayAlias = [10]int
10+
type mapAlias = map[int]int
11+
type sliceAlias = []int
12+
type structAlias = struct{}
13+
14+
func Exported() {
15+
_ = arrayAlias{}
16+
_ = mapAlias{}
17+
_ = sliceAlias{}
18+
_ = structAlias{}
19+
}

0 commit comments

Comments
 (0)