Skip to content

Commit b847d4c

Browse files
cuonglmgopherbot
authored andcommitted
cmd/compile: fix early deadcode with label statement
CL 517775 moved early deadcode into unified writer. with new way to handle dead code with label statement involved: any statements after terminating statement will be considered dead until next label statement. However, this is not safe, because code after label statement may still refer to dead statements between terminating and label statement. It's only safe to remove statements after terminating *and* label one. Fixes #65593 Change-Id: Idb630165240931fad50789304a9e4535f51f56e2 Reviewed-on: https://go-review.googlesource.com/c/go/+/565596 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Keith Randall <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> Auto-Submit: Cuong Manh Le <[email protected]>
1 parent fdb2008 commit b847d4c

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/cmd/compile/internal/noder/writer.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,10 +1217,17 @@ func (w *writer) stmt(stmt syntax.Stmt) {
12171217
func (w *writer) stmts(stmts []syntax.Stmt) {
12181218
dead := false
12191219
w.Sync(pkgbits.SyncStmts)
1220-
for _, stmt := range stmts {
1221-
if dead {
1222-
// Any statements after a terminating statement are safe to
1223-
// omit, at least until the next labeled statement.
1220+
var lastLabel = -1
1221+
for i, stmt := range stmts {
1222+
if _, ok := stmt.(*syntax.LabeledStmt); ok {
1223+
lastLabel = i
1224+
}
1225+
}
1226+
for i, stmt := range stmts {
1227+
if dead && i > lastLabel {
1228+
// Any statements after a terminating and last label statement are safe to omit.
1229+
// Otherwise, code after label statement may refer to dead stmts between terminating
1230+
// and label statement, see issue #65593.
12241231
if _, ok := stmt.(*syntax.LabeledStmt); !ok {
12251232
continue
12261233
}

test/fixedbugs/issue65593.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// compile
2+
3+
// Copyright 2024 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+
const run = false
10+
11+
func f() {
12+
if !run {
13+
return
14+
}
15+
16+
messages := make(chan struct{}, 1)
17+
main:
18+
for range messages {
19+
break main
20+
}
21+
}

0 commit comments

Comments
 (0)