Skip to content

Make slice::from_raw_parts[_mut] const #90011

Closed
@WaffleLapkin

Description

@WaffleLapkin

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)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