]> Cypherpunks.ru repositories - gostls13.git/blobdiff - src/reflect/all_test.go
reflect: optimize Value.IsZero for array types
[gostls13.git] / src / reflect / all_test.go
index c257bec1e589fbecf1e92bcaccd56edc3fa65980..bece56f7e73b06c670442b811ad8819cf881477f 100644 (file)
@@ -1396,6 +1396,11 @@ func TestIsNil(t *testing.T) {
        NotNil(fi, t)
 }
 
+func setField[S, V any](in S, offset uintptr, value V) (out S) {
+       *(*V)(unsafe.Add(unsafe.Pointer(&in), offset)) = value
+       return in
+}
+
 func TestIsZero(t *testing.T) {
        for i, tt := range []struct {
                x    any
@@ -1429,14 +1434,14 @@ func TestIsZero(t *testing.T) {
                {float32(1.2), false},
                {float64(0), true},
                {float64(1.2), false},
-               {math.Copysign(0, -1), false},
+               {math.Copysign(0, -1), true},
                {complex64(0), true},
                {complex64(1.2), false},
                {complex128(0), true},
                {complex128(1.2), false},
-               {complex(math.Copysign(0, -1), 0), false},
-               {complex(0, math.Copysign(0, -1)), false},
-               {complex(math.Copysign(0, -1), math.Copysign(0, -1)), false},
+               {complex(math.Copysign(0, -1), 0), true},
+               {complex(0, math.Copysign(0, -1)), true},
+               {complex(math.Copysign(0, -1), math.Copysign(0, -1)), true},
                {uintptr(0), true},
                {uintptr(128), false},
                // Array
@@ -1449,6 +1454,8 @@ func TestIsZero(t *testing.T) {
                {[3][]int{{1}}, false},                  // incomparable array
                {[1 << 12]byte{}, true},
                {[1 << 12]byte{1}, false},
+               {[1]struct{ p *int }{}, true},
+               {[1]struct{ p *int }{{new(int)}}, false},
                {[3]Value{}, true},
                {[3]Value{{}, ValueOf(0), {}}, false},
                // Chan
@@ -1485,6 +1492,14 @@ func TestIsZero(t *testing.T) {
                {struct{ s []int }{[]int{1}}, false},  // incomparable struct
                {struct{ Value }{}, true},
                {struct{ Value }{ValueOf(0)}, false},
+               {struct{ _, a, _ uintptr }{}, true}, // comparable struct with blank fields
+               {setField(struct{ _, a, _ uintptr }{}, 0*unsafe.Sizeof(uintptr(0)), 1), true},
+               {setField(struct{ _, a, _ uintptr }{}, 1*unsafe.Sizeof(uintptr(0)), 1), false},
+               {setField(struct{ _, a, _ uintptr }{}, 2*unsafe.Sizeof(uintptr(0)), 1), true},
+               {struct{ _, a, _ func() }{}, true}, // incomparable struct with blank fields
+               {setField(struct{ _, a, _ func() }{}, 0*unsafe.Sizeof((func())(nil)), func() {}), true},
+               {setField(struct{ _, a, _ func() }{}, 1*unsafe.Sizeof((func())(nil)), func() {}), false},
+               {setField(struct{ _, a, _ func() }{}, 2*unsafe.Sizeof((func())(nil)), func() {}), true},
                // UnsafePointer
                {(unsafe.Pointer)(nil), true},
                {(unsafe.Pointer)(new(int)), false},
@@ -1523,6 +1538,15 @@ func TestIsZero(t *testing.T) {
        }()
 }
 
+func TestInternalIsZero(t *testing.T) {
+       b := make([]byte, 512)
+       for a := 0; a < 8; a++ {
+               for i := 256 + 7; i <= 512-a; i++ {
+                       InternalIsZero(b[a : a+i])
+               }
+       }
+}
+
 func TestInterfaceExtraction(t *testing.T) {
        var s struct {
                W io.Writer
@@ -1706,6 +1730,12 @@ func TestChan(t *testing.T) {
                if i, ok := cv.Recv(); i.Int() != 0 || ok {
                        t.Errorf("after close Recv %d, %t", i.Int(), ok)
                }
+               // Closing a read-only channel
+               shouldPanic("", func() {
+                       c := make(<-chan int, 1)
+                       cv := ValueOf(c)
+                       cv.Close()
+               })
        }
 
        // check creation of unbuffered channel
@@ -3281,14 +3311,16 @@ type unexpI interface {
        f() (int32, int8)
 }
 
-var unexpi unexpI = new(unexp)
-
 func TestUnexportedMethods(t *testing.T) {
-       typ := TypeOf(unexpi)
-
+       typ := TypeOf(new(unexp))
        if got := typ.NumMethod(); got != 0 {
                t.Errorf("NumMethod=%d, want 0 satisfied methods", got)
        }
+
+       typ = TypeOf((*unexpI)(nil))
+       if got := typ.Elem().NumMethod(); got != 1 {
+               t.Errorf("NumMethod=%d, want 1 satisfied methods", got)
+       }
 }
 
 type InnerInt struct {
@@ -7009,10 +7041,18 @@ func verifyGCBits(t *testing.T, typ Type, bits []byte) {
        // e.g. with rep(2, lit(1, 0)).
        bits = trimBitmap(bits)
 
-       if !bytes.Equal(heapBits, bits) {
-               _, _, line, _ := runtime.Caller(1)
-               t.Errorf("line %d: heapBits incorrect for %v\nhave %v\nwant %v", line, typ, heapBits, bits)
+       if bytes.HasPrefix(heapBits, bits) {
+               // Just the prefix matching is OK.
+               //
+               // The Go runtime's pointer/scalar iterator generates pointers beyond
+               // the size of the type, up to the size of the size class. This space
+               // is safe for the GC to scan since it's zero, and GCBits checks to
+               // make sure that's true. But we need to handle the fact that the bitmap
+               // may be larger than we expect.
+               return
        }
+       _, _, line, _ := runtime.Caller(1)
+       t.Errorf("line %d: heapBits incorrect for %v\nhave %v\nwant %v", line, typ, heapBits, bits)
 }
 
 func verifyGCBitsSlice(t *testing.T, typ Type, cap int, bits []byte) {
@@ -7021,15 +7061,20 @@ func verifyGCBitsSlice(t *testing.T, typ Type, cap int, bits []byte) {
        // repeat a bitmap for a small array or executing a repeat in
        // a GC program.
        val := MakeSlice(typ, 0, cap)
-       data := NewAt(ArrayOf(cap, typ), val.UnsafePointer())
+       data := NewAt(typ.Elem(), val.UnsafePointer())
        heapBits := GCBits(data.Interface())
        // Repeat the bitmap for the slice size, trimming scalars in
        // the last element.
        bits = trimBitmap(rep(cap, bits))
-       if !bytes.Equal(heapBits, bits) {
-               _, _, line, _ := runtime.Caller(1)
-               t.Errorf("line %d: heapBits incorrect for make(%v, 0, %v)\nhave %v\nwant %v", line, typ, cap, heapBits, bits)
+       if bytes.Equal(heapBits, bits) {
+               return
+       }
+       if len(heapBits) > len(bits) && bytes.Equal(heapBits[:len(bits)], bits) {
+               // Just the prefix matching is OK.
+               return
        }
+       _, _, line, _ := runtime.Caller(1)
+       t.Errorf("line %d: heapBits incorrect for make(%v, 0, %v)\nhave %v\nwant %v", line, typ, cap, heapBits, bits)
 }
 
 func TestGCBits(t *testing.T) {