Skip to content

Commit d4b46b0

Browse files
ianlancetaylorgopherbot
authored andcommitted
encoding/gob: use reflect.TypeFor for known types
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 <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Rob Pike <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]>
1 parent 9138743 commit d4b46b0

File tree

4 files changed

+20
-20
lines changed

4 files changed

+20
-20
lines changed

src/encoding/gob/decode.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ func (dec *Decoder) getDecEnginePtr(remoteId typeId, ut *userTypeInfo) (enginePt
11971197
// emptyStruct is the type we compile into when ignoring a struct value.
11981198
type emptyStruct struct{}
11991199

1200-
var emptyStructType = reflect.TypeOf((*emptyStruct)(nil)).Elem()
1200+
var emptyStructType = reflect.TypeFor[emptyStruct]()
12011201

12021202
// getIgnoreEnginePtr returns the engine for the specified type when the value is to be discarded.
12031203
func (dec *Decoder) getIgnoreEnginePtr(wireId typeId) (enginePtr **decEngine, err error) {

src/encoding/gob/gobencdec_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ func TestIgnoreDepthLimit(t *testing.T) {
806806
defer func() { maxIgnoreNestingDepth = oldNestingDepth }()
807807
b := new(bytes.Buffer)
808808
enc := NewEncoder(b)
809-
typ := reflect.TypeOf(int(0))
809+
typ := reflect.TypeFor[int]()
810810
nested := reflect.ArrayOf(1, typ)
811811
for i := 0; i < 100; i++ {
812812
nested = reflect.ArrayOf(1, nested)

src/encoding/gob/type.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ func validUserType(rt reflect.Type) (*userTypeInfo, error) {
103103
}
104104

105105
var (
106-
gobEncoderInterfaceType = reflect.TypeOf((*GobEncoder)(nil)).Elem()
107-
gobDecoderInterfaceType = reflect.TypeOf((*GobDecoder)(nil)).Elem()
108-
binaryMarshalerInterfaceType = reflect.TypeOf((*encoding.BinaryMarshaler)(nil)).Elem()
109-
binaryUnmarshalerInterfaceType = reflect.TypeOf((*encoding.BinaryUnmarshaler)(nil)).Elem()
110-
textMarshalerInterfaceType = reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem()
111-
textUnmarshalerInterfaceType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
112-
113-
wireTypeType = reflect.TypeOf((*wireType)(nil)).Elem()
106+
gobEncoderInterfaceType = reflect.TypeFor[GobEncoder]()
107+
gobDecoderInterfaceType = reflect.TypeFor[GobDecoder]()
108+
binaryMarshalerInterfaceType = reflect.TypeFor[encoding.BinaryMarshaler]()
109+
binaryUnmarshalerInterfaceType = reflect.TypeFor[encoding.BinaryUnmarshaler]()
110+
textMarshalerInterfaceType = reflect.TypeFor[encoding.TextMarshaler]()
111+
textUnmarshalerInterfaceType = reflect.TypeFor[encoding.TextUnmarshaler]()
112+
113+
wireTypeType = reflect.TypeFor[wireType]()
114114
)
115115

116116
// implementsInterface reports whether the type implements the
@@ -270,12 +270,12 @@ var wireTypeUserInfo *userTypeInfo // userTypeInfo of wireType
270270
func init() {
271271
// Some magic numbers to make sure there are no surprises.
272272
checkId(16, tWireType)
273-
checkId(17, mustGetTypeInfo(reflect.TypeOf((*arrayType)(nil)).Elem()).id)
274-
checkId(18, mustGetTypeInfo(reflect.TypeOf((*CommonType)(nil)).Elem()).id)
275-
checkId(19, mustGetTypeInfo(reflect.TypeOf((*sliceType)(nil)).Elem()).id)
276-
checkId(20, mustGetTypeInfo(reflect.TypeOf((*structType)(nil)).Elem()).id)
277-
checkId(21, mustGetTypeInfo(reflect.TypeOf((*fieldType)(nil)).Elem()).id)
278-
checkId(23, mustGetTypeInfo(reflect.TypeOf((*mapType)(nil)).Elem()).id)
273+
checkId(17, mustGetTypeInfo(reflect.TypeFor[arrayType]()).id)
274+
checkId(18, mustGetTypeInfo(reflect.TypeFor[CommonType]()).id)
275+
checkId(19, mustGetTypeInfo(reflect.TypeFor[sliceType]()).id)
276+
checkId(20, mustGetTypeInfo(reflect.TypeFor[structType]()).id)
277+
checkId(21, mustGetTypeInfo(reflect.TypeFor[fieldType]()).id)
278+
checkId(23, mustGetTypeInfo(reflect.TypeFor[mapType]()).id)
279279

280280
copy(builtinIdToTypeSlice[:], idToType)
281281

src/encoding/gob/type_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ func TestBasic(t *testing.T) {
4949

5050
// Reregister some basic types to check registration is idempotent.
5151
func TestReregistration(t *testing.T) {
52-
newtyp := getTypeUnlocked("int", reflect.TypeOf(int(0)))
52+
newtyp := getTypeUnlocked("int", reflect.TypeFor[int]())
5353
if newtyp != tInt.gobType() {
5454
t.Errorf("reregistration of %s got new type", newtyp.string())
5555
}
56-
newtyp = getTypeUnlocked("uint", reflect.TypeOf(uint(0)))
56+
newtyp = getTypeUnlocked("uint", reflect.TypeFor[uint]())
5757
if newtyp != tUint.gobType() {
5858
t.Errorf("reregistration of %s got new type", newtyp.string())
5959
}
60-
newtyp = getTypeUnlocked("string", reflect.TypeOf("hello"))
60+
newtyp = getTypeUnlocked("string", reflect.TypeFor[string]())
6161
if newtyp != tString.gobType() {
6262
t.Errorf("reregistration of %s got new type", newtyp.string())
6363
}
@@ -145,7 +145,7 @@ type Foo struct {
145145
}
146146

147147
func TestStructType(t *testing.T) {
148-
sstruct := getTypeUnlocked("Foo", reflect.TypeOf(Foo{}))
148+
sstruct := getTypeUnlocked("Foo", reflect.TypeFor[Foo]())
149149
str := sstruct.string()
150150
// If we can print it correctly, we built it correctly.
151151
expected := "Foo = struct { A int; B int; C string; D bytes; E float; F float; G Bar = struct { X string; }; H Bar; I Foo; }"

0 commit comments

Comments
 (0)