Skip to content

Commit

Permalink
all: use reflect.{Pointer,PointerTo}
Browse files Browse the repository at this point in the history
Updates #47651
Updates #48665

Change-Id: I69a87b45a5cad7a07fbd855040cd9935cf874554
Reviewed-on: https://go-review.googlesource.com/c/go/+/358454
Trust: Cuong Manh Le <[email protected]>
Run-TryBot: Cuong Manh Le <[email protected]>
TryBot-Result: Go Bot <[email protected]>
Reviewed-by: Brad Fitzpatrick <[email protected]>
  • Loading branch information
cuonglm committed Oct 26, 2021
1 parent a2b8c18 commit 283d8a3
Show file tree
Hide file tree
Showing 41 changed files with 150 additions and 147 deletions.
2 changes: 1 addition & 1 deletion src/cmd/fix/cftype.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func typefix(f *ast.File, badType func(string) bool) bool {
return
}
v := reflect.ValueOf(n)
if v.Type().Kind() != reflect.Ptr {
if v.Type().Kind() != reflect.Pointer {
return
}
if v.IsNil() {
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/fix/typecheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func typecheck1(cfg *TypeConfig, f interface{}, typeof map[interface{}]string, a
if n == nil {
return
}
if false && reflect.TypeOf(n).Kind() == reflect.Ptr { // debugging trace
if false && reflect.TypeOf(n).Kind() == reflect.Pointer { // debugging trace
defer func() {
if t := typeof[n]; t != "" {
pos := fset.Position(n.(ast.Node).Pos())
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/gofmt/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func subst(m map[string]reflect.Value, pattern reflect.Value, pos reflect.Value)
}
return v

case reflect.Ptr:
case reflect.Pointer:
v := reflect.New(p.Type()).Elem()
if elem := p.Elem(); elem.IsValid() {
v.Set(subst(m, elem, pos).Addr())
Expand Down
6 changes: 3 additions & 3 deletions src/database/sql/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func convertAssignRows(dest, src interface{}, rows *Rows) error {
}

dpv := reflect.ValueOf(dest)
if dpv.Kind() != reflect.Ptr {
if dpv.Kind() != reflect.Pointer {
return errors.New("destination not a pointer")
}
if dpv.IsNil() {
Expand Down Expand Up @@ -419,7 +419,7 @@ func convertAssignRows(dest, src interface{}, rows *Rows) error {
// This also allows scanning into user defined types such as "type Int int64".
// For symmetry, also check for string destination types.
switch dv.Kind() {
case reflect.Ptr:
case reflect.Pointer:
if src == nil {
dv.Set(reflect.Zero(dv.Type()))
return nil
Expand Down Expand Up @@ -551,7 +551,7 @@ var valuerReflectType = reflect.TypeOf((*driver.Valuer)(nil)).Elem()
//
// This function is mirrored in the database/sql/driver package.
func callValuerValue(vr driver.Valuer) (v driver.Value, err error) {
if rv := reflect.ValueOf(vr); rv.Kind() == reflect.Ptr &&
if rv := reflect.ValueOf(vr); rv.Kind() == reflect.Pointer &&
rv.IsNil() &&
rv.Type().Elem().Implements(valuerReflectType) {
return nil, nil
Expand Down
4 changes: 2 additions & 2 deletions src/database/sql/driver/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ var valuerReflectType = reflect.TypeOf((*Valuer)(nil)).Elem()
//
// This function is mirrored in the database/sql package.
func callValuerValue(vr Valuer) (v Value, err error) {
if rv := reflect.ValueOf(vr); rv.Kind() == reflect.Ptr &&
if rv := reflect.ValueOf(vr); rv.Kind() == reflect.Pointer &&
rv.IsNil() &&
rv.Type().Elem().Implements(valuerReflectType) {
return nil, nil
Expand Down Expand Up @@ -256,7 +256,7 @@ func (defaultConverter) ConvertValue(v interface{}) (Value, error) {

rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Ptr:
case reflect.Pointer:
// indirect pointers
if rv.IsNil() {
return nil, nil
Expand Down
4 changes: 2 additions & 2 deletions src/encoding/asn1/asn1.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ func (e *invalidUnmarshalError) Error() string {
return "asn1: Unmarshal recipient value is nil"
}

if e.Type.Kind() != reflect.Ptr {
if e.Type.Kind() != reflect.Pointer {
return "asn1: Unmarshal recipient value is non-pointer " + e.Type.String()
}
return "asn1: Unmarshal recipient value is nil " + e.Type.String()
Expand All @@ -1111,7 +1111,7 @@ func (e *invalidUnmarshalError) Error() string {
// top-level element. The form of the params is the same as the field tags.
func UnmarshalWithParams(b []byte, val interface{}, params string) (rest []byte, err error) {
v := reflect.ValueOf(val)
if v.Kind() != reflect.Ptr || v.IsNil() {
if v.Kind() != reflect.Pointer || v.IsNil() {
return nil, &invalidUnmarshalError{reflect.TypeOf(val)}
}
offset, err := parseField(v.Elem(), b, 0, parseFieldParameters(params))
Expand Down
2 changes: 1 addition & 1 deletion src/encoding/binary/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func Read(r io.Reader, order ByteOrder, data interface{}) error {
v := reflect.ValueOf(data)
size := -1
switch v.Kind() {
case reflect.Ptr:
case reflect.Pointer:
v = v.Elem()
size = dataSize(v)
case reflect.Slice:
Expand Down
14 changes: 7 additions & 7 deletions src/encoding/gob/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func ignoreTwoUints(i *decInstr, state *decoderState, v reflect.Value) {
// The callers to the individual decoders are expected to have used decAlloc.
// The individual decoders don't need to it.
func decAlloc(v reflect.Value) reflect.Value {
for v.Kind() == reflect.Ptr {
for v.Kind() == reflect.Pointer {
if v.IsNil() {
v.Set(reflect.New(v.Type().Elem()))
}
Expand Down Expand Up @@ -464,7 +464,7 @@ func (dec *Decoder) decodeStruct(engine *decEngine, value reflect.Value) {
if instr.index != nil {
// Otherwise the field is unknown to us and instr.op is an ignore op.
field = value.FieldByIndex(instr.index)
if field.Kind() == reflect.Ptr {
if field.Kind() == reflect.Pointer {
field = decAlloc(field)
}
}
Expand Down Expand Up @@ -518,7 +518,7 @@ func (dec *Decoder) decodeArrayHelper(state *decoderState, value reflect.Value,
return
}
instr := &decInstr{elemOp, 0, nil, ovfl}
isPtr := value.Type().Elem().Kind() == reflect.Ptr
isPtr := value.Type().Elem().Kind() == reflect.Pointer
for i := 0; i < length; i++ {
if state.b.Len() == 0 {
errorf("decoding array or slice: length exceeds input size (%d elements)", length)
Expand Down Expand Up @@ -561,8 +561,8 @@ func (dec *Decoder) decodeMap(mtyp reflect.Type, state *decoderState, value refl
if value.IsNil() {
value.Set(reflect.MakeMapWithSize(mtyp, n))
}
keyIsPtr := mtyp.Key().Kind() == reflect.Ptr
elemIsPtr := mtyp.Elem().Kind() == reflect.Ptr
keyIsPtr := mtyp.Key().Kind() == reflect.Pointer
elemIsPtr := mtyp.Elem().Kind() == reflect.Pointer
keyInstr := &decInstr{keyOp, 0, nil, ovfl}
elemInstr := &decInstr{elemOp, 0, nil, ovfl}
keyP := reflect.New(mtyp.Key())
Expand Down Expand Up @@ -945,7 +945,7 @@ func (dec *Decoder) decIgnoreOpFor(wireId typeId, inProgress map[typeId]*decOp)
func (dec *Decoder) gobDecodeOpFor(ut *userTypeInfo) *decOp {
rcvrType := ut.user
if ut.decIndir == -1 {
rcvrType = reflect.PtrTo(rcvrType)
rcvrType = reflect.PointerTo(rcvrType)
} else if ut.decIndir > 0 {
for i := int8(0); i < ut.decIndir; i++ {
rcvrType = rcvrType.Elem()
Expand All @@ -954,7 +954,7 @@ func (dec *Decoder) gobDecodeOpFor(ut *userTypeInfo) *decOp {
var op decOp
op = func(i *decInstr, state *decoderState, value reflect.Value) {
// We now have the base type. We need its address if the receiver is a pointer.
if value.Kind() != reflect.Ptr && rcvrType.Kind() == reflect.Ptr {
if value.Kind() != reflect.Pointer && rcvrType.Kind() == reflect.Pointer {
value = value.Addr()
}
state.dec.decodeGobDecoder(ut, state, value)
Expand Down
4 changes: 2 additions & 2 deletions src/encoding/gob/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (dec *Decoder) Decode(e interface{}) error {
value := reflect.ValueOf(e)
// If e represents a value as opposed to a pointer, the answer won't
// get back to the caller. Make sure it's a pointer.
if value.Type().Kind() != reflect.Ptr {
if value.Type().Kind() != reflect.Pointer {
dec.err = errors.New("gob: attempt to decode into a non-pointer")
return dec.err
}
Expand All @@ -208,7 +208,7 @@ func (dec *Decoder) Decode(e interface{}) error {
// does not modify v.
func (dec *Decoder) DecodeValue(v reflect.Value) error {
if v.IsValid() {
if v.Kind() == reflect.Ptr && !v.IsNil() {
if v.Kind() == reflect.Pointer && !v.IsNil() {
// That's okay, we'll store through the pointer.
} else if !v.CanSet() {
return errors.New("gob: DecodeValue of unassignable value")
Expand Down
8 changes: 4 additions & 4 deletions src/encoding/gob/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func valid(v reflect.Value) bool {
switch v.Kind() {
case reflect.Invalid:
return false
case reflect.Ptr:
case reflect.Pointer:
return !v.IsNil()
}
return true
Expand Down Expand Up @@ -386,7 +386,7 @@ func (enc *Encoder) encodeInterface(b *encBuffer, iv reflect.Value) {
// Gobs can encode nil interface values but not typed interface
// values holding nil pointers, since nil pointers point to no value.
elem := iv.Elem()
if elem.Kind() == reflect.Ptr && elem.IsNil() {
if elem.Kind() == reflect.Pointer && elem.IsNil() {
errorf("gob: cannot encode nil pointer of type %s inside interface", iv.Elem().Type())
}
state := enc.newEncoderState(b)
Expand Down Expand Up @@ -446,7 +446,7 @@ func isZero(val reflect.Value) bool {
return !val.Bool()
case reflect.Complex64, reflect.Complex128:
return val.Complex() == 0
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Ptr:
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Pointer:
return val.IsNil()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return val.Int() == 0
Expand Down Expand Up @@ -600,7 +600,7 @@ func encOpFor(rt reflect.Type, inProgress map[reflect.Type]*encOp, building map[
func gobEncodeOpFor(ut *userTypeInfo) (*encOp, int) {
rt := ut.user
if ut.encIndir == -1 {
rt = reflect.PtrTo(rt)
rt = reflect.PointerTo(rt)
} else if ut.encIndir > 0 {
for i := int8(0); i < ut.encIndir; i++ {
rt = rt.Elem()
Expand Down
2 changes: 1 addition & 1 deletion src/encoding/gob/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (enc *Encoder) EncodeValue(value reflect.Value) error {
if value.Kind() == reflect.Invalid {
return errors.New("gob: cannot encode nil value")
}
if value.Kind() == reflect.Ptr && value.IsNil() {
if value.Kind() == reflect.Pointer && value.IsNil() {
panic("gob: cannot encode nil pointer of type " + value.Type().String())
}

Expand Down
12 changes: 6 additions & 6 deletions src/encoding/gob/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func validUserType(rt reflect.Type) (*userTypeInfo, error) {
slowpoke := ut.base // walks half as fast as ut.base
for {
pt := ut.base
if pt.Kind() != reflect.Ptr {
if pt.Kind() != reflect.Pointer {
break
}
ut.base = pt.Elem()
Expand Down Expand Up @@ -126,7 +126,7 @@ func implementsInterface(typ, gobEncDecType reflect.Type) (success bool, indir i
if rt.Implements(gobEncDecType) {
return true, indir
}
if p := rt; p.Kind() == reflect.Ptr {
if p := rt; p.Kind() == reflect.Pointer {
indir++
if indir > 100 { // insane number of indirections
return false, 0
Expand All @@ -137,9 +137,9 @@ func implementsInterface(typ, gobEncDecType reflect.Type) (success bool, indir i
break
}
// No luck yet, but if this is a base type (non-pointer), the pointer might satisfy.
if typ.Kind() != reflect.Ptr {
if typ.Kind() != reflect.Pointer {
// Not a pointer, but does the pointer work?
if reflect.PtrTo(typ).Implements(gobEncDecType) {
if reflect.PointerTo(typ).Implements(gobEncDecType) {
return true, -1
}
}
Expand Down Expand Up @@ -569,7 +569,7 @@ func isSent(field *reflect.StructField) bool {
// If the field is a chan or func or pointer thereto, don't send it.
// That is, treat it like an unexported field.
typ := field.Type
for typ.Kind() == reflect.Ptr {
for typ.Kind() == reflect.Pointer {
typ = typ.Elem()
}
if typ.Kind() == reflect.Chan || typ.Kind() == reflect.Func {
Expand Down Expand Up @@ -842,7 +842,7 @@ func Register(value interface{}) {
// Dereference one pointer looking for a named type.
star := ""
if rt.Name() == "" {
if pt := rt; pt.Kind() == reflect.Ptr {
if pt := rt; pt.Kind() == reflect.Pointer {
star = "*"
// NOTE: The following line should be rt = pt.Elem() to implement
// what the comment above claims, but fixing it would break compatibility
Expand Down
2 changes: 1 addition & 1 deletion src/encoding/gob/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func TestRegistrationNaming(t *testing.T) {
t.Errorf("nameToConcreteType[%q] = %v, want %v", tc.name, ct, tct)
}
// concreteTypeToName is keyed off the base type.
if tct.Kind() == reflect.Ptr {
if tct.Kind() == reflect.Pointer {
tct = tct.Elem()
}
if n, _ := concreteTypeToName.Load(tct); n != tc.name {
Expand Down
18 changes: 9 additions & 9 deletions src/encoding/json/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ func (e *InvalidUnmarshalError) Error() string {
return "json: Unmarshal(nil)"
}

if e.Type.Kind() != reflect.Ptr {
if e.Type.Kind() != reflect.Pointer {
return "json: Unmarshal(non-pointer " + e.Type.String() + ")"
}
return "json: Unmarshal(nil " + e.Type.String() + ")"
}

func (d *decodeState) unmarshal(v interface{}) error {
rv := reflect.ValueOf(v)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
if rv.Kind() != reflect.Pointer || rv.IsNil() {
return &InvalidUnmarshalError{reflect.TypeOf(v)}
}

Expand Down Expand Up @@ -440,7 +440,7 @@ func indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnm
// If v is a named type and is addressable,
// start with its address, so that if the type has pointer methods,
// we find them.
if v.Kind() != reflect.Ptr && v.Type().Name() != "" && v.CanAddr() {
if v.Kind() != reflect.Pointer && v.Type().Name() != "" && v.CanAddr() {
haveAddr = true
v = v.Addr()
}
Expand All @@ -449,14 +449,14 @@ func indirect(v reflect.Value, decodingNull bool) (Unmarshaler, encoding.TextUnm
// usefully addressable.
if v.Kind() == reflect.Interface && !v.IsNil() {
e := v.Elem()
if e.Kind() == reflect.Ptr && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Ptr) {
if e.Kind() == reflect.Pointer && !e.IsNil() && (!decodingNull || e.Elem().Kind() == reflect.Pointer) {
haveAddr = false
v = e
continue
}
}

if v.Kind() != reflect.Ptr {
if v.Kind() != reflect.Pointer {
break
}

Expand Down Expand Up @@ -641,7 +641,7 @@ func (d *decodeState) object(v reflect.Value) error {
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
default:
if !reflect.PtrTo(t.Key()).Implements(textUnmarshalerType) {
if !reflect.PointerTo(t.Key()).Implements(textUnmarshalerType) {
d.saveError(&UnmarshalTypeError{Value: "object", Type: t, Offset: int64(d.off)})
d.skip()
return nil
Expand Down Expand Up @@ -717,7 +717,7 @@ func (d *decodeState) object(v reflect.Value) error {
subv = v
destring = f.quoted
for _, i := range f.index {
if subv.Kind() == reflect.Ptr {
if subv.Kind() == reflect.Pointer {
if subv.IsNil() {
// If a struct embeds a pointer to an unexported type,
// it is not possible to set a newly allocated value
Expand Down Expand Up @@ -782,7 +782,7 @@ func (d *decodeState) object(v reflect.Value) error {
kt := t.Key()
var kv reflect.Value
switch {
case reflect.PtrTo(kt).Implements(textUnmarshalerType):
case reflect.PointerTo(kt).Implements(textUnmarshalerType):
kv = reflect.New(kt)
if err := d.literalStore(item, kv, true); err != nil {
return err
Expand Down Expand Up @@ -907,7 +907,7 @@ func (d *decodeState) literalStore(item []byte, v reflect.Value, fromQuoted bool
break
}
switch v.Kind() {
case reflect.Interface, reflect.Ptr, reflect.Map, reflect.Slice:
case reflect.Interface, reflect.Pointer, reflect.Map, reflect.Slice:
v.Set(reflect.Zero(v.Type()))
// otherwise, ignore null for primitives/string
}
Expand Down
2 changes: 1 addition & 1 deletion src/encoding/json/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1103,7 +1103,7 @@ func TestUnmarshal(t *testing.T) {
}

typ := reflect.TypeOf(tt.ptr)
if typ.Kind() != reflect.Ptr {
if typ.Kind() != reflect.Pointer {
t.Errorf("#%d: unmarshalTest.ptr %T is not a pointer type", i, tt.ptr)
continue
}
Expand Down
Loading

0 comments on commit 283d8a3

Please sign in to comment.