]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: unexported fields are tied to a package
authorDavid Crawshaw <crawshaw@golang.org>
Fri, 4 Nov 2016 22:22:06 +0000 (18:22 -0400)
committerRuss Cox <rsc@golang.org>
Thu, 10 Nov 2016 14:06:23 +0000 (14:06 +0000)
An unexported field of a struct is not visible outside of the package
that defines it, so the package path is implicitly part of the
definition of any struct with an unexported field.

Change-Id: I17c6aac822bd0c24188ab8ba1cc406d6b5d82771
Reviewed-on: https://go-review.googlesource.com/32820
Run-TryBot: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/reflect/all_test.go
src/reflect/export_test.go
src/reflect/type.go

index c99ad79b64da6423a0fcb994ee84a1b0eaba5d99..4c116092188482342c05073630a8bcb998a448af 100644 (file)
@@ -5913,3 +5913,15 @@ func TestSwapper(t *testing.T) {
                }
        }
 }
+
+func TestInaccessibleField(t *testing.T) {
+       var b Buffer
+       var localBuffer struct {
+               buf []byte
+       }
+       lv := ValueOf(&localBuffer).Elem()
+       rv := ValueOf(b)
+       shouldPanic(func() {
+               lv.Set(rv)
+       })
+}
index 2cc1530250c084e536ad09ca9d1a69baf4bd9442..ffd1104487ba2af8a8fb8cebfee7396d681f0bee 100644 (file)
@@ -113,3 +113,7 @@ func IsExported(t Type) bool {
 func ResolveReflectName(s string) {
        resolveReflectName(newName(s, "", "", false))
 }
+
+type Buffer struct {
+       buf []byte
+}
index 66c27ebb93d0b0f4343360d2abd79796fb481645..e04eff7931fc9152c01483fb3fe827d5cde3d638 100644 (file)
@@ -1680,6 +1680,7 @@ func haveIdenticalUnderlyingType(T, V *rtype, cmpTags bool) bool {
                if len(t.fields) != len(v.fields) {
                        return false
                }
+               allExported := true
                for i := range t.fields {
                        tf := &t.fields[i]
                        vf := &v.fields[i]
@@ -1695,6 +1696,15 @@ func haveIdenticalUnderlyingType(T, V *rtype, cmpTags bool) bool {
                        if tf.offset != vf.offset {
                                return false
                        }
+                       allExported = allExported && tf.name.isExported()
+               }
+               if !allExported && t.pkgPath.name() != v.pkgPath.name() {
+                       // An unexported field of a struct is not
+                       // visible outside of the package that defines
+                       // it, so the package path is implicitly part
+                       // of the definition of any struct with an
+                       // unexported field.
+                       return false
                }
                return true
        }