]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: make PtrTo(FuncOf(...)) not crash
authorRuss Cox <rsc@golang.org>
Fri, 15 May 2015 18:46:20 +0000 (14:46 -0400)
committerRuss Cox <rsc@golang.org>
Sat, 16 May 2015 00:51:05 +0000 (00:51 +0000)
Change-Id: Ie67e295bf327126dfdc75b73979fe33fbcb79ad9
Reviewed-on: https://go-review.googlesource.com/10150
Reviewed-by: Austin Clements <austin@google.com>
src/reflect/all_test.go
src/reflect/type.go

index 9214577c2ef9bf963a58785833dca35adb6d4116..9a99f742d6fa338e99c8ec5a5e89719a17f4b59c 100644 (file)
@@ -4618,7 +4618,7 @@ func TestGCBits(t *testing.T) {
        verifyGCBits(t, ChanOf(BothDir, ArrayOf(100, Tscalar)), lit(1))
 
        verifyGCBits(t, TypeOf((func([100]Xscalarptr))(nil)), lit(1))
-       //verifyGCBits(t, FuncOf([]Type{ArrayOf(100, Tscalarptr)}, nil, false), lit(1))
+       verifyGCBits(t, FuncOf([]Type{ArrayOf(100, Tscalarptr)}, nil, false), lit(1))
 
        verifyGCBits(t, TypeOf((map[[100]Xscalarptr]Xscalar)(nil)), lit(1))
        verifyGCBits(t, MapOf(ArrayOf(100, Tscalarptr), Tscalar), lit(1))
@@ -4643,3 +4643,24 @@ func TestGCBits(t *testing.T) {
 func rep(n int, b []byte) []byte { return bytes.Repeat(b, n) }
 func join(b ...[]byte) []byte    { return bytes.Join(b, nil) }
 func lit(x ...byte) []byte       { return x }
+
+func TestTypeOfTypeOf(t *testing.T) {
+       // Check that all the type constructors return concrete *rtype implementations.
+       // It's difficult to test directly because the reflect package is only at arm's length.
+       // The easiest thing to do is just call a function that crashes if it doesn't get an *rtype.
+       check := func(name string, typ Type) {
+               if underlying := TypeOf(typ).String(); underlying != "*reflect.rtype" {
+                       t.Errorf("%v returned %v, not *reflect.rtype", name, underlying)
+               }
+       }
+
+       type T struct{ int }
+       check("TypeOf", TypeOf(T{}))
+
+       check("ArrayOf", ArrayOf(10, TypeOf(T{})))
+       check("ChanOf", ChanOf(BothDir, TypeOf(T{})))
+       check("FuncOf", FuncOf([]Type{TypeOf(T{})}, nil, false))
+       check("MapOf", MapOf(TypeOf(T{}), TypeOf(T{})))
+       check("PtrTo", PtrTo(TypeOf(T{})))
+       check("SliceOf", SliceOf(TypeOf(T{})))
+}
index f39ba52a4262153f2a841d6f4b760cab6eaa4345..e55a0d146cfb77706a34e9180f8cc6923f7b1351 100644 (file)
@@ -1609,7 +1609,7 @@ func FuncOf(in, out []Type, variadic bool) Type {
        ft.ptrToThis = nil
        funcLookupCache.m[hash] = append(funcLookupCache.m[hash], &ft.rtype)
 
-       return ft
+       return &ft.rtype
 }
 
 // funcStr builds a string representation of a funcType.