For instance, if ptr has type *const S where S has an alignment of 8, then ptr must be 8-aligned or else (*ptr).f is "based on an misaligned pointer"
What the document means; S is 8-aligned, then ptr has to 8-aligned? What if S is 16-aligned? Then assert!((&ptr as const* _ as isize) & 0xf ) == 0) always be true?
Having an alignment greater than that required is acceptable, as it necessarily means that all smaller alignments are true as well for the reason you stated.
k-alignment generally refers to a memory address a where a ⥠0 (mod k).
In the case of a pointer, there are two relevant memory addresses:
The address of the pointee, i.e. the value stored within the pointer
The memory location where the pointer itself is stored
Unfortunately, this leads to an ambiguity in a lot of documentation. Either of these can be meant when talking about the alignment of a pointer, and which one is relevant has to be inferred from context.
This statement is true, referring to the location of the pointee S:
As is this one on currently-supported platforms, now referring to the address where ptr is stored:
But this one is not generally true, because it's mixing the two senses of a pointer's alignment:
Your updated code is still referring to the value of the pointer / the location of an S instance. If you want to talk about the location of the pointer instead, you'll need to do something with &ptr or similar.
The formulation is potentially confusing, Iâll admit, however note that the document did never speak of the alignment of *const S. It speaks of the alignment of ptr. There is a distinction between âTYPE has alignment of Nâ and âPOINTER_VALUE_EXPRESSION as N-alignedâ which differs both in the specific wording âhas alignmentâ vs âis alignedâ and the fact that weâre talking about different types of things, types vs. pointer values.
Of course it all could probably still be made more clear if we just said âPOINTER_VALUE_EXPRESSIONâs address is N-alignedâ:
For instance, if ptr has type *const S where S has an alignment of 8, then ptrâs address must be 8-aligned or else (*ptr).f is "based on an misaligned pointer"
On the other hand, that wording would arguably be more inconsistent with the usage of âmisaligned pointerâ in other places. âthe pointer is misalignedâ and âPOINTER_VALUE_EXPRESSION as N-alignedâ seems very consistent.
Also, for experienced readers, no wrong interpretation should make any sense. âptr is N-alignedâ canât possibly mean âthe alignment of the type of ptr is Nâ because pointers all have a size and alignment of size_of::<usize>() anyways. And to speak of the alignment of the place in memory where a concrete pointer value itself lives doesnât make sense either in case you interpret that ptr is supposed to talk about the value of ptr. Though in case of âPOINTER_PLACE_EXPRESSION as N-alignedâ maybe thereâs some ambiguity; the reference page in question disambiguates by also introducing the term âPLACE_EXPRESSION is based on a misaligned pointerâ, though.
Back to possible clarifications... one thing thatâs kinda missing is defining anywhere what âPOINTER_VALUE is N-alignedâ actually means, so that could probably be added.
For instance, if ptr has type *const S where S has an alignment of 8, then ptr must be 8-aligned (i.e. ptr as usize is a multiple of 8) or else (*ptr).f is "based on an misaligned pointer"