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: StructOf allows creation of impossible types #23063

Closed
dsnet opened this issue Dec 8, 2017 · 2 comments
Closed

reflect: StructOf allows creation of impossible types #23063

dsnet opened this issue Dec 8, 2017 · 2 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@dsnet
Copy link
Member

dsnet commented Dec 8, 2017

The validation logic in reflect.StructOf needs some work.

Can embed an anonymous type

t := reflect.TypeOf(struct{ Nobody string }{})
sf := reflect.StructField{Name: "Billy", Type: t, Anonymous: true}
t2 := reflect.StructOf([]reflect.StructField{sf})
t2.Field(0) // StructField{Name: "Billy", Type: t, Anonymous: true}

This is invalid because the Go language forbids embedding an anonymous type.

Can rename an embedded type

type Batman struct{ Banananananana string }
t := reflect.TypeOf(Batman{})
sf := reflect.StructField{Name: "Superman", Type: t, Anonymous: true}
t2 := reflect.StructOf([]reflect.StructField{sf})
t2.Field(0) // StructField{Name: "Superman", Type: t, Anonymous: true}

When a type is embedded, the field takes on the name of the type. However, I was able to rename the field to Superman, when it should have been Batman.

@dsnet dsnet added the NeedsFix The path to resolution is known, but the work has not been done. label Dec 8, 2017
@dsnet dsnet added this to the Go1.11 milestone Dec 8, 2017
@jimmyfrasche
Copy link
Member

Renaming is to support type aliases.

@dsnet
Copy link
Member Author

dsnet commented Dec 8, 2017

Renaming is to support type aliases.

Ah, you are correct. Type aliases actually make both cases possible.

@dsnet dsnet closed this as completed Dec 8, 2017
@golang golang locked and limited conversation to collaborators Dec 8, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

3 participants