Skip to content

Commit 52c61de

Browse files
skabbesdeadprogram
authored andcommitted
compiler: alignof(func) is 1 pointer, not 2
This ensures that an embedded [0]func() never ends up being larger than 1 pointer, which is requried by protobuf processing code.
1 parent f308d7d commit 52c61de

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

compiler/sizes.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ func (s *stdSizes) Alignof(T types.Type) int64 {
2121
// of alignment of the elements and fields, respectively.
2222
switch t := T.Underlying().(type) {
2323
case *types.Array:
24-
if t.Len() == 0 {
25-
// 0-sized arrays, always have 0 size.
26-
// And from the spec, should have an alignment of _at least_ 1
27-
return 1
28-
}
29-
3024
// spec: "For a variable x of array type: unsafe.Alignof(x)
3125
// is the same as unsafe.Alignof(x[0]), but at least 1."
3226
return s.Alignof(t.Elem())
@@ -51,7 +45,11 @@ func (s *stdSizes) Alignof(T types.Type) int64 {
5145
if t.Info()&types.IsString != 0 {
5246
return s.PtrSize
5347
}
48+
case *types.Signature:
49+
// Even though functions in tinygo are 2 pointers, they are not 2 pointer aligned
50+
return s.PtrSize
5451
}
52+
5553
a := s.Sizeof(T) // may be 0
5654
// spec: "For a variable x of any type: unsafe.Alignof(x) is at least 1."
5755
if a < 1 {

testdata/reflect.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ func main() {
347347
println("errorValue.Implements(errorType) was true, expected false")
348348
}
349349

350+
println("\nalignment / offset:")
351+
v2 := struct {
352+
noCompare [0]func()
353+
data byte
354+
}{}
355+
println("struct{[0]func(); byte}:", unsafe.Offsetof(v2.data) == uintptr(unsafe.Pointer(&v2.data))-uintptr(unsafe.Pointer(&v2)))
356+
350357
println("\nstruct tags")
351358
TestStructTag()
352359

testdata/reflect.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,9 @@ offset for int64 matches: true
405405
offset for complex128 matches: true
406406
type assertion succeeded for unreferenced type
407407

408+
alignment / offset:
409+
struct{[0]func(); byte}: true
410+
408411
struct tags
409412
blue gopher
410413

0 commit comments

Comments
 (0)