Will ptr be valid for reads? Or should I consider it to be invalidated once A is out-of-scope?
IIUC, a pointer to a static will be alive for the entirety of the program (since the data it points to gets compiled as a part of the binary), but I'm not sure about consts.
You understand statics correctly. Unlike statics, consts do not have their own addresses. The docs says about consts, that:
Constants are essentially inlined wherever they are used, meaning that they are copied directly into the relevant context when used.
This behavior is very similar to literals.
So in the general case, the pointer would not be valid.[1]
But, like literals, constant values are also eligible for constant promotion:
Promotion of a value expression to a 'static slot occurs when the expression could be written in a constant and borrowed, and that borrow could be dereferenced where the expression was originally written, without changing the runtime behavior. That is, the promoted expression can be evaluated at compile-time and the resulting value does not contain interior mutability or destructors (these properties are determined based on the value where possible, e.g. &None always has the type &'static Option<_>, as it contains nothing disallowed).
So in this case (of a [u8; 8]) the value gets promoted into a static slot, and the pointer will be valid. I would not recommend relying on promotion though, especially not with unsafe code!
That’s true, but I would suggest that it might be wiser to not implicitly rely on implicit promotion — to make it harder for edits to the program to accidentally break its required properties. This can even be done without switching to a static item (which would enforce an unnecessary unique address):
Thank you both! I knew about constant promotions, but the rules are quite difficult to follow, and I always had a (wrong) assumption that they only happen when references are involved (i.e. it is why you can conjure a &[] out of thin air, and return it from a function with a lifetime tied to one of its params?)
I think I'll file a PR to change the const into a static, if not for anything then at least so at least other people won't find themselves going down this rabbit hole
I don't see any pointer to const in that macro. Which line specifically? OUTPUT.as_ptr() is not a pointer to const item, since OUTPUT is a slice instead of an array (like in your OP).