From 7b44fcd865370d5ecf7471b04ad1b4e013e63708 Mon Sep 17 00:00:00 2001 From: Damian Gryski Date: Fri, 24 Feb 2023 23:48:31 -0800 Subject: [PATCH] runtime: properly turn pointer into empty interface when hashing --- src/runtime/hashmap.go | 2 +- testdata/map.go | 34 ++++++++++++++++++++++++++++++++++ testdata/map.txt | 1 + 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/runtime/hashmap.go b/src/runtime/hashmap.go index 2db09f59..e00b223d 100644 --- a/src/runtime/hashmap.go +++ b/src/runtime/hashmap.go @@ -563,7 +563,7 @@ func hashmapInterfaceHash(itf interface{}, seed uintptr) uint32 { } func hashmapInterfacePtrHash(iptr unsafe.Pointer, size uintptr, seed uintptr) uint32 { - _i := *(*_interface)(iptr) + _i := *(*interface{})(iptr) return hashmapInterfaceHash(_i, seed) } diff --git a/testdata/map.go b/testdata/map.go index d3088991..d746cf9f 100644 --- a/testdata/map.go +++ b/testdata/map.go @@ -129,6 +129,8 @@ func main() { floatcmplx() mapgrow() + + interfacerehash() } func floatcmplx() { @@ -274,3 +276,35 @@ func mapgrow() { } println("done") } + +type Counter interface { + count() int +} + +type counter struct { + i int +} + +func (c *counter) count() int { + return c.i +} + +func interfacerehash() { + m := make(map[Counter]int) + + for i := 0; i < 20; i++ { + c := &counter{i} + m[c] = i + } + + var failures int + for k, v := range m { + if got := m[k]; got != v { + println("lookup failure got", got, "want", v) + failures++ + } + } + if failures == 0 { + println("no interface lookup failures") + } +} diff --git a/testdata/map.txt b/testdata/map.txt index 6bf04c80..d5e553b1 100644 --- a/testdata/map.txt +++ b/testdata/map.txt @@ -80,3 +80,4 @@ tested growing of a map 2 2 done +no interface lookup failures