Commit e0d8064e authored by Josh Bleecher Snyder's avatar Josh Bleecher Snyder

go/types: fix multiword data structure alignment on nacl

Fixes #16464

Change-Id: Ibf5625c1b5fa3abd18623023f18664e8f81fa45a
Reviewed-on: https://go-review.googlesource.com/26996
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRobert Griesemer <gri@golang.org>
parent 7a974a4c
......@@ -64,6 +64,15 @@ func (s *StdSizes) Alignof(T Type) int64 {
}
}
return max
case *Slice, *Interface:
// Multiword data structures are effectively structs
// in which each element has size WordSize.
return s.WordSize
case *Basic:
// Strings are like slices and interfaces.
if t.Info()&IsString != 0 {
return s.WordSize
}
}
a := s.Sizeof(T) // may be 0
// spec: "For a variable x of any type: unsafe.Alignof(x) is at least 1."
......
......@@ -58,3 +58,26 @@ type S struct {
t.Errorf("Sizeof(%v) with WordSize 8 = %d want 40", ts, got)
}
}
// Issue 16464
func TestAlignofNaclSlice(t *testing.T) {
const src = `
package main
var s struct {
x *int
y []byte
}
`
ts := findStructType(t, src)
sizes := &types.StdSizes{WordSize: 4, MaxAlign: 8}
var fields []*types.Var
// Make a copy manually :(
for i := 0; i < ts.NumFields(); i++ {
fields = append(fields, ts.Field(i))
}
offsets := sizes.Offsetsof(fields)
if offsets[0] != 0 || offsets[1] != 4 {
t.Errorf("OffsetsOf(%v) = %v want %v", ts, offsets, []int{0, 4})
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment