Skip to content

Commit

Permalink
Revert "cmd/compile: split exported/non-exported methods for interfac…
Browse files Browse the repository at this point in the history
…e type"

This reverts commit 8f26b57.

Reason for revert: break a bunch of code, include standard library.

Fixes #42123

Change-Id: Ife90ecbafd2cb395623d1db555fbfc9c1b0098e0
Reviewed-on: https://go-review.googlesource.com/c/go/+/264026
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
  • Loading branch information
cuonglm authored and rsc committed Oct 28, 2020
1 parent e3c58bb commit 642329f
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 125 deletions.
9 changes: 0 additions & 9 deletions doc/go1.16.html
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,6 @@ <h3 id="net"><a href="/pkg/net/">net</a></h3>
On Linux kernel version 4.1 and above, the maximum is now <code>4294967295</code>.
</p>

<h3 id="reflect"><a href="/pkg/reflect/">reflect</a></h3>

<p><!-- CL 259237, golang.org/issue/22075 -->
For interface types and values, <a href="/pkg/reflect/#Value.Method">Method</a>,
<a href="/pkg/reflect/#Value.MethodByName">MethodByName</a>, and
<a href="/pkg/reflect/#Value.NumMethod">NumMethod</a> now
operate on the interface's exported method set, rather than its full method set.
</p>

<h3 id="text/template/parse"><a href="/pkg/text/template/parse/">text/template/parse</a></h3>

<p><!-- CL 229398, golang.org/issue/34652 -->
Expand Down
3 changes: 1 addition & 2 deletions src/cmd/compile/internal/gc/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -1275,9 +1275,8 @@ func dtypesym(t *types.Type) *obj.LSym {
}
ot = dgopkgpath(lsym, ot, tpkg)

xcount := sort.Search(n, func(i int) bool { return !types.IsExported(m[i].name.Name) })
ot = dsymptr(lsym, ot, lsym, ot+3*Widthptr+uncommonSize(t))
ot = duintptr(lsym, ot, uint64(xcount))
ot = duintptr(lsym, ot, uint64(n))
ot = duintptr(lsym, ot, uint64(n))
dataAdd := imethodSize() * n
ot = dextratype(lsym, ot, t, dataAdd)
Expand Down
35 changes: 11 additions & 24 deletions src/internal/reflectlite/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,10 @@ type imethod struct {
// interfaceType represents an interface type.
type interfaceType struct {
rtype
pkgPath name // import path
expMethods []imethod // sorted by name, see runtime/type.go:interfacetype to see how it is encoded.
pkgPath name // import path
methods []imethod // sorted by hash
}

func (t *interfaceType) methods() []imethod { return t.expMethods[:cap(t.expMethods)] }
func (t *interfaceType) isEmpty() bool { return cap(t.expMethods) == 0 }

// mapType represents a map type.
type mapType struct {
rtype
Expand Down Expand Up @@ -698,7 +695,7 @@ func add(p unsafe.Pointer, x uintptr, whySafe string) unsafe.Pointer {
}

// NumMethod returns the number of interface methods in the type's method set.
func (t *interfaceType) NumMethod() int { return len(t.expMethods) }
func (t *interfaceType) NumMethod() int { return len(t.methods) }

// TypeOf returns the reflection Type that represents the dynamic type of i.
// If i is a nil interface value, TypeOf returns nil.
Expand Down Expand Up @@ -735,10 +732,9 @@ func implements(T, V *rtype) bool {
return false
}
t := (*interfaceType)(unsafe.Pointer(T))
if t.isEmpty() {
if len(t.methods) == 0 {
return true
}
tmethods := t.methods()

// The same algorithm applies in both cases, but the
// method tables for an interface type and a concrete type
Expand All @@ -755,11 +751,10 @@ func implements(T, V *rtype) bool {
if V.Kind() == Interface {
v := (*interfaceType)(unsafe.Pointer(V))
i := 0
vmethods := v.methods()
for j := 0; j < len(vmethods); j++ {
tm := &tmethods[i]
for j := 0; j < len(v.methods); j++ {
tm := &t.methods[i]
tmName := t.nameOff(tm.name)
vm := &vmethods[j]
vm := &v.methods[j]
vmName := V.nameOff(vm.name)
if vmName.name() == tmName.name() && V.typeOff(vm.typ) == t.typeOff(tm.typ) {
if !tmName.isExported() {
Expand All @@ -775,7 +770,7 @@ func implements(T, V *rtype) bool {
continue
}
}
if i++; i >= len(tmethods) {
if i++; i >= len(t.methods) {
return true
}
}
Expand All @@ -790,7 +785,7 @@ func implements(T, V *rtype) bool {
i := 0
vmethods := v.methods()
for j := 0; j < int(v.mcount); j++ {
tm := &tmethods[i]
tm := &t.methods[i]
tmName := t.nameOff(tm.name)
vm := vmethods[j]
vmName := V.nameOff(vm.name)
Expand All @@ -808,7 +803,7 @@ func implements(T, V *rtype) bool {
continue
}
}
if i++; i >= len(tmethods) {
if i++; i >= len(t.methods) {
return true
}
}
Expand Down Expand Up @@ -902,7 +897,7 @@ func haveIdenticalUnderlyingType(T, V *rtype, cmpTags bool) bool {
case Interface:
t := (*interfaceType)(unsafe.Pointer(T))
v := (*interfaceType)(unsafe.Pointer(V))
if t.isEmpty() && v.isEmpty() {
if len(t.methods) == 0 && len(v.methods) == 0 {
return true
}
// Might have the same methods but still
Expand Down Expand Up @@ -967,11 +962,3 @@ func toType(t *rtype) Type {
func ifaceIndir(t *rtype) bool {
return t.kind&kindDirectIface == 0
}

func isEmptyIface(t *rtype) bool {
if t.Kind() != Interface {
return false
}
tt := (*interfaceType)(unsafe.Pointer(t))
return tt.isEmpty()
}
4 changes: 2 additions & 2 deletions src/internal/reflectlite/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (v Value) Elem() Value {
switch k {
case Interface:
var eface interface{}
if isEmptyIface(v.typ) {
if v.typ.NumMethod() == 0 {
eface = *(*interface{})(v.ptr)
} else {
eface = (interface{})(*(*interface {
Expand Down Expand Up @@ -433,7 +433,7 @@ func (v Value) assignTo(context string, dst *rtype, target unsafe.Pointer) Value
return Value{dst, nil, flag(Interface)}
}
x := valueInterface(v)
if isEmptyIface(dst) {
if dst.NumMethod() == 0 {
*(*interface{})(target) = x
} else {
ifaceE2I(dst, x, target)
Expand Down
18 changes: 5 additions & 13 deletions src/reflect/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2995,14 +2995,6 @@ func TestUnexportedMethods(t *testing.T) {
if got := typ.NumMethod(); got != 0 {
t.Errorf("NumMethod=%d, want 0 satisfied methods", got)
}

var i unexpI
if got := TypeOf(&i).Elem().NumMethod(); got != 0 {
t.Errorf("NumMethod=%d, want 0 satisfied methods", got)
}
if got := ValueOf(&i).Elem().NumMethod(); got != 0 {
t.Errorf("NumMethod=%d, want 0 satisfied methods", got)
}
}

type InnerInt struct {
Expand Down Expand Up @@ -3656,21 +3648,21 @@ func TestCallPanic(t *testing.T) {
v := ValueOf(T{i, i, i, i, T2{i, i}, i, i, T2{i, i}})
badCall(func() { call(v.Field(0).Method(0)) }) // .t0.W
badCall(func() { call(v.Field(0).Elem().Method(0)) }) // .t0.W
badMethod(func() { call(v.Field(0).Method(1)) }) // .t0.w
badCall(func() { call(v.Field(0).Method(1)) }) // .t0.w
badMethod(func() { call(v.Field(0).Elem().Method(2)) }) // .t0.w
ok(func() { call(v.Field(1).Method(0)) }) // .T1.Y
ok(func() { call(v.Field(1).Elem().Method(0)) }) // .T1.Y
badMethod(func() { call(v.Field(1).Method(1)) }) // .T1.y
badCall(func() { call(v.Field(1).Method(1)) }) // .T1.y
badMethod(func() { call(v.Field(1).Elem().Method(2)) }) // .T1.y

ok(func() { call(v.Field(2).Method(0)) }) // .NamedT0.W
ok(func() { call(v.Field(2).Elem().Method(0)) }) // .NamedT0.W
badMethod(func() { call(v.Field(2).Method(1)) }) // .NamedT0.w
badCall(func() { call(v.Field(2).Method(1)) }) // .NamedT0.w
badMethod(func() { call(v.Field(2).Elem().Method(2)) }) // .NamedT0.w

ok(func() { call(v.Field(3).Method(0)) }) // .NamedT1.Y
ok(func() { call(v.Field(3).Elem().Method(0)) }) // .NamedT1.Y
badMethod(func() { call(v.Field(3).Method(1)) }) // .NamedT1.y
badCall(func() { call(v.Field(3).Method(1)) }) // .NamedT1.y
badMethod(func() { call(v.Field(3).Elem().Method(3)) }) // .NamedT1.y

ok(func() { call(v.Field(4).Field(0).Method(0)) }) // .NamedT2.T1.Y
Expand All @@ -3680,7 +3672,7 @@ func TestCallPanic(t *testing.T) {

badCall(func() { call(v.Field(5).Method(0)) }) // .namedT0.W
badCall(func() { call(v.Field(5).Elem().Method(0)) }) // .namedT0.W
badMethod(func() { call(v.Field(5).Method(1)) }) // .namedT0.w
badCall(func() { call(v.Field(5).Method(1)) }) // .namedT0.w
badMethod(func() { call(v.Field(5).Elem().Method(2)) }) // .namedT0.w

badCall(func() { call(v.Field(6).Method(0)) }) // .namedT1.Y
Expand Down
55 changes: 22 additions & 33 deletions src/reflect/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,10 @@ type imethod struct {
// interfaceType represents an interface type.
type interfaceType struct {
rtype
pkgPath name // import path
expMethods []imethod // sorted by name, see runtime/type.go:interfacetype to see how it is encoded.
pkgPath name // import path
methods []imethod // sorted by hash
}

// methods returns t's full method set, both exported and non-exported.
func (t *interfaceType) methods() []imethod { return t.expMethods[:cap(t.expMethods)] }
func (t *interfaceType) isEmpty() bool { return cap(t.expMethods) == 0 }

// mapType represents a map type.
type mapType struct {
rtype
Expand Down Expand Up @@ -1053,31 +1049,34 @@ func (d ChanDir) String() string {

// Method returns the i'th method in the type's method set.
func (t *interfaceType) Method(i int) (m Method) {
if i < 0 || i >= len(t.expMethods) {
panic("reflect: Method index out of range")
if i < 0 || i >= len(t.methods) {
return
}
p := &t.expMethods[i]
p := &t.methods[i]
pname := t.nameOff(p.name)
m.Name = pname.name()
if !pname.isExported() {
panic("reflect: unexported method: " + pname.name())
m.PkgPath = pname.pkgPath()
if m.PkgPath == "" {
m.PkgPath = t.pkgPath.name()
}
}
m.Type = toType(t.typeOff(p.typ))
m.Index = i
return
}

// NumMethod returns the number of exported interface methods in the type's method set.
func (t *interfaceType) NumMethod() int { return len(t.expMethods) }
// NumMethod returns the number of interface methods in the type's method set.
func (t *interfaceType) NumMethod() int { return len(t.methods) }

// MethodByName method with the given name in the type's method set.
func (t *interfaceType) MethodByName(name string) (m Method, ok bool) {
if t == nil {
return
}
var p *imethod
for i := range t.expMethods {
p = &t.expMethods[i]
for i := range t.methods {
p = &t.methods[i]
if t.nameOff(p.name).name() == name {
return t.Method(i), true
}
Expand Down Expand Up @@ -1486,10 +1485,9 @@ func implements(T, V *rtype) bool {
return false
}
t := (*interfaceType)(unsafe.Pointer(T))
if t.isEmpty() {
if len(t.methods) == 0 {
return true
}
tmethods := t.methods()

// The same algorithm applies in both cases, but the
// method tables for an interface type and a concrete type
Expand All @@ -1506,11 +1504,10 @@ func implements(T, V *rtype) bool {
if V.Kind() == Interface {
v := (*interfaceType)(unsafe.Pointer(V))
i := 0
vmethods := v.methods()
for j := 0; j < len(vmethods); j++ {
tm := &tmethods[i]
for j := 0; j < len(v.methods); j++ {
tm := &t.methods[i]
tmName := t.nameOff(tm.name)
vm := &vmethods[j]
vm := &v.methods[j]
vmName := V.nameOff(vm.name)
if vmName.name() == tmName.name() && V.typeOff(vm.typ) == t.typeOff(tm.typ) {
if !tmName.isExported() {
Expand All @@ -1526,7 +1523,7 @@ func implements(T, V *rtype) bool {
continue
}
}
if i++; i >= len(tmethods) {
if i++; i >= len(t.methods) {
return true
}
}
Expand All @@ -1541,7 +1538,7 @@ func implements(T, V *rtype) bool {
i := 0
vmethods := v.methods()
for j := 0; j < int(v.mcount); j++ {
tm := &tmethods[i]
tm := &t.methods[i]
tmName := t.nameOff(tm.name)
vm := vmethods[j]
vmName := V.nameOff(vm.name)
Expand All @@ -1559,7 +1556,7 @@ func implements(T, V *rtype) bool {
continue
}
}
if i++; i >= len(tmethods) {
if i++; i >= len(t.methods) {
return true
}
}
Expand Down Expand Up @@ -1661,7 +1658,7 @@ func haveIdenticalUnderlyingType(T, V *rtype, cmpTags bool) bool {
case Interface:
t := (*interfaceType)(unsafe.Pointer(T))
v := (*interfaceType)(unsafe.Pointer(V))
if t.isEmpty() && v.isEmpty() {
if len(t.methods) == 0 && len(v.methods) == 0 {
return true
}
// Might have the same methods but still
Expand Down Expand Up @@ -2445,7 +2442,7 @@ func StructOf(fields []StructField) Type {
switch f.typ.Kind() {
case Interface:
ift := (*interfaceType)(unsafe.Pointer(ft))
for im, m := range ift.methods() {
for im, m := range ift.methods {
if ift.nameOff(m.name).pkgPath() != "" {
// TODO(sbinet). Issue 15924.
panic("reflect: embedded interface with unexported method(s) not implemented")
Expand Down Expand Up @@ -3152,11 +3149,3 @@ func addTypeBits(bv *bitVector, offset uintptr, t *rtype) {
}
}
}

func isEmptyIface(rt *rtype) bool {
if rt.Kind() != Interface {
return false
}
tt := (*interfaceType)(unsafe.Pointer(rt))
return len(tt.methods()) == 0
}
Loading

0 comments on commit 642329f

Please sign in to comment.