compiler: alignof [0]func() = 1

In the go protobuf code, a pattern is used to statically prevent
comparable structs by embedding:

```
type DoNotCompare [0]func()

type message struct {
  DoNotCompare
  data *uint32
}
```

Previously, sizezof(message{}) is 2 words large, but it only needs to be
1 byte.  Making it be 1 byte allows protobufs to compile slightly more
(though not all the way).
This commit is contained in:
Steven Kabbes
2022-05-17 21:23:43 -07:00
committed by Ron Evans
parent 995e815b63
commit 4c7449efe5
2 changed files with 16 additions and 0 deletions
+6
View File
@@ -21,6 +21,12 @@ func (s *stdSizes) Alignof(T types.Type) int64 {
// of alignment of the elements and fields, respectively.
switch t := T.Underlying().(type) {
case *types.Array:
if t.Len() == 0 {
// 0-sized arrays, always have 0 size.
// And from the spec, should have an alignment of _at least_ 1
return 1
}
// spec: "For a variable x of array type: unsafe.Alignof(x)
// is the same as unsafe.Alignof(x[0]), but at least 1."
return s.Alignof(t.Elem())