Skip to content

reflect: Type.Comparable returns incorrect values for some types obtained via reflection #21011

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

Closed
ghost opened this issue Jul 14, 2017 · 7 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Milestone

Comments

@ghost
Copy link

ghost commented Jul 14, 2017

Please answer these questions before submitting your issue. Thanks!

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

go version go1.8.3 darwin/amd64

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.8/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.8/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/rk/3wxxpsdj3kx052390hbwxk0h0000gn/T/go-build181924434=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.

package main

import (
	"reflect"
)

func main() {
	var i interface{} = func() { println("Hello world") }
	v := reflect.Indirect(reflect.ValueOf(&i))
	if v.Type().Comparable() {
		m := map[interface{}]string{}
		m[v.Interface()] = "this will panic"
	} else {
		println("v is not comparable")
	}
}

A link on play.golang.org is best.
https://play.golang.org/p/l7rmrLiGvY

What did you expect to see?

v is not comparable

What did you see instead?

panic: runtime error: hash of unhashable type func()

goroutine 1 [running]:
main.main()
	/tmp/sandbox040073154/main.go:12 +0x1e0
@bradfitz bradfitz changed the title reflect.Type.Comparable() returns incorrect values for some types obtained via reflection reflect: Type.Comparable returns incorrect values for some types obtained via reflection Jul 14, 2017
@bradfitz bradfitz added the NeedsFix The path to resolution is known, but the work has not been done. label Jul 14, 2017
@bradfitz
Copy link
Contributor

Nice. But because this is not a regression, there's no time (or risk) budget for Go 1.9, due out shortly.

@bradfitz bradfitz added this to the Go1.10 milestone Jul 14, 2017
@ianlancetaylor
Copy link
Member

I think this is correct behavior. If you print v.Type().String(), you will see that it has type interface {}. Interface types are comparable. It just happens that this interface value holds a value that can not be compared.

In other words, this is approximately equivalent to

package main

func main() {
	var i interface{} = func() { println("Hello world") }
	println(i == i)
}

which will also panic.

@bradfitz
Copy link
Contributor

Whoops, I overlooked the type interface {} after "var i".

I agree this looks fine.

@ghost
Copy link
Author

ghost commented Jul 14, 2017

That's fine, but if interface{} is comparable, why can't I use it in the map then?

@ghost
Copy link
Author

ghost commented Jul 14, 2017

Also everything works as expected if I remove pointer: v := reflect.Indirect(reflect.ValueOf(_&_i))

@ianlancetaylor
Copy link
Member

You can use an interface{} in a map.

What you can't do is use an interface{} in a map if you store a func value in the interface.

If you remove the pointer, then the value you get in v has type func(), which is not comparable. In both cases, try printing v.Type().String() to see what is going on.

@ghost
Copy link
Author

ghost commented Jul 14, 2017

Thanks! I think I've misunderstood, what Interface() returns:

// Special case: return the element inside the interface.
// Empty interface has one layout, all interfaces with
// methods have a second layout.

@golang golang locked and limited conversation to collaborators Jul 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Projects
None yet
Development

No branches or pull requests

3 participants