-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Kill remaining uses of mem::uninitialized in libcore, liballoc #57045
Conversation
r? @bluss (rust_highfive has picked a reviewer for you, use r? to override) |
I was surprised recently to find that |
I think it makes a lot of sense to migrate more things to it to get some experience with the API before stabilizing. |
// Creating a `[MaybeUninit; N]` array by first creating a | ||
// `MaybeUninit<[MaybeUninit; N]>`; the `into_inner` is safe because the inner | ||
// array does not require initialization. | ||
keys: MaybeUninit::uninitialized().into_inner(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do I have a terrible feeling I'm going to be reading MaybeUninit::uninitialized().into_inner()
everywhere rather than mem::uninitialized()
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it possible to wrap it into a MaybeUninit::uninitialized_array() -> [MaybeUninit; #]
static method that has the pre-copy as a doc-comment so as to remove the 7 times that exact comment is given? or is that (blocked by) what Centril meant by #49147 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I mentioned above, this can be made safe once #49147 is stable. Once that happens, you can just write [MaybeUninit::uninitialized(), N]
directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do I have a terrible feeling I'm going to be reading MaybeUninit::uninitialized().into_inner() everywhere rather than mem::uninitialized()...
In my defense, that's not the only change here. ;)
The main problem is that we have pretty bad support for working with arrays, and arrays of MaybeUninit
are no exceptions. If we had functions generic over array lengths, this all would be much easier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah-- it's just that working with uninitialized arrays is my only current use of mem::uninitialized
, and it used to use ManuallyDrop
etc. but that route is now broken. Seeing this working-around of MaybeUninit
's semantics makes me sad, but I understand it's a stepping-stone to the eventual goal.
I have added a macro to create uninitialized arrays. Unfortunately it requires repeating the array length, but there will be a compile error if you use the wrong one. This removes all the new |
It's too bad the names On the other hand, it's nice to be explicit. |
I actually feel like the methods on the slice should have been called differently to contain something like But well, too late for any of that. What I'd really like to do is make these functions methods that take |
This comment has been minimized.
This comment has been minimized.
36dcd75
to
4a6497b
Compare
This comment has been minimized.
This comment has been minimized.
4a6497b
to
b9595b3
Compare
Rebased. However, stdsimd introduced some new uses of |
This comment has been minimized.
This comment has been minimized.
b9595b3
to
8e312e7
Compare
Updated stdsimd. |
So, how do we proceed with this? |
☔ The latest upstream changes (presumably #57354) made this pull request unmergeable. Please resolve the merge conflicts. |
8e312e7
to
f4dd732
Compare
☔ The latest upstream changes (presumably #57765) made this pull request unmergeable. Please resolve the merge conflicts. |
f4dd732
to
678bd35
Compare
⌛ Testing commit 6a52ca3 with merge acec5ebdc09eed0dc05329bcfea27749b48a4ee0... |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Fixed debug tests. @bors r=SimonSapin |
📌 Commit 489a792 has been approved by |
Kill remaining uses of mem::uninitialized in libcore, liballoc Kill remaining uses of mem::uninitialized in libcore and liballoc, and enable a lint that will warn when uses are added again in the future. To avoid casting raw pointers around (which is always very dangerous because it is not typechecked at all -- it doesn't even get the "same size" sanity check that `transmute` gets), I also added two new functions to `MaybeUninit`: ```rust /// Get a pointer to the first contained values. pub fn first_ptr(this: &[MaybeUninit<T>]) -> *const T { this as *const [MaybeUninit<T>] as *const T } /// Get a mutable pointer to the first contained values. pub fn first_mut_ptr(this: &mut [MaybeUninit<T>]) -> *mut T { this as *mut [MaybeUninit<T>] as *mut T } ``` I changed some of the existing code to use array-of-`MaybeUninit` instead of `MaybeUninit`-of-array, successfully removing raw pointer casts there as well.
💔 Test failed - checks-travis |
Your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem. Click to expand the log.
I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact |
@bors retry Seems spurious. |
…apin Kill remaining uses of mem::uninitialized in libcore, liballoc Kill remaining uses of mem::uninitialized in libcore and liballoc, and enable a lint that will warn when uses are added again in the future. To avoid casting raw pointers around (which is always very dangerous because it is not typechecked at all -- it doesn't even get the "same size" sanity check that `transmute` gets), I also added two new functions to `MaybeUninit`: ```rust /// Get a pointer to the first contained values. pub fn first_ptr(this: &[MaybeUninit<T>]) -> *const T { this as *const [MaybeUninit<T>] as *const T } /// Get a mutable pointer to the first contained values. pub fn first_mut_ptr(this: &mut [MaybeUninit<T>]) -> *mut T { this as *mut [MaybeUninit<T>] as *mut T } ``` I changed some of the existing code to use array-of-`MaybeUninit` instead of `MaybeUninit`-of-array, successfully removing raw pointer casts there as well.
Rollup of 7 pull requests Successful merges: - #57045 (Kill remaining uses of mem::uninitialized in libcore, liballoc) - #57674 (Avoid erase_regions_ty queries if there are no regions to erase) - #57833 (Print a slightly clearer message when failing to launch a thread) - #57859 (Fix invalid background color) - #57904 (add typo suggestion to unknown attribute error) - #57915 (Pretty print `$crate` as `crate` or `crate_name` in more cases) - #57950 (Extend E0106, E0261) Failed merges: r? @ghost
…Mark-Simulacrum Test gdb pretty printing more and fix overzealous type substitution Adresses a problem concerning printing BTreeMap / BTreeSet data in gdb: when the key or value type name contains substring "LeafNode", and the map has multiple nodes (e.g. more than 11 elements), printing causes an exception. E.g. ``` rustc -g - <<EOF use std::collections::BTreeMap; struct MyLeafNode(i8); fn main() { let m: BTreeMap<i8, MyLeafNode> = (0..12).map(|i| (i, MyLeafNode(i))).collect(); assert!(!m.is_empty()); } EOF ``` ``` $ rust-gdb rust_out (gdb) b 7 (gdb) r (gdb) p m $1 = BTreeMap<i8, rust_out::MyLeafNode>(len: 12)Python Exception <class 'gdb.error'> No type named alloc::collections::btree::node::InternalNode<i8, rust_out::MyInternalNode>.: use std::collections::BTreeMap; ``` The code was written in rust-lang#56144 by @tromey (and later touched upon by @RalfJung in rust-lang#57045, but I think that had nothing to do with the issues in this PR).
Kill remaining uses of mem::uninitialized in libcore and liballoc, and enable a lint that will warn when uses are added again in the future.
To avoid casting raw pointers around (which is always very dangerous because it is not typechecked at all -- it doesn't even get the "same size" sanity check that
transmute
gets), I also added two new functions toMaybeUninit
:I changed some of the existing code to use array-of-
MaybeUninit
instead ofMaybeUninit
-of-array, successfully removing raw pointer casts there as well.