]> Cypherpunks.ru repositories - gostls13.git/commitdiff
encoding/gob: use reflect.TypeFor for known types
authorIan Lance Taylor <iant@golang.org>
Mon, 31 Jul 2023 17:30:27 +0000 (10:30 -0700)
committerGopher Robot <gobot@golang.org>
Tue, 1 Aug 2023 14:26:13 +0000 (14:26 +0000)
This avoids several mildly confusing Elem calls.

For #60088

Change-Id: If7b83d2ab10537c7e886a035b43cb272130c1669
Reviewed-on: https://go-review.googlesource.com/c/go/+/514455
Run-TryBot: Ian Lance Taylor <iant@google.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Rob Pike <r@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>

src/encoding/gob/decode.go
src/encoding/gob/gobencdec_test.go
src/encoding/gob/type.go
src/encoding/gob/type_test.go

index 66c76a070985e8775485359585354c0c87141c69..684505bf9019ba84ea991c0a524f545ec8a1b1ef 100644 (file)
@@ -1197,7 +1197,7 @@ func (dec *Decoder) getDecEnginePtr(remoteId typeId, ut *userTypeInfo) (enginePt
 // emptyStruct is the type we compile into when ignoring a struct value.
 type emptyStruct struct{}
 
-var emptyStructType = reflect.TypeOf((*emptyStruct)(nil)).Elem()
+var emptyStructType = reflect.TypeFor[emptyStruct]()
 
 // getIgnoreEnginePtr returns the engine for the specified type when the value is to be discarded.
 func (dec *Decoder) getIgnoreEnginePtr(wireId typeId) (enginePtr **decEngine, err error) {
index 6fefd3675664b68e04fbf60df08a7a02ff61d9a9..ae806fc39a21fcae595c088b3465170b2c35b08a 100644 (file)
@@ -806,7 +806,7 @@ func TestIgnoreDepthLimit(t *testing.T) {
        defer func() { maxIgnoreNestingDepth = oldNestingDepth }()
        b := new(bytes.Buffer)
        enc := NewEncoder(b)
-       typ := reflect.TypeOf(int(0))
+       typ := reflect.TypeFor[int]()
        nested := reflect.ArrayOf(1, typ)
        for i := 0; i < 100; i++ {
                nested = reflect.ArrayOf(1, nested)
index 205a0b369450c9d964752cbc688ac6cf94d2b694..acc36425bd4871d9af940e047ff2a45a82e62ad9 100644 (file)
@@ -103,14 +103,14 @@ func validUserType(rt reflect.Type) (*userTypeInfo, error) {
 }
 
 var (
-       gobEncoderInterfaceType        = reflect.TypeOf((*GobEncoder)(nil)).Elem()
-       gobDecoderInterfaceType        = reflect.TypeOf((*GobDecoder)(nil)).Elem()
-       binaryMarshalerInterfaceType   = reflect.TypeOf((*encoding.BinaryMarshaler)(nil)).Elem()
-       binaryUnmarshalerInterfaceType = reflect.TypeOf((*encoding.BinaryUnmarshaler)(nil)).Elem()
-       textMarshalerInterfaceType     = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
-       textUnmarshalerInterfaceType   = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
-
-       wireTypeType = reflect.TypeOf((*wireType)(nil)).Elem()
+       gobEncoderInterfaceType        = reflect.TypeFor[GobEncoder]()
+       gobDecoderInterfaceType        = reflect.TypeFor[GobDecoder]()
+       binaryMarshalerInterfaceType   = reflect.TypeFor[encoding.BinaryMarshaler]()
+       binaryUnmarshalerInterfaceType = reflect.TypeFor[encoding.BinaryUnmarshaler]()
+       textMarshalerInterfaceType     = reflect.TypeFor[encoding.TextMarshaler]()
+       textUnmarshalerInterfaceType   = reflect.TypeFor[encoding.TextUnmarshaler]()
+
+       wireTypeType = reflect.TypeFor[wireType]()
 )
 
 // implementsInterface reports whether the type implements the
@@ -270,12 +270,12 @@ var wireTypeUserInfo *userTypeInfo // userTypeInfo of wireType
 func init() {
        // Some magic numbers to make sure there are no surprises.
        checkId(16, tWireType)
-       checkId(17, mustGetTypeInfo(reflect.TypeOf((*arrayType)(nil)).Elem()).id)
-       checkId(18, mustGetTypeInfo(reflect.TypeOf((*CommonType)(nil)).Elem()).id)
-       checkId(19, mustGetTypeInfo(reflect.TypeOf((*sliceType)(nil)).Elem()).id)
-       checkId(20, mustGetTypeInfo(reflect.TypeOf((*structType)(nil)).Elem()).id)
-       checkId(21, mustGetTypeInfo(reflect.TypeOf((*fieldType)(nil)).Elem()).id)
-       checkId(23, mustGetTypeInfo(reflect.TypeOf((*mapType)(nil)).Elem()).id)
+       checkId(17, mustGetTypeInfo(reflect.TypeFor[arrayType]()).id)
+       checkId(18, mustGetTypeInfo(reflect.TypeFor[CommonType]()).id)
+       checkId(19, mustGetTypeInfo(reflect.TypeFor[sliceType]()).id)
+       checkId(20, mustGetTypeInfo(reflect.TypeFor[structType]()).id)
+       checkId(21, mustGetTypeInfo(reflect.TypeFor[fieldType]()).id)
+       checkId(23, mustGetTypeInfo(reflect.TypeFor[mapType]()).id)
 
        copy(builtinIdToTypeSlice[:], idToType)
 
index f5f8db8bcb4a3db5a9c402bdfe4c39212272f7a3..8d4c6d7ff9ac14887a113a517d8a0883b0491bb4 100644 (file)
@@ -49,15 +49,15 @@ func TestBasic(t *testing.T) {
 
 // Reregister some basic types to check registration is idempotent.
 func TestReregistration(t *testing.T) {
-       newtyp := getTypeUnlocked("int", reflect.TypeOf(int(0)))
+       newtyp := getTypeUnlocked("int", reflect.TypeFor[int]())
        if newtyp != tInt.gobType() {
                t.Errorf("reregistration of %s got new type", newtyp.string())
        }
-       newtyp = getTypeUnlocked("uint", reflect.TypeOf(uint(0)))
+       newtyp = getTypeUnlocked("uint", reflect.TypeFor[uint]())
        if newtyp != tUint.gobType() {
                t.Errorf("reregistration of %s got new type", newtyp.string())
        }
-       newtyp = getTypeUnlocked("string", reflect.TypeOf("hello"))
+       newtyp = getTypeUnlocked("string", reflect.TypeFor[string]())
        if newtyp != tString.gobType() {
                t.Errorf("reregistration of %s got new type", newtyp.string())
        }
@@ -145,7 +145,7 @@ type Foo struct {
 }
 
 func TestStructType(t *testing.T) {
-       sstruct := getTypeUnlocked("Foo", reflect.TypeOf(Foo{}))
+       sstruct := getTypeUnlocked("Foo", reflect.TypeFor[Foo]())
        str := sstruct.string()
        // If we can print it correctly, we built it correctly.
        expected := "Foo = struct { A int; B int; C string; D bytes; E float; F float; G Bar = struct { X string; }; H Bar; I Foo; }"