Skip to content
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

reflect: gccgo and go differ on handling of fields from embedded structs #11007

Closed
Thinkofname opened this issue May 31, 2015 · 7 comments
Closed

Comments

@Thinkofname
Copy link

Test case: http://play.golang.org/p/pqIQCAJOXp

On go 1.4.2 and tip that works fine (as seen on the playground) but on gccgo
go version go1.4.2 gccgo (Ubuntu 5.1~rc1-0ubuntu1) 5.0.1 20150414 (prerelease) [gcc-5-branch revision 222102] linux/amd64
it will fail with

&{{}}
panic: reflect: reflect.mustBeAssignable.N13_reflect.Value using value obtained using unexported field

goroutine 16 [running]:
reflect.mustBeAssignable.N12_reflect.flag
    ../../../src/libgo/go/reflect/value.go:216
reflect.mustBeAssignable.N13_reflect.Value
    ../../../src/libgo/go/reflect/value.go:33
reflect.SetString.N13_reflect.Value
    ../../../src/libgo/go/reflect/value.go:1373
main.main
    /home/matt/.local/share/data/liteide/liteide/goplay.go:19
created by main
    ../../../src/libgo/runtime/go-main.c:42

This might be related to #7363 however I assumed that Exported fields from embedded structs should have the same visibility as if it was on the parent struct.

@minux
Copy link
Member

minux commented May 31, 2015 via email

@Thinkofname
Copy link
Author

Seems to me that a mix between the two is the right option, matching the language.
e.g.

// package example
type unexported struct {
    Exported string
}

type Exported struct {
    unexported
}

// package main
import "example"

func main() {
    val := &example.Exported{}

    val.Exported = "test" // Ok
    reflect.ValueOf(val).Elem().FieldByName("Exported").SetString("Hello world") // Also ok

    val.unexported = /*something*/  // invalid
    reflect.ValueOf(val).Elem().Field(0).Set(/*something*/) // Also invalid
}

@ianlancetaylor
Copy link
Member

Looks like #7363 .

@ianlancetaylor
Copy link
Member

Oh, I see that you saw that. Hmmm. This does seem to be an issue in the reflect package. It doesn't currently consider how an embedded field affects visibility.

@ianlancetaylor ianlancetaylor added this to the Go1.5Maybe milestone May 31, 2015
@rsc
Copy link
Contributor

rsc commented Jul 15, 2015

The fix for #7363 should address this too.

@rsc rsc assigned mpvl Jul 15, 2015
@rsc rsc modified the milestones: Go1.6Early, Go1.5Maybe Jul 15, 2015
@mpvl mpvl added the Started label Aug 26, 2015
@gopherbot
Copy link
Contributor

CL https://golang.org/cl/14010 mentions this issue.

@gopherbot
Copy link
Contributor

CL https://golang.org/cl/14085 mentions this issue.

mpvl added a commit that referenced this issue Oct 26, 2015
This CL changes reflect to allow access to exported fields and
methods in unexported embedded structs for gccgo and after gc
has been adjusted to disallow access to embedded unexported structs.

Adresses #12367, #7363, #11007, and #7247.

Change-Id: If80536eab35abcd25300d8ddc2d27d5c42d7e78e
Reviewed-on: https://go-review.googlesource.com/14010
Reviewed-by: Russ Cox <rsc@golang.org>
@mpvl mpvl closed this as completed in afe9837 Oct 26, 2015
@golang golang locked and limited conversation to collaborators Oct 26, 2016
@rsc rsc unassigned mpvl Jun 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants