Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make slice::from_raw_parts[_mut] const #90011

Closed
WaffleLapkin opened this issue Oct 18, 2021 · 7 comments · Fixed by #90377
Closed

Make slice::from_raw_parts[_mut] const #90011

WaffleLapkin opened this issue Oct 18, 2021 · 7 comments · Fixed by #90377
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@WaffleLapkin
Copy link
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

@rustbot rustbot added A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) A-const-fn T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 18, 2021
@WaffleLapkin
Copy link
Member Author

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
Copy link
Contributor

oli-obk commented Oct 18, 2021

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

@mbartlett21
Copy link
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
Copy link
Member

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

@mbartlett21
Copy link
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
Copy link
Member

So why not use const_eval_select?

@RalfJung
Copy link
Member

The CTFE catches invalid pointer dereferences anyways.

It doesn't catch insufficiently aligned pointers though.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 30, 2021
…arts, r=oli-obk

Make `core::slice::from_raw_parts[_mut]` const

Responses to rust-lang#90012 seem to allow `@rust-lang/wg-const-eval` to decide on use of `const_eval_select`, so we can make `core::slice::from_raw_parts[_mut]` const :)

---
This PR marks the following APIs as const:
```rust
// core::slice
pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T];
pub const unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T];
```
---

Resolves rust-lang#90011
r? `@oli-obk`
@bors bors closed this as completed in b531364 Oct 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: Constant evaluation, covers all const contexts (static, const fn, ...) T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants