]> Cypherpunks repositories - gostls13.git/commitdiff
unsafe: document when Sizeof/Offsetof/Alignof are not constant
authorIan Lance Taylor <iant@golang.org>
Sat, 16 Jul 2022 22:51:25 +0000 (15:51 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 21 Jul 2022 23:05:01 +0000 (23:05 +0000)
They are not constant if their arguments have types that are variable size.

Fixes #53921

Change-Id: I2d46754c27f55a281331c099a11ea3cd85ec4e89
Reviewed-on: https://go-review.googlesource.com/c/go/+/417917
Auto-Submit: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Rob Pike <r@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/unsafe/unsafe.go

index da15902b293595772499539e8037396bd90667c3..5051b3ee9fe2e25c78a554df7481917bf5bd0975 100644 (file)
@@ -189,13 +189,18 @@ type Pointer *ArbitraryType
 // For instance, if x is a slice, Sizeof returns the size of the slice
 // descriptor, not the size of the memory referenced by the slice.
 // For a struct, the size includes any padding introduced by field alignment.
-// The return value of Sizeof is a Go constant.
+// The return value of Sizeof is a Go constant if the type of the argument x
+// does not have variable size.
+// (A type has variable size if it is a type parameter or if it is an array
+// or struct type with elements of variable size).
 func Sizeof(x ArbitraryType) uintptr
 
 // Offsetof returns the offset within the struct of the field represented by x,
 // which must be of the form structValue.field. In other words, it returns the
 // number of bytes between the start of the struct and the start of the field.
-// The return value of Offsetof is a Go constant.
+// The return value of Offsetof is a Go constant if the type of the argument x
+// does not have variable size.
+// (See the description of [Sizeof] for a definition of variable sized types.)
 func Offsetof(x ArbitraryType) uintptr
 
 // Alignof takes an expression x of any type and returns the required alignment
@@ -206,7 +211,9 @@ func Offsetof(x ArbitraryType) uintptr
 // within that struct, then Alignof(s.f) will return the required alignment
 // of a field of that type within a struct. This case is the same as the
 // value returned by reflect.TypeOf(s.f).FieldAlign().
-// The return value of Alignof is a Go constant.
+// The return value of Alignof is a Go constant if the type of the argument
+// does not have variable size.
+// (See the description of [Sizeof] for a definition of variable sized types.)
 func Alignof(x ArbitraryType) uintptr
 
 // The function Add adds len to ptr and returns the updated pointer