1
0
mirror of https://github.com/golang/go synced 2024-11-14 13:20:30 -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
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) {

View File

@ -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)