Skip to content

Commit 6f1724f

Browse files
committed
reflect: fix method indexing for non-ASCII exported methods
Currently, methods are sorted by name. This happens to guarantee that exported ASCII methods appear before non-exported ASCII methods, but this breaks down when Unicode method names are considered. Type.Method already accounts for this by always indexing into the slice returned by exportedMethods. This CL makes Value.Method do the same. Fixes #22073. Change-Id: I9bfc6bbfb7353e0bd3c439a15d1c3da60d16d209 Reviewed-on: https://go-review.googlesource.com/66770 Run-TryBot: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: David Crawshaw <[email protected]>
1 parent b3cae37 commit 6f1724f

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

src/reflect/all_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6405,3 +6405,19 @@ func TestIssue22031(t *testing.T) {
64056405
}
64066406
}
64076407
}
6408+
6409+
type NonExportedFirst int
6410+
6411+
func (i NonExportedFirst) ΦExported() {}
6412+
func (i NonExportedFirst) nonexported() int { panic("wrong") }
6413+
6414+
func TestIssue22073(t *testing.T) {
6415+
m := ValueOf(NonExportedFirst(0)).Method(0)
6416+
6417+
if got := m.Type().NumOut(); got != 0 {
6418+
t.Errorf("NumOut: got %v, want 0", got)
6419+
}
6420+
6421+
// Shouldn't panic.
6422+
m.Call(nil)
6423+
}

src/reflect/value.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -584,11 +584,11 @@ func methodReceiver(op string, v Value, methodIndex int) (rcvrtype, t *rtype, fn
584584
t = tt.typeOff(m.typ)
585585
} else {
586586
rcvrtype = v.typ
587-
ut := v.typ.uncommon()
588-
if ut == nil || uint(i) >= uint(ut.mcount) {
587+
ms := v.typ.exportedMethods()
588+
if uint(i) >= uint(len(ms)) {
589589
panic("reflect: internal error: invalid method index")
590590
}
591-
m := ut.methods()[i]
591+
m := ms[i]
592592
if !v.typ.nameOff(m.name).isExported() {
593593
panic("reflect: " + op + " of unexported method")
594594
}
@@ -1717,11 +1717,11 @@ func (v Value) Type() Type {
17171717
return v.typ.typeOff(m.typ)
17181718
}
17191719
// Method on concrete type.
1720-
ut := v.typ.uncommon()
1721-
if ut == nil || uint(i) >= uint(ut.mcount) {
1720+
ms := v.typ.exportedMethods()
1721+
if uint(i) >= uint(len(ms)) {
17221722
panic("reflect: internal error: invalid method index")
17231723
}
1724-
m := ut.methods()[i]
1724+
m := ms[i]
17251725
return v.typ.typeOff(m.mtyp)
17261726
}
17271727

0 commit comments

Comments
 (0)