Skip to content

Commit

Permalink
refect: rename Ptr Kind to Pointer (but keep Ptr)
Browse files Browse the repository at this point in the history
reflect.Ptr didn't match reflect.UnsafePointer or unsafe.Pointer
so rename it to reflect.Pointer. Keep reflect.Ptr for compatibility.

Likewise with PtrTo.

Change to use it in std will come in a subsequent CL.

Fixes #47651

Change-Id: I5d4abe2b2fe10948bd68bb12c557165bedffbcba
Reviewed-on: https://go-review.googlesource.com/c/go/+/341333
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Joe Tsai <joetsai@digital-static.net>
Reviewed-by: Robert Griesemer <gri@golang.org>
  • Loading branch information
bradfitz committed Sep 3, 2021
1 parent 9f69a44 commit 17910ed
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 24 deletions.
4 changes: 3 additions & 1 deletion src/internal/reflectlite/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,15 @@ const (
Func
Interface
Map
Ptr
Pointer
Slice
String
Struct
UnsafePointer
)

const Ptr = Pointer

// tflag is used by an rtype to signal what extra type information is
// available in the memory directly following the rtype value.
//
Expand Down
8 changes: 4 additions & 4 deletions src/internal/reflectlite/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (f flag) ro() flag {
}

// pointer returns the underlying pointer represented by v.
// v.Kind() must be Ptr, Map, Chan, Func, or UnsafePointer
// v.Kind() must be Pointer, Map, Chan, Func, or UnsafePointer
func (v Value) pointer() unsafe.Pointer {
if v.typ.size != goarch.PtrSize || !v.typ.pointers() {
panic("can't call pointer on a non-pointer Value")
Expand Down Expand Up @@ -220,7 +220,7 @@ func (v Value) CanSet() bool {

// Elem returns the value that the interface v contains
// or that the pointer v points to.
// It panics if v's Kind is not Interface or Ptr.
// It panics if v's Kind is not Interface or Pointer.
// It returns the zero Value if v is nil.
func (v Value) Elem() Value {
k := v.kind()
Expand All @@ -239,7 +239,7 @@ func (v Value) Elem() Value {
x.flag |= v.flag.ro()
}
return x
case Ptr:
case Pointer:
ptr := v.ptr
if v.flag&flagIndir != 0 {
ptr = *(*unsafe.Pointer)(ptr)
Expand Down Expand Up @@ -288,7 +288,7 @@ func valueInterface(v Value) interface{} {
func (v Value) IsNil() bool {
k := v.kind()
switch k {
case Chan, Func, Map, Ptr, UnsafePointer:
case Chan, Func, Map, Pointer, UnsafePointer:
// if v.flag&flagMethod != 0 {
// return false
// }
Expand Down
50 changes: 31 additions & 19 deletions src/reflect/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ type Type interface {
// Chan: ChanDir, Elem
// Func: In, NumIn, Out, NumOut, IsVariadic.
// Map: Key, Elem
// Ptr: Elem
// Pointer: Elem
// Slice: Elem
// Struct: Field, FieldByIndex, FieldByName, FieldByNameFunc, NumField

Expand Down Expand Up @@ -154,7 +154,7 @@ type Type interface {
IsVariadic() bool

// Elem returns a type's element type.
// It panics if the type's Kind is not Array, Chan, Map, Ptr, or Slice.
// It panics if the type's Kind is not Array, Chan, Map, Pointer, or Slice.
Elem() Type

// Field returns a struct type's i'th field.
Expand Down Expand Up @@ -261,13 +261,18 @@ const (
Func
Interface
Map
Ptr
Pointer
Slice
String
Struct
UnsafePointer
)

// Ptr is the old name for the Pointer kind.
//
// Deprecated: use the new spelling, Pointer.
const Ptr = Pointer

// tflag is used by an rtype to signal what extra type information is
// available in the memory directly following the rtype value.
//
Expand Down Expand Up @@ -658,7 +663,7 @@ var kindNames = []string{
Func: "func",
Interface: "interface",
Map: "map",
Ptr: "ptr",
Pointer: "ptr",
Slice: "slice",
String: "string",
Struct: "struct",
Expand Down Expand Up @@ -741,7 +746,7 @@ func (t *rtype) uncommon() *uncommonType {
switch t.Kind() {
case Struct:
return &(*structTypeUncommon)(unsafe.Pointer(t)).u
case Ptr:
case Pointer:
type u struct {
ptrType
u uncommonType
Expand Down Expand Up @@ -945,7 +950,7 @@ func (t *rtype) Elem() Type {
case Map:
tt := (*mapType)(unsafe.Pointer(t))
return toType(tt.elem)
case Ptr:
case Pointer:
tt := (*ptrType)(unsafe.Pointer(t))
return toType(tt.elem)
case Slice:
Expand Down Expand Up @@ -1265,7 +1270,7 @@ func (t *structType) FieldByIndex(index []int) (f StructField) {
for i, x := range index {
if i > 0 {
ft := f.Type
if ft.Kind() == Ptr && ft.Elem().Kind() == Struct {
if ft.Kind() == Pointer && ft.Elem().Kind() == Struct {
ft = ft.Elem()
}
f.Type = ft
Expand Down Expand Up @@ -1336,7 +1341,7 @@ func (t *structType) FieldByNameFunc(match func(string) bool) (result StructFiel
if f.embedded() {
// Embedded field of type T or *T.
ntyp = f.typ
if ntyp.Kind() == Ptr {
if ntyp.Kind() == Pointer {
ntyp = ntyp.Elem().common()
}
}
Expand Down Expand Up @@ -1416,12 +1421,19 @@ func TypeOf(i interface{}) Type {
return toType(eface.typ)
}

// ptrMap is the cache for PtrTo.
// ptrMap is the cache for PointerTo.
var ptrMap sync.Map // map[*rtype]*ptrType

// PtrTo returns the pointer type with element t.
// For example, if t represents type Foo, PtrTo(t) represents *Foo.
func PtrTo(t Type) Type {
//
// Deprecated: use PointerTo. PtrTo is the old spelling.
// The two functions behaves identically.

This comment has been minimized.

Copy link
@earthboundkid

earthboundkid Sep 3, 2021

Contributor

@bradfitz This is a typo: behaves → behave.

This comment has been minimized.

Copy link
@ianlancetaylor

ianlancetaylor Sep 7, 2021

Contributor

@carlmjohnson GitHub is only a mirror, so very few people see comments made on GitHub commits. If you want to comment on a change please do so in Gerrit, in this case on https://golang.org/issue/341333. Thanks.

func PtrTo(t Type) Type { return PointerTo(t) }

// PointerTo returns the pointer type with element t.
// For example, if t represents type Foo, PointerTo(t) represents *Foo.
func PointerTo(t Type) Type {
return t.(*rtype).ptrTo()
}

Expand Down Expand Up @@ -1695,7 +1707,7 @@ func haveIdenticalUnderlyingType(T, V *rtype, cmpTags bool) bool {
case Map:
return haveIdenticalType(T.Key(), V.Key(), cmpTags) && haveIdenticalType(T.Elem(), V.Elem(), cmpTags)

case Ptr, Slice:
case Pointer, Slice:
return haveIdenticalType(T.Elem(), V.Elem(), cmpTags)

case Struct:
Expand Down Expand Up @@ -2136,7 +2148,7 @@ func funcStr(ft *funcType) string {
// That is, x == x for all values x of type t.
func isReflexive(t *rtype) bool {
switch t.Kind() {
case Bool, Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr, Chan, Ptr, String, UnsafePointer:
case Bool, Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr, Chan, Pointer, String, UnsafePointer:
return true
case Float32, Float64, Complex64, Complex128, Interface:
return false
Expand All @@ -2160,7 +2172,7 @@ func isReflexive(t *rtype) bool {
// needKeyUpdate reports whether map overwrites require the key to be copied.
func needKeyUpdate(t *rtype) bool {
switch t.Kind() {
case Bool, Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr, Chan, Ptr, UnsafePointer:
case Bool, Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr, Chan, Pointer, UnsafePointer:
return false
case Float32, Float64, Complex64, Complex128, Interface, String:
// Float keys can be updated from +0 to -0.
Expand Down Expand Up @@ -2217,10 +2229,10 @@ const (

func bucketOf(ktyp, etyp *rtype) *rtype {
if ktyp.size > maxKeySize {
ktyp = PtrTo(ktyp).(*rtype)
ktyp = PointerTo(ktyp).(*rtype)
}
if etyp.size > maxValSize {
etyp = PtrTo(etyp).(*rtype)
etyp = PointerTo(etyp).(*rtype)
}

// Prepare GC data if any.
Expand Down Expand Up @@ -2458,10 +2470,10 @@ func StructOf(fields []StructField) Type {
repr = append(repr, (" " + name)...)
if f.embedded() {
// Embedded field
if f.typ.Kind() == Ptr {
if f.typ.Kind() == Pointer {
// Embedded ** and *interface{} are illegal
elem := ft.Elem()
if k := elem.Kind(); k == Ptr || k == Interface {
if k := elem.Kind(); k == Pointer || k == Interface {
panic("reflect.StructOf: illegal embedded field type " + ft.String())
}
}
Expand Down Expand Up @@ -2526,7 +2538,7 @@ func StructOf(fields []StructField) Type {
tfn: resolveReflectText(unsafe.Pointer(&tfn)),
})
}
case Ptr:
case Pointer:
ptr := (*ptrType)(unsafe.Pointer(ft))
if unt := ptr.uncommon(); unt != nil {
if i > 0 && unt.mcount > 0 {
Expand Down Expand Up @@ -3123,7 +3135,7 @@ func addTypeBits(bv *bitVector, offset uintptr, t *rtype) {
}

switch Kind(t.kind & kindMask) {
case Chan, Func, Map, Ptr, Slice, String, UnsafePointer:
case Chan, Func, Map, Pointer, Slice, String, UnsafePointer:
// 1 pointer at start of representation
for bv.n < uint32(offset/uintptr(goarch.PtrSize)) {
bv.append(0)
Expand Down

0 comments on commit 17910ed

Please sign in to comment.