Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions compiler/sizes.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ 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())
Expand All @@ -51,7 +45,11 @@ func (s *stdSizes) Alignof(T types.Type) int64 {
if t.Info()&types.IsString != 0 {
return s.PtrSize
}
case *types.Signature:
// Even though functions in tinygo are 2 pointers, they are not 2 pointer aligned
return s.PtrSize
}

a := s.Sizeof(T) // may be 0
// spec: "For a variable x of any type: unsafe.Alignof(x) is at least 1."
if a < 1 {
Expand Down
7 changes: 7 additions & 0 deletions testdata/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,13 @@ func main() {
println("errorValue.Implements(errorType) was true, expected false")
}

println("\nalignment / offset:")
v2 := struct {
noCompare [0]func()
data byte
}{}
println("struct{[0]func(); byte}:", unsafe.Offsetof(v2.data) == uintptr(unsafe.Pointer(&v2.data))-uintptr(unsafe.Pointer(&v2)))

println("\nstruct tags")
TestStructTag()

Expand Down
3 changes: 3 additions & 0 deletions testdata/reflect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,9 @@ offset for int64 matches: true
offset for complex128 matches: true
type assertion succeeded for unreferenced type

alignment / offset:
struct{[0]func(); byte}: true

struct tags
blue gopher

Expand Down