]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: don't bother to declare closure inside redeclared func
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Fri, 14 Aug 2020 17:44:58 +0000 (00:44 +0700)
committerCuong Manh Le <cuong.manhle.vn@gmail.com>
Wed, 19 Aug 2020 02:25:13 +0000 (02:25 +0000)
Fixes #17758

Change-Id: I75f5dc5be85fd8a6791ac89dfc0681be759cca36
Reviewed-on: https://go-review.googlesource.com/c/go/+/248517
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/cmd/compile/internal/gc/closure.go
test/fixedbugs/issue17758.go [new file with mode: 0644]

index 04fb7d54951d20bd2571a80ed32a3eefb596e2ef..23e48939b46b96f39f77280c605f0e583d71780a 100644 (file)
@@ -108,7 +108,17 @@ func typecheckclosure(clo *Node, top int) {
 
        xfunc.Func.Nname.Sym = closurename(Curfn)
        disableExport(xfunc.Func.Nname.Sym)
-       declare(xfunc.Func.Nname, PFUNC)
+       if xfunc.Func.Nname.Sym.Def != nil {
+               // The only case we can reach here is when the outer function was redeclared.
+               // In that case, don't bother to redeclare the closure. Otherwise, we will get
+               // a spurious error message, see #17758. While we are here, double check that
+               // we already reported other error.
+               if nsavederrors+nerrors == 0 {
+                       Fatalf("unexpected symbol collision %v", xfunc.Func.Nname.Sym)
+               }
+       } else {
+               declare(xfunc.Func.Nname, PFUNC)
+       }
        xfunc = typecheck(xfunc, ctxStmt)
 
        // Type check the body now, but only if we're inside a function.
diff --git a/test/fixedbugs/issue17758.go b/test/fixedbugs/issue17758.go
new file mode 100644 (file)
index 0000000..e7f2f3a
--- /dev/null
@@ -0,0 +1,17 @@
+// errorcheck
+
+// Copyright 2020 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 main
+
+func foo() {
+       _ = func() {}
+}
+
+func foo() { // ERROR "foo redeclared in this block"
+       _ = func() {}
+}
+
+func main() {}