Skip to content

Commit ad8c17b

Browse files
committed
cmd/compile: don't export dead code in inlineable fuctions
CL 37499 allows inlining more functions by ignoring dead code. However, that dead code can contain non-exportable constructs. Teach the exporter not to export dead code. Fixes #19679 Change-Id: Idb1d3794053514544b6f1035d29262aa6683e1e7 Reviewed-on: https://go-review.googlesource.com/38601 Run-TryBot: Josh Bleecher Snyder <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent a69754e commit ad8c17b

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,8 +1493,22 @@ func (p *exporter) stmt(n *Node) {
14931493
p.pos(n)
14941494
p.stmtList(n.Ninit)
14951495
p.expr(n.Left)
1496-
p.stmtList(n.Nbody)
1497-
p.stmtList(n.Rlist)
1496+
nbody := n.Nbody
1497+
rlist := n.Rlist
1498+
if Isconst(n.Left, CTBOOL) {
1499+
// if false { ... } or if true { ... }
1500+
// Only export the taken branch.
1501+
// This is more efficient,
1502+
// and avoids trying to export
1503+
// un-exportable nodes.
1504+
if n.Left.Bool() {
1505+
rlist = Nodes{}
1506+
} else {
1507+
nbody = Nodes{}
1508+
}
1509+
}
1510+
p.stmtList(nbody)
1511+
p.stmtList(rlist)
14981512

14991513
case OFOR:
15001514
p.op(OFOR)

test/fixedbugs/issue19679.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// compile
2+
3+
// Copyright 2017 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+
// Used to crash when a type switch was present in dead code
8+
// in an inlineable function.
9+
10+
package p
11+
12+
func Then() {
13+
var i interface{}
14+
if false {
15+
switch i.(type) {
16+
}
17+
}
18+
}
19+
20+
func Else() {
21+
var i interface{}
22+
if true {
23+
_ = i
24+
} else {
25+
switch i.(type) {
26+
}
27+
}
28+
}
29+
30+
func Switch() {
31+
var i interface{}
32+
switch 5 {
33+
case 3:
34+
switch i.(type) {
35+
}
36+
case 5:
37+
}
38+
}

0 commit comments

Comments
 (0)