-
Notifications
You must be signed in to change notification settings - Fork 18k
reflect: can Set to unexported embedded pointer types #21353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
\cc @zombiezen @adams-sarah @cybrcodr @jba @pongad
|
gccgo panics on both calls to |
I think this is simply a bug, so moving from NeedsDecision to NeedsFix. |
Change https://golang.org/cl/53643 mentions this issue: |
Change https://golang.org/cl/60410 mentions this issue: |
Consider the following snippet:
We have two different structs
S1
andS2
that differ only in that one has a embedded pointer, while the other does not. Note thatembed
is an unexported type, and so acts an unexported field inS1
andS2
.From Go1.0 until (and including) Go1.5, you could call
Set
on the unexported fieldembed
on both the embedded value and embedded pointer versions.From Go1.6 until (the present day) Go1.9, you could call
Set
only on theembed
field only if it were pointer version, butreflect
would panic if you tried to callSet
on the pointer version. This is a result of a change to the policy mentioned here.Outside of reflect, the Go language forbids you from directly setting
S1.embed
orS2.embed
from an external package. For this reason, I'm inclined to believe that the current behavior ofreflect
is a bug since it permits you to do something that the language forbids.However, there is an implication to this change. It means that JSON cannot be round-trip marshaled and unmarshaled. For example:
There would need to be a fix to the
json
package to avoid a panic in this situation.If we accept my fix to #21122, then this will also be fixed.
\cc @ianlancetaylor
The text was updated successfully, but these errors were encountered: