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
cmd/compile: zero-sized type increases size of struct when it's used as a field #58483
Comments
Not always. See https://go.dev/play/p/b-Cwofn7RmC Putting the zero-length field in the middle has no effect on the size in this example. |
This an edge case of an edge case. The original reason this is done to is to prevent issues when doing stuff like this: type zero struct{}
type S struct {
a uint16
b zero
}
func F(s *S) *zero {
return &s.b
} If the struct were the same size of This is the same reason why In your case your extra empty field is using the blank identifier making expressions like Edit: after searching turns out this a real thing being done because of the
|
Change https://go.dev/cl/467616 mentions this issue: |
Updates #58483 Tested on Linux amd64: type Element struct { l0, l1, l2, l3, l4 uint64 } type PointAfter struct { x, y, z, t Element _ incomparable } type PointBefore struct { _ incomparable x, y, z, t Element } type incomparable [0]func() func main() { fmt.Println(unsafe.Sizeof(PointAfter{})) // 168 fmt.Println(unsafe.Sizeof(PointBefore{})) // 160 } Change-Id: I6c4fcb586bbf3febf62b6e54608496ff81685e43 Reviewed-on: https://go-review.googlesource.com/c/go/+/467616 Reviewed-by: Roland Shoemaker <roland@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Filippo Valsorda <filippo@golang.org> Reviewed-by: Damien Neil <dneil@google.com> Run-TryBot: Filippo Valsorda <filippo@golang.org>
Updates golang#58483 Tested on Linux amd64: type Element struct { l0, l1, l2, l3, l4 uint64 } type PointAfter struct { x, y, z, t Element _ incomparable } type PointBefore struct { _ incomparable x, y, z, t Element } type incomparable [0]func() func main() { fmt.Println(unsafe.Sizeof(PointAfter{})) // 168 fmt.Println(unsafe.Sizeof(PointBefore{})) // 160 } Change-Id: I6c4fcb586bbf3febf62b6e54608496ff81685e43 Reviewed-on: https://go-review.googlesource.com/c/go/+/467616 Reviewed-by: Roland Shoemaker <roland@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Filippo Valsorda <filippo@golang.org> Reviewed-by: Damien Neil <dneil@google.com> Run-TryBot: Filippo Valsorda <filippo@golang.org>
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
What did you expect to see?
What did you see instead?
The text was updated successfully, but these errors were encountered: