mirror of
https://github.com/golang/go
synced 2024-11-14 05:40:29 -07:00
new
Change-Id: I6dec60bfec56e1c0b0bfa8ab2c65f305af426bfb
This commit is contained in:
parent
d73a7a5417
commit
5ae8a28834
@ -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) {
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user