Skip to content

Make slice::from_raw_parts[_mut] const #90011

Closed

Description

Currently, core::slice::from_raw_parts is not const, since it uses debug_assert! with a non-const check:

debug_assert!(is_aligned_and_not_null(data), "attempt to create unaligned or null slice");

is_aligned_and_not_null can't be made const, since it involves ptr->int cast to check the alignment:

pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
!ptr.is_null() && ptr as usize % mem::align_of::<T>() == 0
}

Recently const_eval_select intrinsic was implemented, it allows to run different code in CTFE and runtime. This, in turn, allows us to only make the alignment check in runtime and ignore it in the CTFE where it doesn't make much sense.

See also: #67456

cc @rust-lang/lang, @rust-lang/libs and @rust-lang/wg-const-eval (it seems like use of const_eval_select requires approval of all of the above teams)

@rustbot label +T-lang +T-libs +A-const-eval +A-const-fn

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

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation (MIR interpretation)A-const-fnArea: const fn foo(..) {..}. Pure functions which can be applied at compile time.T-langRelevant to the language team, which will review and decide on the PR/issue.T-libsRelevant to the library 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