]> Cypherpunks repositories - gostls13.git/commit
reflect: disallow invoking methods on unexported embedded fields
authorMatthew Dempsky <mdempsky@google.com>
Sun, 19 Apr 2020 20:59:16 +0000 (13:59 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Tue, 21 Apr 2020 05:41:33 +0000 (05:41 +0000)
commit0eb694e9c217c051cd8cc18258bf593d0be7fb8d
tree40d746cd173648d4bf00fc78486eac4494a16964
parent925516309128f20bb11a38f3e5265f23360a28ba
reflect: disallow invoking methods on unexported embedded fields

Given:

    type u struct{}
    func (u) M() {}

    type t struct { u; u2 u }

    var v = reflect.ValueOf(t{})

Package reflect allows:

    v.Method(0)          // v.M
    v.Field(0).Method(0) // v.u.M

but panics from:

    v.Field(1).Method(0) // v.u2.M

because u2 is not an exported field. However, u is not an exported
field either, so this is inconsistent.

It seems like this behavior originates from #12367, where it was
decided to allow traversing unexported embedded fields to be able to
access their exported fields, since package reflect doesn't provide an
alternative way to access promoted fields directly.

But extending that logic to promoted *methods* was inappropriate,
because package reflect's normal method handling logic already handles
promoted methods correctly. This CL corrects that mistake.

Fixes #38521.

Change-Id: If65008965f35927b4e7927cddf8614695288eb19
Reviewed-on: https://go-review.googlesource.com/c/go/+/228902
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
doc/go1.15.html
src/reflect/all_test.go
src/reflect/value.go