-
Notifications
You must be signed in to change notification settings - Fork 18.5k
Description
Based on a golang-nuts thread.
var (
x struct{}
y *int = (*int)(unsafe.Pointer(&x))
)
*y = 42The spec says about unsafe.Pointer:
A
Pointeris a pointer type but aPointervalue may not be dereferenced. Any pointer or value of core typeuintptrcan be converted to a type of core typePointerand vice versa. The effect of converting betweenPointeranduintptris implementation-defined.
Notably, while this does specify that conversion between unsafe.Pointer and uintptr is implementation-defined, it doesn't contain the same caveat for conversion between unsafe.Pointer and other pointers (like *int in the example above). So the spec does not give any way to reason about the behavior of the above program - not even that it is implementation defined.
The unsafe.Pointer rules do of course rule that program out, so the implementation-defined rules do allow us to reason about the program. But, technically speaking, we are not giving it purview to do that.
I would propose to change "The effect of converting between Pointer and uintptr is implementation-defined" to "The effect of such conversions is implementation-defined".