-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Comments
Another approach to make |
cc @rust-lang/wg-const-eval opinions before we escalate to lang and libs? |
If a new intrinsic was added in the spirit of fn ptr_guaranteed_aligned_to<T>(ptr: *const T, align: usize) -> bool |
It seems useless to be able to perform any align checks in constants. 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. |
So why not use |
It doesn't catch insufficiently aligned pointers though. |
…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`
Currently,
core::slice::from_raw_parts
is notconst
, since it usesdebug_assert!
with a non-const
check:rust/library/core/src/slice/raw.rs
Line 89 in 5dab47d
is_aligned_and_not_null
can't be madeconst
, since it involves ptr->int cast to check the alignment:rust/library/core/src/intrinsics.rs
Lines 1950 to 1952 in 5dab47d
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
The text was updated successfully, but these errors were encountered: