cmd/compile: fix wrong label loop during rangefunc rewrite

During the rangefunc rewrite, the compiler must correctly identify
the target of branch statements. When a label is defined within a
nested scope - such as inside a function literal or a closure - it
can shadow a label with the same name in the outer scope.

If the rewrite logic does not account for this shadowing, it may
incorrectly associate a branch with a nested label rather than the
intended loop label. Since the typechecker already guarantees that
labels are unique within their respective scopes, any duplicate label
name encountered must belong to a nested scope. These should be
skipped to ensure branch computing correctly targets the current
range-loop scope.

Fixes #78408

Change-Id: I4dce8a4d956f41b3a717a509f8c3f7478720be9f
Reviewed-on: https://go-review.googlesource.com/c/go/+/761420
Reviewed-by: Keith Randall <khr@golang.org>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Jakub Ciolek <jakub@ciolek.dev>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
Reviewed-by: Keith Randall <khr@google.com>
This commit is contained in:
Cuong Manh Le
2026-03-31 18:53:51 +07:00
committed by Gopher Robot
parent 9002bd9fa1
commit f665ff8bc1
2 changed files with 26 additions and 1 deletions

View File

@@ -994,7 +994,14 @@ func (r *rewriter) computeBranchNext() {
l := n.Label.Value
labels = append(labels, l)
f := forStack[len(forStack)-1]
r.labelLoop[l] = f
if _, existed := r.labelLoop[l]; existed {
// The typechecker has already validated that labels are unique within this scope.
// If a duplicate label 'X' is encountered, it must reside in a nested scope
// (e.g., inside a function literal). Because nested labels do not affect
// the current control flow analysis, they can be safely ignored.
} else {
r.labelLoop[l] = f
}
}
} else {
n := stack[len(stack)-1]

View File

@@ -0,0 +1,18 @@
// compile
// Copyright 2026 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
func F() {
A:
for range (func(func() bool))(nil) {
_ = func() {
A:
goto A
}
goto A
}
}