-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Tracking issue for const slice::from_raw_parts_mut (const_slice_from_raw_parts_mut) #67456
Comments
…ts, r=dtolnay Make ptr::slice_from_raw_parts a const fn available under a feature flag A first step in the direction of rust-lang#67456 . This makes `ptr::slice_from_raw_parts` and `ptr::slice_from_raw_parts_mut` available as a const fn under a feature flag.
…ts, r=dtolnay Make ptr::slice_from_raw_parts a const fn available under a feature flag A first step in the direction of rust-lang#67456 . This makes `ptr::slice_from_raw_parts` and `ptr::slice_from_raw_parts_mut` available as a const fn under a feature flag.
Is there particular reason why it remains gated by feature flag instead of just being const fn as it is? |
The constant versions of function's and methods in libcore/libstd need a period of time under a feature flag before they can be stabilized as a const function/method. Const functions are 'relatively' new, and the ability of what one can do inside isn't fully determined yet I think. |
@DutchGhost would it make sense for the Checking the alignment will need support from the MIR interpreter, see #62420 |
I'd say this indeed should fall under the same feature, as you wouldnt want to have 2 seperate features you need to track for really just one thing. Also I think once #79684 has landed, having a |
Cc. #57563 |
This can now be implemented it seems. But Im not sure whether its decided yet what the plan is on allowing to cast pointers to usizes in const contexts, which is needed for #![feature(const_ptr_is_null)]
#![feature(const_raw_ptr_to_usize_cast)]
#![feature(const_raw_ptr_deref)]
#![feature(const_slice_from_raw_parts)]
#![feature(const_panic)]
mod slice {
use core::mem;
use core::ptr;
pub(crate) const unsafe fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
!ptr.is_null() && ptr as usize % mem::align_of::<T>() == 0
}
pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
debug_assert!(
is_aligned_and_not_null(data),
"attempt to create unaligned or null slice"
);
debug_assert!(
mem::size_of::<T>().saturating_mul(len) <= isize::MAX as usize,
"attempt to create slice covering at least half the address space"
);
// SAFETY: the caller must uphold the safety contract for `from_raw_parts`.
unsafe { &*ptr::slice_from_raw_parts(data, len) }
}
pub const fn from_ref<T>(reference: &T) -> &[T] {
unsafe {
from_raw_parts(reference, 1)
}
}
}
const X: &[usize] = slice::from_ref(&1);
fn main() {
dbg!(X);
} |
@DutchGhost
|
This comment has been minimized.
This comment has been minimized.
To clarify: this issue is currently tracking the following APIs being // core::ptr
pub const fn slice_from_raw_parts<T>(data: *const T, len: usize) -> *const [T];
pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T];
// 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]; The former two methods were implemented in #67462 and the latter two were implemented in #90377.
|
As an interested party: is there a path to eventual stabilization here, at least for some of the incremental work that is required? For example, Happy to try and learn/help to get it over the line. 🙏🏻 |
I think that these API are ready for stabilization 🤔 APIs that return |
…=dtolnay Support `char::encode_utf8` in const scenarios. This PR implements [`rust-lang/rfcs#3696`](rust-lang/rfcs#3696). This assumes [`const_slice_from_raw_parts_mut`](rust-lang#67456).
@rust-lang/libs-api this should be ready. :) It const-stabilizes some long-stable functions, which is possible now that |
Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
Support `char::encode_utf8` in const scenarios. This PR implements [`rust-lang/rfcs#3696`](rust-lang/rfcs#3696). This assumes [`const_slice_from_raw_parts_mut`](rust-lang/rust#67456).
@BurntSushi @joshtriplett @m-ou-se |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
Weird. My box was ticked, but FCP didn't get triggered. I unticked and re-ticked my box. |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
…om_raw_parts_mut, r=workingjubilee Stabilize `const_slice_from_raw_parts_mut` Stabilizes rust-lang#67456, since rust-lang#57349 has been stabilized. Stabilized const API: ```rust // core::ptr pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T]; // core::slice pub const unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T]; // core::ptr::NonNull pub const fn slice_from_raw_parts(data: NonNull<T>, len: usize) -> Self ``` Closes rust-lang#67456. r? libs-api
…om_raw_parts_mut, r=workingjubilee Stabilize `const_slice_from_raw_parts_mut` Stabilizes rust-lang#67456, since rust-lang#57349 has been stabilized. Stabilized const API: ```rust // core::ptr pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T]; // core::slice pub const unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T]; // core::ptr::NonNull pub const fn slice_from_raw_parts(data: NonNull<T>, len: usize) -> Self ``` Closes rust-lang#67456. r? libs-api
…_raw_parts_mut, r=workingjubilee Stabilize `const_slice_from_raw_parts_mut` Stabilizes rust-lang#67456, since rust-lang#57349 has been stabilized. Stabilized const API: ```rust // core::ptr pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T]; // core::slice pub const unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T]; // core::ptr::NonNull pub const fn slice_from_raw_parts(data: NonNull<T>, len: usize) -> Self ``` Closes rust-lang#67456. r? libs-api
Rollup merge of rust-lang#130403 - eduardosm:stabilize-const_slice_from_raw_parts_mut, r=workingjubilee Stabilize `const_slice_from_raw_parts_mut` Stabilizes rust-lang#67456, since rust-lang#57349 has been stabilized. Stabilized const API: ```rust // core::ptr pub const fn slice_from_raw_parts_mut<T>(data: *mut T, len: usize) -> *mut [T]; // core::slice pub const unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T]; // core::ptr::NonNull pub const fn slice_from_raw_parts(data: NonNull<T>, len: usize) -> Self ``` Closes rust-lang#67456. r? libs-api
Make
slice::from_raw_parts
andslice::from_raw_parts_mut
a const fn available under a feature flag.This would require a change in the
ptr
module as well, as slice module just forwards to it.slice::from_raw_parts[mut]
is used in alot of places (e.gslice::from_ref[mut]
, which would get one step closer into constification ifslice::from_raw_parts[mut]
is a const fn.Here is a little playground to show it's possible:
https://play.rust-lang.org/?version=nightly&mode=release&edition=2018&gist=dd5c506a3e082c619f557d972e9956ff
In order to get this working, the following functions and functionalities need to be constified:
ptr::slice_from_raw_parts
(Make ptr::slice_from_raw_parts a const fn available under a feature flag #67462)Implementation PR for
ptr::slice_from_raw_parts
: #67462Partially stabilized in #97522; remaining unstable functions:
The text was updated successfully, but these errors were encountered: