Skip to content

Make slice::from_raw_parts[_mut] const #90011

@WaffleLapkin

Description

@WaffleLapkin
Member

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

Activity

added
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
T-langRelevant to the language team
T-libsRelevant to the library team, which will review and decide on the PR/issue.
on Oct 18, 2021
WaffleLapkin

WaffleLapkin commented on Oct 18, 2021

@WaffleLapkin
MemberAuthor

Another approach to make slice::from_raw_parts[_mut] const would be to disable the checks altogether, but since we already have const_eval_select, that seems unreasonable.

oli-obk

oli-obk commented on Oct 18, 2021

@oli-obk
Contributor

cc @rust-lang/wg-const-eval opinions before we escalate to lang and libs?

mbartlett21

mbartlett21 commented on Oct 18, 2021

@mbartlett21
Contributor

If a new intrinsic was added in the spirit of ptr_guaranteed_eq and ptr_guaranteed_ne, this could possibly provide a solution without const_eval_select. Something like:

fn ptr_guaranteed_aligned_to<T>(ptr: *const T, align: usize) -> bool
fee1-dead

fee1-dead commented on Oct 18, 2021

@fee1-dead
Member

It seems useless to be able to perform any align checks in constants. The CTFE catches invalid pointer dereferences anyways.

mbartlett21

mbartlett21 commented on Oct 18, 2021

@mbartlett21
Contributor

The CTFE catches invalid pointer dereferences anyways.

Yes, but we also want to catch invalid pointer dereferences when we are not in CTFE, hence the idea of the function/intrinsic.

fee1-dead

fee1-dead commented on Oct 18, 2021

@fee1-dead
Member

So why not use const_eval_select?

RalfJung

RalfJung commented on Oct 18, 2021

@RalfJung
Member

The CTFE catches invalid pointer dereferences anyways.

It doesn't catch insufficiently aligned pointers though.

added 2 commits that reference this issue on Oct 30, 2021
015d7f7
b531364
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)T-langRelevant to the language teamT-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

      Participants

      @RalfJung@oli-obk@mbartlett21@WaffleLapkin@fee1-dead

      Issue actions

        Make `slice::from_raw_parts[_mut]` const · Issue #90011 · rust-lang/rust