Skip to content

Tracking Issue for const_pointer_is_aligned #104203

Open

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

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:
    1. Return true if the pointer is guaranteed to be aligned at runtime and false if we don't know whether the pointer is aligned at runtime. This is the current implementation.
    2. 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.
    3. 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
    4. 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.

Related Links

Footnotes

  1. https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    C-tracking-issueCategory: A tracking issue for an RFC or an unstable feature.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions