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] diff --git a/test/fixedbugs/issue78408.go b/test/fixedbugs/issue78408.go new file mode 100644 index 0000000000..a43e5c23f1 --- /dev/null +++ b/test/fixedbugs/issue78408.go @@ -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 + } +}