diff options
author | Cuong Manh Le <cuong.manhle.vn@gmail.com> | 2020-10-22 00:25:17 +0700 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2020-10-28 17:10:08 +0000 |
commit | 642329fdd55aabafc67b3a7c50902e29125621ab (patch) | |
tree | 391ec188d728cbc5578b07781d4e155e82f7c34c /src/reflect | |
parent | e3c58bbeb8c76fa3abc0f7153edbab72208c1f88 (diff) | |
download | go-642329fdd55aabafc67b3a7c50902e29125621ab.tar.gz go-642329fdd55aabafc67b3a7c50902e29125621ab.zip |
Revert "cmd/compile: split exported/non-exported methods for interface type"
This reverts commit 8f26b57f9afc238bdecb9b7030bc2f4364093885.
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>
Diffstat (limited to 'src/reflect')
-rw-r--r-- | src/reflect/all_test.go | 18 | ||||
-rw-r--r-- | src/reflect/type.go | 55 | ||||
-rw-r--r-- | src/reflect/value.go | 21 |
3 files changed, 36 insertions, 58 deletions
diff --git a/src/reflect/all_test.go b/src/reflect/all_test.go index be15362aae..a12712d254 100644 --- a/src/reflect/all_test.go +++ b/src/reflect/all_test.go @@ -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 { @@ -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 @@ -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 diff --git a/src/reflect/type.go b/src/reflect/type.go index 0b34ca0c94..a3a616701b 100644 --- a/src/reflect/type.go +++ b/src/reflect/type.go @@ -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 @@ -1053,22 +1049,25 @@ 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) { @@ -1076,8 +1075,8 @@ func (t *interfaceType) MethodByName(name string) (m Method, ok bool) { 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 } @@ -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 @@ -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() { @@ -1526,7 +1523,7 @@ func implements(T, V *rtype) bool { continue } } - if i++; i >= len(tmethods) { + if i++; i >= len(t.methods) { return true } } @@ -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) @@ -1559,7 +1556,7 @@ func implements(T, V *rtype) bool { continue } } - if i++; i >= len(tmethods) { + if i++; i >= len(t.methods) { return true } } @@ -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 @@ -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") @@ -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 -} diff --git a/src/reflect/value.go b/src/reflect/value.go index 24eab6a2c6..bf926a7453 100644 --- a/src/reflect/value.go +++ b/src/reflect/value.go @@ -636,11 +636,10 @@ func methodReceiver(op string, v Value, methodIndex int) (rcvrtype *rtype, t *fu i := methodIndex if v.typ.Kind() == Interface { tt := (*interfaceType)(unsafe.Pointer(v.typ)) - ttmethods := tt.methods() - if uint(i) >= uint(len(ttmethods)) { + if uint(i) >= uint(len(tt.methods)) { panic("reflect: internal error: invalid method index") } - m := &ttmethods[i] + m := &tt.methods[i] if !tt.nameOff(m.name).isExported() { panic("reflect: " + op + " of unexported method") } @@ -814,7 +813,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 { @@ -1035,7 +1034,7 @@ func valueInterface(v Value, safe bool) interface{} { // Special case: return the element inside the interface. // Empty interface has one layout, all interfaces with // methods have a second layout. - if isEmptyIface(v.typ) { + if v.NumMethod() == 0 { return *(*interface{})(v.ptr) } return *(*interface { @@ -1919,11 +1918,10 @@ func (v Value) Type() Type { if v.typ.Kind() == Interface { // Method on interface. tt := (*interfaceType)(unsafe.Pointer(v.typ)) - ttmethods := tt.methods() - if uint(i) >= uint(len(ttmethods)) { + if uint(i) >= uint(len(tt.methods)) { panic("reflect: internal error: invalid method index") } - m := &ttmethods[i] + m := &tt.methods[i] return v.typ.typeOff(m.typ) } // Method on concrete type. @@ -2441,7 +2439,7 @@ func (v Value) assignTo(context string, dst *rtype, target unsafe.Pointer) Value return Value{dst, nil, flag(Interface)} } x := valueInterface(v, false) - if isEmptyIface(dst) { + if dst.NumMethod() == 0 { *(*interface{})(target) = x } else { ifaceE2I(dst, x, target) @@ -2730,11 +2728,10 @@ func cvtDirect(v Value, typ Type) Value { func cvtT2I(v Value, typ Type) Value { target := unsafe_New(typ.common()) x := valueInterface(v, false) - rt := typ.(*rtype) - if isEmptyIface(rt) { + if typ.NumMethod() == 0 { *(*interface{})(target) = x } else { - ifaceE2I(rt, x, target) + ifaceE2I(typ.(*rtype), x, target) } return Value{typ.common(), target, v.flag.ro() | flagIndir | flag(Interface)} } |