From f665ff8bc1ab2576d173cb69c3b2e87916dc74fd Mon Sep 17 00:00:00 2001 From: Cuong Manh Le Date: Tue, 31 Mar 2026 18:53:51 +0700 Subject: 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 Auto-Submit: Cuong Manh Le LUCI-TryBot-Result: Go LUCI Reviewed-by: Jakub Ciolek Reviewed-by: Junyang Shao Reviewed-by: Keith Randall --- src/cmd/compile/internal/rangefunc/rewrite.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/cmd') diff --git a/src/cmd/compile/internal/rangefunc/rewrite.go b/src/cmd/compile/internal/rangefunc/rewrite.go index 551e126af8..2421efbad8 100644 --- a/src/cmd/compile/internal/rangefunc/rewrite.go +++ b/src/cmd/compile/internal/rangefunc/rewrite.go @@ -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] -- cgit v1.3-5-g9baa