-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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_swap #83163
Comments
This got reverted again by #86003, but hopefully we can re-land it soon. |
I have reverted the constness reverts in #86295 |
Extend the const swap feature Adds the `const_swap` feature gate to three more swap functions. cc tracking issue rust-lang#83163 ```Rust impl<T> [T] { pub const fn swap(&mut self, a: usize, b: usize); pub const unsafe fn swap_unchecked(&mut self, a: usize, b: usize); } impl<T: ?Sized> *mut T { pub const unsafe fn swap(self, with: *mut T); }
This should be blocked on const mut refs, I believe. |
The following code used to work with #![feature(const_swap)]
#![feature(const_mut_refs)]
use std::{
mem::{self, MaybeUninit},
ptr,
};
const X: () = {
let mut ptr1 = &1;
let mut ptr2 = &2;
// Swap them, bytewise.
unsafe {
ptr::swap_nonoverlapping(
&mut ptr1 as *mut _ as *mut MaybeUninit<u8>,
&mut ptr2 as *mut _ as *mut MaybeUninit<u8>,
mem::size_of::<&i32>(),
);
}
// Make sure they still work.
assert!(*ptr1 == 2);
assert!(*ptr2 == 1);
}; That is a regression introduced by the new implementation of Not being able to work bytewise on pointer is a general limitation of CTFE, so we could document this as such. However, note that |
…ulacrum test const_copy to make sure bytewise pointer copies are working This is non-trivial; for `swap_nonoverlapping`, this is [not working](rust-lang#83163 (comment)).
test const_copy to make sure bytewise pointer copies are working This is non-trivial; for `swap_nonoverlapping`, this is [not working](rust-lang/rust#83163 (comment)).
So... we could easily stabilize Fixing this requires either an intrinsic for swapping (maybe only used by const-eval), or using const_heap... we have to allocate enough space to do the 3-copy version of swapping, as the more efficient "progressive" swapping is exactly what causes the problem. |
Here's the test for this: #[test]
fn test_const_swap() {
const {
let mut ptr1 = &1;
let mut ptr2 = &666;
// Swap ptr1 and ptr2, bytewise.
unsafe {
ptr::swap_nonoverlapping(
ptr::from_mut(&mut ptr1).cast::<u8>(),
ptr::from_mut(&mut ptr2).cast::<u8>(),
mem::size_of::<&i32>(),
);
}
// Make sure they still work.
assert!(*ptr1 == 666);
assert!(*ptr2 == 1);
};
const {
let mut ptr1 = &1;
let mut ptr2 = &666;
// Swap ptr1 and ptr2, bytewise. `swap` does not take a count
// so the best we can do is use an array.
type T = [u8; mem::size_of::<&i32>()];
unsafe {
ptr::swap(
ptr::from_mut(&mut ptr1).cast::<T>(),
ptr::from_mut(&mut ptr2).cast::<T>(),
);
}
// Make sure they still work.
assert!(*ptr1 == 666);
assert!(*ptr2 == 1);
};
} |
Just to comment on this, but |
Feel free to make a PR that proposes stabilizing |
After today's 1.83.0 release this is no longer blocked on |
Yeah that has been unblocked on nightly for a while. But it is blocked on the concern mentioned above.
EDIT: I updated the PR description to match that.
|
@rust-lang/libs-api I propose that we const-stabilize all of the above mentioned functions except for
|
@rfcbot fcp merge
Leaving out core::ptr::swap_nonoverlapping for now per the recommendation in #83163 (comment). Lastly there is <[_]>::swap_unchecked which is still unstable. Let's move its const stability out of this issue to be tracked as part of #88539 instead. |
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. |
Oh, good catch, I missed that. I will file a PR to move EDIT: PR is up at #133669. |
…lnay Move some functions out of const_swap feature gate - `swap_unchecked` is still unstable as a regular fn, so that feature gate can also cover its constness. - `swap_nonoverlapping` isn't ready to be stabilized yet, so make it a different feature gate. Part of rust-lang#83163, rust-lang#88539, rust-lang#133668
…lnay Move some functions out of const_swap feature gate - `swap_unchecked` is still unstable as a regular fn, so that feature gate can also cover its constness. - `swap_nonoverlapping` isn't ready to be stabilized yet, so make it a different feature gate. Part of rust-lang#83163, rust-lang#88539, rust-lang#133668
Rollup merge of rust-lang#133669 - RalfJung:const_swap_splitup, r=dtolnay Move some functions out of const_swap feature gate - `swap_unchecked` is still unstable as a regular fn, so that feature gate can also cover its constness. - `swap_nonoverlapping` isn't ready to be stabilized yet, so make it a different feature gate. Part of rust-lang#83163, rust-lang#88539, rust-lang#133668
@Amanieu @BurntSushi @joshtriplett @m-ou-se FCP checkbox reminder. :) |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
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. |
Feature gate:
#![feature(const_swap)]
This is a tracking issue for making the functions
mem::swap
andptr::swap[_nonoverlapping]
and some other swap related functionsconst fn
.Public API
Steps / History
mem
andptr
): Constifycopy
related functions #83091[T]
and*mut T )
Extend the const swap feature #90644NonNull
methods stabilized,const
ness moved to this gate Stabilizenon_null_convenience
#124498Unresolved Questions
The text was updated successfully, but these errors were encountered: