New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: spurious `foo.func1 redeclared in this block` #17758

Open
dominikh opened this Issue Nov 3, 2016 · 5 comments

Comments

Projects
None yet
9 participants
@dominikh
Member

dominikh commented Nov 3, 2016

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.7.3 linux/amd64
go version devel +53fc330 Tue Nov 1 04:42:33 2016 +0000 linux/amd64

What did you do?

https://play.golang.org/p/m7Z1qHv6ak

What did you expect to see?

main.go:7: foo redeclared in this block
	previous declaration at tmp/sandbox669345157/main.go:3

What did you see instead?

main.go:7: foo redeclared in this block
	previous declaration at tmp/sandbox669345157/main.go:3
main.go:8: foo.func1 redeclared in this block
	previous declaration at tmp/sandbox669345157/main.go:4

@griesemer griesemer added this to the Go1.8Maybe milestone Nov 3, 2016

@griesemer

This comment has been minimized.

Contributor

griesemer commented Nov 3, 2016

At least we get an error. Not urgent.

@odeke-em

This comment has been minimized.

Member

odeke-em commented Nov 3, 2016

Interestingly when the content of the repeated functions differs, we get the expected single warning

package main

func foo() {
    _ = func() {}
}

func foo() {
    _ = "hey"
}

func main() {}
tmp/sandbox704696438/main.go:7: foo redeclared in this block
    previous declaration at tmp/sandbox704696438/main.go:3

In the reported issue log the compiler seems to be peeking in to compare bodies of the functions hence the spurious

main.go:8: foo.func1 redeclared in this block
    previous declaration at tmp/sandbox669345157/main.go:4

and then

package main

func foo() {
    _ = func() {}
    _ = func() { _ = "hey" }
}

func foo() {
    _ = func() {}
    _ = func() { _ = "hey" }
}

func main() {}
tmp/sandbox613823462/main.go:8: foo redeclared in this block
    previous declaration at tmp/sandbox613823462/main.go:3
tmp/sandbox613823462/main.go:9: foo.func1 redeclared in this block
    previous declaration at tmp/sandbox613823462/main.go:4
tmp/sandbox613823462/main.go:10: foo.func2 redeclared in this block
    previous declaration at tmp/sandbox613823462/main.go:5
@dominikh

This comment has been minimized.

Member

dominikh commented Nov 3, 2016

@odeke-em Your first example only shows a single error because the anonymous function isn't declared a second time. It's isn't really "comparing the bodies" as much as compiling them. Anonymous functions still have a name, which is a concatenation of the outer function's name, some static text, and a counter. Having two outer functions with the same name (the first error) with anonymous functions inside will lead to these names also clashing.

@odeke-em

This comment has been minimized.

Member

odeke-em commented Nov 3, 2016

Ah I see, thanks for the explanation @dominikh, TIL!

@dhananjay92

This comment has been minimized.

Member

dhananjay92 commented Nov 5, 2016

First error is from parseFile() and second is from typecheckslice().

Root cause: For both of the above calls, closurename() assigns same name —func1 to blank function. n.Func.Outerfunc.Func.Closgen++ during parseFile() did not have effect in the next call in type-checking. Looks like it got reinitialized somewhere in between those two events.

Here's full stacktrace, obtained by putting debug.PrintStack() in yyerrorl():

goroutine 1 [running]:
runtime/debug.Stack(0xc4202fb570, 0x471925, 0xa45fa4)
    /repositories/go/src/runtime/debug/stack.go:24 +0x79
runtime/debug.PrintStack()
    /repositories/go/src/runtime/debug/stack.go:16 +0x22
cmd/compile/internal/gc.yyerrorl(0x7, 0xa60f3b, 0x2c, 0xc4202fb698, 0x3, 0x3)
    /repositories/go/src/cmd/compile/internal/gc/subr.go:100 +0x37d
cmd/compile/internal/gc.redeclare(0xc420321f80, 0xa5106b, 0xd)
    /repositories/go/src/cmd/compile/internal/gc/dcl.go:140 +0x2a2
cmd/compile/internal/gc.declare(0xc420322e10, 0xc420322e06)
    /repositories/go/src/cmd/compile/internal/gc/dcl.go:206 +0x209
cmd/compile/internal/gc.(*noder).funcHeader(0xc42030ba20, 0xc420319860, 0x0)
    /repositories/go/src/cmd/compile/internal/gc/noder.go:269 +0x2cf
cmd/compile/internal/gc.(*noder).funcDecl(0xc42030ba20, 0xc420319860, 0xc420319860)
    /repositories/go/src/cmd/compile/internal/gc/noder.go:209 +0x4d
cmd/compile/internal/gc.(*noder).decls(0xc42030ba20, 0xc420310d00, 0x3, 0x4, 0xc42000c4c0, 0x0, 0x0)
    /repositories/go/src/cmd/compile/internal/gc/noder.go:94 +0x8ad
cmd/compile/internal/gc.(*noder).file(0xc42030ba20, 0xc420310a80)
    /repositories/go/src/cmd/compile/internal/gc/noder.go:47 +0x9c
cmd/compile/internal/gc.parseFile(0x7ffedfdc413c, 0x1f)
    /repositories/go/src/cmd/compile/internal/gc/noder.go:24 +0x262
cmd/compile/internal/gc.Main()
    /repositories/go/src/cmd/compile/internal/gc/main.go:311 +0x13f8
main.main()
    /repositories/go/src/cmd/compile/main.go:47 +0x15d


goroutine 1 [running]:
runtime/debug.Stack(0xc420335968, 0x471925, 0xa45fa4)
    /repositories/go/src/runtime/debug/stack.go:24 +0x79
runtime/debug.PrintStack()
    /repositories/go/src/runtime/debug/stack.go:16 +0x22
cmd/compile/internal/gc.yyerrorl(0x8, 0xa60f3b, 0x2c, 0xc420335a90, 0x3, 0x3)
    /repositories/go/src/cmd/compile/internal/gc/subr.go:100 +0x37d
cmd/compile/internal/gc.redeclare(0xc420327490, 0xa5106b, 0xd)
    /repositories/go/src/cmd/compile/internal/gc/dcl.go:140 +0x2a2
cmd/compile/internal/gc.declare(0xc420323710, 0xc420323706)
    /repositories/go/src/cmd/compile/internal/gc/dcl.go:206 +0x209
cmd/compile/internal/gc.makeclosure(0xc420322f30, 0x1)
    /repositories/go/src/cmd/compile/internal/gc/closure.go:222 +0x200
cmd/compile/internal/gc.typecheckclosure(0xc420322f30, 0x2)
    /repositories/go/src/cmd/compile/internal/gc/closure.go:148 +0x239
cmd/compile/internal/gc.typecheck1(0xc420322f30, 0x2, 0xc4202f3320)
    /repositories/go/src/cmd/compile/internal/gc/typecheck.go:1905 +0x536e
cmd/compile/internal/gc.typecheck(0xc420322f30, 0x2, 0xc4202f3320)
    /repositories/go/src/cmd/compile/internal/gc/typecheck.go:190 +0x608
cmd/compile/internal/gc.typecheckas(0xc420323050)
    /repositories/go/src/cmd/compile/internal/gc/typecheck.go:3261 +0xa9
cmd/compile/internal/gc.typecheck1(0xc420323050, 0x1, 0x97)
    /repositories/go/src/cmd/compile/internal/gc/typecheck.go:1968 +0x3c82
cmd/compile/internal/gc.typecheck(0xc420323050, 0x1, 0xc420323050)
    /repositories/go/src/cmd/compile/internal/gc/typecheck.go:190 +0x608
cmd/compile/internal/gc.typecheckslice(0xc42000c510, 0x1, 0x1, 0x1)
    /repositories/go/src/cmd/compile/internal/gc/typecheck.go:51 +0x76
cmd/compile/internal/gc.Main()
    /repositories/go/src/cmd/compile/internal/gc/main.go:371 +0x1987
main.main()
    /repositories/go/src/cmd/compile/main.go:47 +0x15d

@quentinmit quentinmit added the NeedsFix label Nov 7, 2016

@rsc rsc modified the milestones: Go1.9, Go1.8Maybe Nov 11, 2016

@randall77 randall77 modified the milestones: Go1.10, Go1.9 Jun 6, 2017

@mdempsky mdempsky modified the milestones: Go1.10, Go1.11 Nov 29, 2017

@gopherbot gopherbot modified the milestones: Go1.11, Unplanned May 23, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment