diff options
author | Cuong Manh Le <cuong.manhle.vn@gmail.com> | 2024-02-21 18:50:11 +0700 |
---|---|---|
committer | Than McIntosh <thanm@google.com> | 2024-03-26 19:31:39 +0000 |
commit | 3826650c99b177364670dad9766ea499cab48dfc (patch) | |
tree | b54261543e0fc4ede04743af24faba54a375b324 | |
parent | e71b0b1fee7df59a99ab6b5192854695d21ff688 (diff) | |
download | go-3826650c99b177364670dad9766ea499cab48dfc.tar.gz go-3826650c99b177364670dad9766ea499cab48dfc.zip |
[release-branch.go1.22] 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 #66134
Change-Id: Idb630165240931fad50789304a9e4535f51f56e2
Reviewed-on: https://go-review.googlesource.com/c/go/+/565596
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/569717
Reviewed-by: Michael Knyszek <mknyszek@google.com>
-rw-r--r-- | src/cmd/compile/internal/noder/writer.go | 15 | ||||
-rw-r--r-- | test/fixedbugs/issue65593.go | 21 |
2 files changed, 32 insertions, 4 deletions
diff --git a/src/cmd/compile/internal/noder/writer.go b/src/cmd/compile/internal/noder/writer.go index e5894c9505..c317f392c1 100644 --- a/src/cmd/compile/internal/noder/writer.go +++ b/src/cmd/compile/internal/noder/writer.go @@ -1209,10 +1209,17 @@ func (w *writer) stmt(stmt syntax.Stmt) { func (w *writer) stmts(stmts []syntax.Stmt) { dead := false w.Sync(pkgbits.SyncStmts) - for _, stmt := range stmts { - if dead { - // Any statements after a terminating statement are safe to - // omit, at least until the next labeled statement. + var lastLabel = -1 + for i, stmt := range stmts { + if _, ok := stmt.(*syntax.LabeledStmt); ok { + lastLabel = i + } + } + for i, stmt := range stmts { + if dead && i > lastLabel { + // Any statements after a terminating and last label statement are safe to omit. + // Otherwise, code after label statement may refer to dead stmts between terminating + // and label statement, see issue #65593. if _, ok := stmt.(*syntax.LabeledStmt); !ok { continue } diff --git a/test/fixedbugs/issue65593.go b/test/fixedbugs/issue65593.go new file mode 100644 index 0000000000..892a78122e --- /dev/null +++ b/test/fixedbugs/issue65593.go @@ -0,0 +1,21 @@ +// compile + +// Copyright 2024 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 + +const run = false + +func f() { + if !run { + return + } + + messages := make(chan struct{}, 1) +main: + for range messages { + break main + } +} |