Skip to content

Commit ac875bc

Browse files
committed
cmd/compile: don't bother to declare closure inside redeclared func
Fixes #17758 Change-Id: I75f5dc5be85fd8a6791ac89dfc0681be759cca36 Reviewed-on: https://go-review.googlesource.com/c/go/+/248517 Run-TryBot: Cuong Manh Le <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 98a0071 commit ac875bc

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/cmd/compile/internal/gc/closure.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,17 @@ func typecheckclosure(clo *Node, top int) {
108108

109109
xfunc.Func.Nname.Sym = closurename(Curfn)
110110
disableExport(xfunc.Func.Nname.Sym)
111-
declare(xfunc.Func.Nname, PFUNC)
111+
if xfunc.Func.Nname.Sym.Def != nil {
112+
// The only case we can reach here is when the outer function was redeclared.
113+
// In that case, don't bother to redeclare the closure. Otherwise, we will get
114+
// a spurious error message, see #17758. While we are here, double check that
115+
// we already reported other error.
116+
if nsavederrors+nerrors == 0 {
117+
Fatalf("unexpected symbol collision %v", xfunc.Func.Nname.Sym)
118+
}
119+
} else {
120+
declare(xfunc.Func.Nname, PFUNC)
121+
}
112122
xfunc = typecheck(xfunc, ctxStmt)
113123

114124
// Type check the body now, but only if we're inside a function.

test/fixedbugs/issue17758.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// errorcheck
2+
3+
// Copyright 2020 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
func foo() {
10+
_ = func() {}
11+
}
12+
13+
func foo() { // ERROR "foo redeclared in this block"
14+
_ = func() {}
15+
}
16+
17+
func main() {}

0 commit comments

Comments
 (0)