Closed
Description
Feature gate: #![feature(const_pointer_is_aligned)]
This is a tracking issue for using ptr.is_aligned()
and ptr.is_aligned_to(alignment)
in const
contexts.
Public API
impl<T: Sized> *(const|mut) T {
pub const fn is_aligned(self) -> bool;
}
impl<T: ?Sized> *(const|mut) T {
pub const fn is_aligned_to(self, align: usize) -> bool;
}
impl <T> NonNull<T> {
pub const fn is_aligned(self) -> bool;
}
Steps / History
- Implementation: Constify
is_aligned
viaalign_offset
#102795 -
NonNull
methods stabilized,const
ness moved to this gate Stabilizenon_null_convenience
#124498 - Allow
const
functions to behave differently during constant-evaluation and runtime: Relax const-eval restrictions rfcs#3352 - Final comment period (FCP)1
- Stabilization PR
Unresolved Questions
- Do we want this at all or should alignment be runtime only / should users be using a stable const_eval_select instead?
- Should these be separate functions
guaranteed_aligned{,to}
instead? - What behavior should these functions have during const eval? Some options include:
- Return
true
if the pointer is guaranteed to be aligned at runtime andfalse
if we don't know whether the pointer is aligned at runtime. This is the current implementation. - Same as above, but only document that the function may spuriously fail (during const eval), similar to
align_offset
. This allows changes to const alignment in the future. - Assign a dummy address to all variables during const eval. Each allocation, including stack variables, would get its own "address space", which overlaps with other const eval address spaces and the runtime address space. This would make const eval alignment behavior similar to runtime, but a runtime pointer derived from a const eval pointer is still differently aligned. Pointer-to-integer casts are still not supported. The implementation for this would just remove this check: https://github.com/rust-lang/rust/blob/005f92d24acde37a02e0d084ce2ca8f40d2ee95a/compiler/rustc_const_eval/src/const_eval/machine.rs#L237
- Same as above, but we also allow pointer-to-integer casts, but not integer-to-pointer casts. Any program that assumes that different variables have different addresses bursts up in flame.
- Return
Related Links
- Tracking Issue for (runtime)
is_aligned
: Tracking Issue for pointer_is_aligned_to #96284 - Tracking Issue for const
align_offset
: Tracking Issue forconst_align_offset
#90962