From 5ae8a28834c8b809a52c74617e2a8530acec8095 Mon Sep 17 00:00:00 2001 From: qiulaidongfeng <2645477756@qq.com> Date: Wed, 25 Sep 2024 18:06:31 +0800 Subject: [PATCH] new Change-Id: I6dec60bfec56e1c0b0bfa8ab2c65f305af426bfb --- src/hash/maphash/maphash.go | 12 +++++------- src/hash/maphash/maphash_test.go | 28 ++++++++-------------------- 2 files changed, 13 insertions(+), 27 deletions(-) diff --git a/src/hash/maphash/maphash.go b/src/hash/maphash/maphash.go index da7ed0a581..02475d5583 100644 --- a/src/hash/maphash/maphash.go +++ b/src/hash/maphash/maphash.go @@ -13,7 +13,6 @@ package maphash import ( - "fmt" "internal/abi" "internal/byteorder" "math" @@ -296,11 +295,10 @@ func Comparable[T comparable](seed Seed, v T) uint64 { } func comparableReady[T comparable](v T) { - // Let v be on the heap, - // make sure that if v is a pointer to a variable inside the function, - // if v and the value it points to do not change, - // Comparable(seed,v) before goroutine stack growth - // is equal to Comparable(seed,v) after goroutine stack growth. + // Force v to be on the heap. + // We cannot hash pointers to local variables, + // as the address of the local variable + // might change on stack growth. abi.Escape(v) } @@ -374,7 +372,7 @@ func appendT(h *Hash, v reflect.Value) { appendT(h, v.Elem()) return } - panic(fmt.Errorf("hash/maphash: %s not comparable", v.Type().String())) + panic("maphash: " + v.Type().String() + " not comparable") } func (h *Hash) float64(f float64) { diff --git a/src/hash/maphash/maphash_test.go b/src/hash/maphash/maphash_test.go index 9591505bd8..f1b292e101 100644 --- a/src/hash/maphash/maphash_test.go +++ b/src/hash/maphash/maphash_test.go @@ -6,12 +6,10 @@ package maphash import ( "bytes" - "crypto/rand" "fmt" "hash" "math" "reflect" - "strings" "testing" "unsafe" ) @@ -268,21 +266,10 @@ func testComparableNoEqual[T comparable](t *testing.T, v1, v2 T) { } } -var heapStrValue []byte +var heapStrValue = []byte("aTestString") -//go:noinline func heapStr(t *testing.T) string { - s := make([]byte, 10) - if heapStrValue != nil { - copy(s, heapStrValue) - } else { - _, err := rand.Read(s) - if err != nil { - t.Fatal(err) - } - heapStrValue = s - } - return string(s) + return string(heapStrValue) } func testComparable[T comparable](t *testing.T, v T, v2 ...T) { @@ -405,14 +392,15 @@ func TestComparableShouldPanic(t *testing.T) { defer func() { err := recover() if err == nil { - t.Fatalf("hash any([]byte) should panic(error) in maphash.appendT") + t.Fatalf("hash any([]byte) should panic in maphash.appendT") } - e, ok := err.(error) + s, ok := err.(string) if !ok { - t.Fatalf("hash any([]byte) should panic(error) in maphash.appendT") + t.Fatalf("hash any([]byte) should panic in maphash.appendT") } - if !strings.Contains(e.Error(), "comparable") { - t.Fatalf("hash any([]byte) should panic(error) in maphash.appendT") + want := "maphash: []uint8 not comparable" + if s != want { + t.Fatalf("want %s, got %s", want, s) } }() Comparable(MakeSeed(), a)