From ea38fa534559d1c25e629d0b1e96a730fa96780b Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Sun, 3 Nov 2024 21:27:00 +0700 Subject: [PATCH] cmd/compile: fix mis-compilation with labeled fallthrough A fallthrough statement can be a labeled fallthrough per Go spec. However, the hasFallthrough function is not considering this case, causing mis-compilation. Fixing this by un-wrapping (possible nested) labeled fallthrough statements if any. Fixes #70173 Change-Id: Ic93d4fb75ff02703a32dfc63c3e84a8b7f78c261 Reviewed-on: https://go-review.googlesource.com/c/go/+/624717 Reviewed-by: Cherry Mui Reviewed-by: Keith Randall Reviewed-by: Keith Randall Reviewed-by: Youlin Feng Auto-Submit: Cuong Manh Le LUCI-TryBot-Result: Go LUCI --- src/cmd/compile/internal/noder/writer.go | 12 +++++++++- .../compile/testdata/script/issue70173.txt | 23 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/cmd/compile/testdata/script/issue70173.txt diff --git a/src/cmd/compile/internal/noder/writer.go b/src/cmd/compile/internal/noder/writer.go index 564087d912..f4b02f279d 100644 --- a/src/cmd/compile/internal/noder/writer.go +++ b/src/cmd/compile/internal/noder/writer.go @@ -3070,7 +3070,17 @@ func isPtrTo(from, to types2.Type) bool { // hasFallthrough reports whether stmts ends in a fallthrough // statement. func hasFallthrough(stmts []syntax.Stmt) bool { - last, ok := lastNonEmptyStmt(stmts).(*syntax.BranchStmt) + // From spec: the last non-empty statement may be a (possibly labeled) "fallthrough" statement + // Stripping (possible nested) labeled statement if any. + stmt := lastNonEmptyStmt(stmts) + for { + ls, ok := stmt.(*syntax.LabeledStmt) + if !ok { + break + } + stmt = ls.Stmt + } + last, ok := stmt.(*syntax.BranchStmt) return ok && last.Tok == syntax.Fallthrough } diff --git a/src/cmd/compile/testdata/script/issue70173.txt b/src/cmd/compile/testdata/script/issue70173.txt new file mode 100644 index 0000000000..20d4b4fcbe --- /dev/null +++ b/src/cmd/compile/testdata/script/issue70173.txt @@ -0,0 +1,23 @@ +go run main.go +! stdout . +! stderr . + +-- main.go -- + +package main + +func main() { + switch { + case true: + _: + fallthrough + default: + } + switch { + case true: + _: + _: + fallthrough + default: + } +} -- 2.48.1