mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-07-26 06:38:42 +00:00
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:
@@ -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())
|
||||
|
||||
Reference in New Issue
Block a user