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

spec: Interplay between type conversions and embedded structs is not clear. #53975

Open
mattdee123 opened this issue Jul 20, 2022 · 0 comments
Open
Assignees
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@mattdee123
Copy link

The go 1.18 spec seems to disagree with the implementation regarding how type conversions and embedded structs interplay. Specifically:

type E struct {}

type A struct {
  E
}

type B struct {
  E E
}

func main() {
  _ = A(B{})
}

Results in

cannot convert B{} (value of type B) to type A

However, the spec seems to imply that this should work:

A non-constant value x can be converted to type T in any of these cases: [...] x's type and T are not type parameters but have identical underlying types.

The definition of type identity then says

Two struct types are identical if they have the same sequence of fields, and if corresponding fields have the same names, and identical types, and identical tags.

Finally, the section on struct type says of embedded structs

The unqualified type name acts as the field name.

So, in the case above, the corresponding fields do have the same names and types, which would imply that they are identical and can be converted. I think the definition of type identity should be updated to clarify that corresponding fields must have different "embedded-ness" as well.

@ianlancetaylor ianlancetaylor added the NeedsFix The path to resolution is known, but the work has not been done. label Jul 20, 2022
@ianlancetaylor ianlancetaylor added this to the Backlog milestone Jul 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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