1
0
mirror of https://github.com/golang/go synced 2024-11-14 17:30:29 -07:00
Change-Id: I6dec60bfec56e1c0b0bfa8ab2c65f305af426bfb
This commit is contained in:
qiulaidongfeng 2024-09-25 18:06:31 +08:00
parent d73a7a5417
commit 5ae8a28834
No known key found for this signature in database
GPG Key ID: 2C6D0C0AA6632FF6
2 changed files with 13 additions and 27 deletions

View File

@ -13,7 +13,6 @@
package maphash package maphash
import ( import (
"fmt"
"internal/abi" "internal/abi"
"internal/byteorder" "internal/byteorder"
"math" "math"
@ -296,11 +295,10 @@ func Comparable[T comparable](seed Seed, v T) uint64 {
} }
func comparableReady[T comparable](v T) { func comparableReady[T comparable](v T) {
// Let v be on the heap, // Force v to be on the heap.
// make sure that if v is a pointer to a variable inside the function, // We cannot hash pointers to local variables,
// if v and the value it points to do not change, // as the address of the local variable
// Comparable(seed,v) before goroutine stack growth // might change on stack growth.
// is equal to Comparable(seed,v) after goroutine stack growth.
abi.Escape(v) abi.Escape(v)
} }
@ -374,7 +372,7 @@ func appendT(h *Hash, v reflect.Value) {
appendT(h, v.Elem()) appendT(h, v.Elem())
return 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) { func (h *Hash) float64(f float64) {

View File

@ -6,12 +6,10 @@ package maphash
import ( import (
"bytes" "bytes"
"crypto/rand"
"fmt" "fmt"
"hash" "hash"
"math" "math"
"reflect" "reflect"
"strings"
"testing" "testing"
"unsafe" "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 { func heapStr(t *testing.T) string {
s := make([]byte, 10) return string(heapStrValue)
if heapStrValue != nil {
copy(s, heapStrValue)
} else {
_, err := rand.Read(s)
if err != nil {
t.Fatal(err)
}
heapStrValue = s
}
return string(s)
} }
func testComparable[T comparable](t *testing.T, v T, v2 ...T) { func testComparable[T comparable](t *testing.T, v T, v2 ...T) {
@ -405,14 +392,15 @@ func TestComparableShouldPanic(t *testing.T) {
defer func() { defer func() {
err := recover() err := recover()
if err == nil { 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 { 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") { want := "maphash: []uint8 not comparable"
t.Fatalf("hash any([]byte) should panic(error) in maphash.appendT") if s != want {
t.Fatalf("want %s, got %s", want, s)
} }
}() }()
Comparable(MakeSeed(), a) Comparable(MakeSeed(), a)