-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Add PhantomData
marker for dropck to BTreeMap
#99413
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,6 +178,8 @@ pub struct BTreeMap< | |
length: usize, | ||
/// `ManuallyDrop` to control drop order (needs to be dropped after all the nodes). | ||
pub(super) alloc: ManuallyDrop<A>, | ||
// For dropck; the `Box` avoids making the `Unpin` impl more strict than before | ||
_marker: PhantomData<crate::boxed::Box<(K, V)>>, | ||
} | ||
|
||
#[stable(feature = "btree_drop", since = "1.7.0")] | ||
|
@@ -187,6 +189,19 @@ unsafe impl<#[may_dangle] K, #[may_dangle] V, A: Allocator + Clone> Drop for BTr | |
} | ||
} | ||
|
||
// FIXME: This implementation is "wrong", but changing it would be a breaking change. | ||
// (The bounds of the automatic `UnwindSafe` implementation have been like this since Rust 1.50.) | ||
// Maybe we can fix it nonetheless with a crater run, or if the `UnwindSafe` | ||
// traits are deprecated, or disarmed (no longer causing hard errors) in the future. | ||
#[stable(feature = "btree_unwindsafe", since = "1.64.0")] | ||
Comment on lines
+192
to
+196
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be worth just correcting (so, breaking) this. The damage is probably very small, if any. @rust-lang/libs-api Opinions? |
||
impl<K, V, A: Allocator + Clone> core::panic::UnwindSafe for BTreeMap<K, V, A> | ||
where | ||
A: core::panic::UnwindSafe, | ||
K: core::panic::RefUnwindSafe, | ||
V: core::panic::RefUnwindSafe, | ||
{ | ||
} | ||
|
||
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<K: Clone, V: Clone, A: Allocator + Clone> Clone for BTreeMap<K, V, A> { | ||
fn clone(&self) -> BTreeMap<K, V, A> { | ||
|
@@ -204,6 +219,7 @@ impl<K: Clone, V: Clone, A: Allocator + Clone> Clone for BTreeMap<K, V, A> { | |
root: Some(Root::new(alloc.clone())), | ||
length: 0, | ||
alloc: ManuallyDrop::new(alloc), | ||
_marker: PhantomData, | ||
}; | ||
|
||
{ | ||
|
@@ -567,7 +583,7 @@ impl<K, V> BTreeMap<K, V> { | |
#[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] | ||
#[must_use] | ||
pub const fn new() -> BTreeMap<K, V> { | ||
BTreeMap { root: None, length: 0, alloc: ManuallyDrop::new(Global) } | ||
BTreeMap { root: None, length: 0, alloc: ManuallyDrop::new(Global), _marker: PhantomData } | ||
} | ||
} | ||
|
||
|
@@ -593,6 +609,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> { | |
root: mem::replace(&mut self.root, None), | ||
length: mem::replace(&mut self.length, 0), | ||
alloc: self.alloc.clone(), | ||
_marker: PhantomData, | ||
}); | ||
} | ||
|
||
|
@@ -615,7 +632,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> { | |
/// ``` | ||
#[unstable(feature = "btreemap_alloc", issue = "32838")] | ||
pub fn new_in(alloc: A) -> BTreeMap<K, V, A> { | ||
BTreeMap { root: None, length: 0, alloc: ManuallyDrop::new(alloc) } | ||
BTreeMap { root: None, length: 0, alloc: ManuallyDrop::new(alloc), _marker: PhantomData } | ||
} | ||
} | ||
|
||
|
@@ -1320,7 +1337,12 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> { | |
let (new_left_len, right_len) = Root::calc_split_length(total_num, &left_root, &right_root); | ||
self.length = new_left_len; | ||
|
||
BTreeMap { root: Some(right_root), length: right_len, alloc: self.alloc.clone() } | ||
BTreeMap { | ||
root: Some(right_root), | ||
length: right_len, | ||
alloc: self.alloc.clone(), | ||
_marker: PhantomData, | ||
} | ||
} | ||
|
||
/// Creates an iterator that visits all elements (key-value pairs) in | ||
|
@@ -1445,7 +1467,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> { | |
let mut root = Root::new(alloc.clone()); | ||
let mut length = 0; | ||
root.bulk_push(DedupSortedIter::new(iter.into_iter()), &mut length, alloc.clone()); | ||
BTreeMap { root: Some(root), length, alloc: ManuallyDrop::new(alloc) } | ||
BTreeMap { root: Some(root), length, alloc: ManuallyDrop::new(alloc), _marker: PhantomData } | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
struct PrintOnDrop<'a>(&'a str); | ||
|
||
impl Drop for PrintOnDrop<'_> { | ||
fn drop(&mut self) { | ||
println!("printint: {}", self.0); | ||
} | ||
} | ||
|
||
use std::collections::BTreeMap; | ||
use std::iter::FromIterator; | ||
|
||
fn main() { | ||
let s = String::from("Hello World!"); | ||
let _map = BTreeMap::from_iter([((), PrintOnDrop(&s))]); | ||
drop(s); //~ ERROR cannot move out of `s` because it is borrowed | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
error[E0505]: cannot move out of `s` because it is borrowed | ||
--> $DIR/btreemap_dropck.rs:15:10 | ||
| | ||
LL | let _map = BTreeMap::from_iter([((), PrintOnDrop(&s))]); | ||
| -- borrow of `s` occurs here | ||
LL | drop(s); | ||
| ^ move out of `s` occurs here | ||
LL | } | ||
| - borrow might be used here, when `_map` is dropped and runs the `Drop` code for type `BTreeMap` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0505`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think this is yet another reminder for us that we need something more intuitive than PhantomData. PhantomData is used to control Send, Sync, dropck, UnwindSafe, UnwindSafeRef, variance, and Unpin. Possibly more (in the future). It happens so often that we try to control just some of these, and accidentally end up affecting one of the others too. (Like what would've happened here if (K, V) wasn't wrapped in a Box.)
cc @rust-lang/lang
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.
This reminds me of a crate that I wrote half a year ago. https://docs.rs/phtm/1.0.3/phtm/