Skip to content

gccgo: confusing closure names #29690

Open
@cherrymui

Description

@cherrymui

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

gccgo2 (GCC) 9.0.0 20190108 (experimental)

What operating system and processor architecture are you using (go env)?

Linux/AMD64

What did you do?

package main

type T struct {
	closure1, closure2 func()
}

func (t *T) M1()  { t.closure1 = func() { t.closure2() } }
func (t *T) M2()  { t.closure2 = func() { panic("XXX") } }

func main() {
	var t T
	t.M1()
	t.M2()
	t.closure1()
}

Built with gccgo, this program prints

panic: XXX

goroutine 1 [running]:
panic
	/tmp/src/gccgo/libgo/go/runtime/panic.go:588
main.func1
	/tmp/c.go:8
main.func1
	/tmp/c.go:7
main.main
	/tmp/c.go:14

The closures on line 7 and 8 are different functions, yet have the same name, which is confusing. It is ok that closures have somewhat artificial names, but it is super confusing if different closures have the name.

It appears to me (without checking the source code) that if the closures are defined in global scope or inside a function (NOT method), they are properly named as package_name.funcN or package_name.function_name.funcN with proper N. However, if they are defined in methods, they are all named package_name.funcN with N starting from 1 in each method. In this case, the two closures are defined in two different methods, and both named main.func1.

cc @ianlancetaylor @thanm

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions