Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override Box::<[T]>::clone_from #72499

Merged
merged 3 commits into from May 30, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/liballoc/boxed.rs
Expand Up @@ -1090,6 +1090,14 @@ impl<T: Clone> Clone for Box<[T]> {
fn clone(&self) -> Self {
self.to_vec().into_boxed_slice()
}

fn clone_from(&mut self, other: &Self) {
if self.len() == other.len() {
self.clone_from_slice(&other);
} else {
*self = other.clone();
}
}
}

#[stable(feature = "box_borrow", since = "1.1.0")]
Expand Down
36 changes: 36 additions & 0 deletions src/liballoc/tests/boxed.rs
Expand Up @@ -16,3 +16,39 @@ fn unitialized_zero_size_box() {
NonNull::<MaybeUninit<String>>::dangling().as_ptr(),
);
}

#[derive(Clone, PartialEq, Eq, Debug)]
struct Dummy {
_data: u8,
}

#[test]
fn box_clone_and_clone_from_equivalence() {
for size in (0..8).map(|i| 2usize.pow(i)) {
let control = vec![Dummy { _data: 42 }; size].into_boxed_slice();
let clone = control.clone();
let mut copy = vec![Dummy { _data: 84 }; size].into_boxed_slice();
copy.clone_from(&control);
assert_eq!(control, clone);
assert_eq!(control, copy);
}
}

#[test]
fn box_clone_from_ptr_stability() {
for size in (0..8).map(|i| 2usize.pow(i)) {
let control = vec![Dummy { _data: 42 }; size].into_boxed_slice();
let mut copy = vec![Dummy { _data: 84 }; size].into_boxed_slice();
let copy_raw = copy.as_ptr() as usize;
copy.clone_from(&control);
assert_eq!(copy.as_ptr() as usize, copy_raw);
}

for size in (0..8).map(|i| 2usize.pow(i)) {
let control = vec![Dummy { _data: 42 }; size].into_boxed_slice();
let mut copy = vec![Dummy { _data: 84 }; size + 1].into_boxed_slice();
let copy_raw = copy.as_ptr() as usize;
copy.clone_from(&control);
assert_ne!(copy.as_ptr() as usize, copy_raw);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this would be flaky. The allocator can put these at the same address if the compiler decides to drop the old one before allocating the new one.

What sort of regression is this test hoping to catch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, I made this test while I was trying to write them without UB or unsafe. It doesn't make much sense now that you point that out

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other test on the other hand might not catch a regression for the same reason 馃 but I can't think of a another way to test this. Basically it might give a false positive but never a false negative

}
}