Rollup merge of #97494 - est31:remove_box_alloc_tests, r=Dylan-DPC

Use Box::new() instead of box syntax in library tests

The tests inside `library/*` have no reason to use `box` syntax as they have 0 performance relevance. Therefore, we can safely remove them (instead of having to use alternatives like the one in #97293).
This commit is contained in:
Dylan DPC 2022-05-30 14:33:48 +02:00 committed by GitHub
commit 0ed320bdb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 86 additions and 82 deletions

View File

@ -25,6 +25,6 @@ fn allocate_zeroed() {
#[cfg_attr(miri, ignore)] // isolated Miri does not support benchmarks
fn alloc_owned_small(b: &mut Bencher) {
b.iter(|| {
let _: Box<_> = box 10;
let _: Box<_> = Box::new(10);
})
}

View File

@ -183,22 +183,22 @@ fn test_push() {
#[test]
fn test_push_unique() {
let mut heap = BinaryHeap::<Box<_>>::from(vec![box 2, box 4, box 9]);
let mut heap = BinaryHeap::<Box<_>>::from(vec![Box::new(2), Box::new(4), Box::new(9)]);
assert_eq!(heap.len(), 3);
assert!(**heap.peek().unwrap() == 9);
heap.push(box 11);
heap.push(Box::new(11));
assert_eq!(heap.len(), 4);
assert!(**heap.peek().unwrap() == 11);
heap.push(box 5);
heap.push(Box::new(5));
assert_eq!(heap.len(), 5);
assert!(**heap.peek().unwrap() == 11);
heap.push(box 27);
heap.push(Box::new(27));
assert_eq!(heap.len(), 6);
assert!(**heap.peek().unwrap() == 27);
heap.push(box 3);
heap.push(Box::new(3));
assert_eq!(heap.len(), 7);
assert!(**heap.peek().unwrap() == 27);
heap.push(box 103);
heap.push(Box::new(103));
assert_eq!(heap.len(), 8);
assert!(**heap.peek().unwrap() == 103);
}

View File

@ -791,7 +791,7 @@ impl<T> LinkedList<T> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn push_front(&mut self, elt: T) {
self.push_front_node(box Node::new(elt));
self.push_front_node(Box::new(Node::new(elt)));
}
/// Removes the first element and returns it, or `None` if the list is
@ -834,7 +834,7 @@ impl<T> LinkedList<T> {
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn push_back(&mut self, elt: T) {
self.push_back_node(box Node::new(elt));
self.push_back_node(Box::new(Node::new(elt)));
}
/// Removes the last element from a list and returns it, or `None` if

View File

@ -12,20 +12,20 @@ fn test_basic() {
assert_eq!(m.pop_front(), None);
assert_eq!(m.pop_back(), None);
assert_eq!(m.pop_front(), None);
m.push_front(box 1);
assert_eq!(m.pop_front(), Some(box 1));
m.push_back(box 2);
m.push_back(box 3);
m.push_front(Box::new(1));
assert_eq!(m.pop_front(), Some(Box::new(1)));
m.push_back(Box::new(2));
m.push_back(Box::new(3));
assert_eq!(m.len(), 2);
assert_eq!(m.pop_front(), Some(box 2));
assert_eq!(m.pop_front(), Some(box 3));
assert_eq!(m.pop_front(), Some(Box::new(2)));
assert_eq!(m.pop_front(), Some(Box::new(3)));
assert_eq!(m.len(), 0);
assert_eq!(m.pop_front(), None);
m.push_back(box 1);
m.push_back(box 3);
m.push_back(box 5);
m.push_back(box 7);
assert_eq!(m.pop_front(), Some(box 1));
m.push_back(Box::new(1));
m.push_back(Box::new(3));
m.push_back(Box::new(5));
m.push_back(Box::new(7));
assert_eq!(m.pop_front(), Some(Box::new(1)));
let mut n = LinkedList::new();
n.push_front(2);

View File

@ -369,7 +369,8 @@ impl<T> Rc<T> {
// if the weak pointer is stored inside the strong one.
unsafe {
Self::from_inner(
Box::leak(box RcBox { strong: Cell::new(1), weak: Cell::new(1), value }).into(),
Box::leak(Box::new(RcBox { strong: Cell::new(1), weak: Cell::new(1), value }))
.into(),
)
}
}
@ -433,11 +434,11 @@ impl<T> Rc<T> {
{
// Construct the inner in the "uninitialized" state with a single
// weak reference.
let uninit_ptr: NonNull<_> = Box::leak(box RcBox {
let uninit_ptr: NonNull<_> = Box::leak(Box::new(RcBox {
strong: Cell::new(0),
weak: Cell::new(1),
value: mem::MaybeUninit::<T>::uninit(),
})
}))
.into();
let init_ptr: NonNull<RcBox<T>> = uninit_ptr.cast();

View File

@ -32,7 +32,7 @@ fn test_simple_clone() {
#[test]
fn test_destructor() {
let x: Rc<Box<_>> = Rc::new(box 5);
let x: Rc<Box<_>> = Rc::new(Box::new(5));
assert_eq!(**x, 5);
}
@ -153,7 +153,7 @@ fn try_unwrap() {
#[test]
fn into_from_raw() {
let x = Rc::new(box "hello");
let x = Rc::new(Box::new("hello"));
let y = x.clone();
let x_ptr = Rc::into_raw(x);
@ -192,7 +192,7 @@ fn test_into_from_raw_unsized() {
#[test]
fn into_from_weak_raw() {
let x = Rc::new(box "hello");
let x = Rc::new(Box::new("hello"));
let y = Rc::downgrade(&x);
let y_ptr = Weak::into_raw(y);
@ -409,7 +409,7 @@ fn test_clone_from_slice_panic() {
#[test]
fn test_from_box() {
let b: Box<u32> = box 123;
let b: Box<u32> = Box::new(123);
let r: Rc<u32> = Rc::from(b);
assert_eq!(*r, 123);
@ -438,7 +438,7 @@ fn test_from_box_trait() {
use std::fmt::Display;
use std::string::ToString;
let b: Box<dyn Display> = box 123;
let b: Box<dyn Display> = Box::new(123);
let r: Rc<dyn Display> = Rc::from(b);
assert_eq!(r.to_string(), "123");
@ -448,7 +448,7 @@ fn test_from_box_trait() {
fn test_from_box_trait_zero_sized() {
use std::fmt::Debug;
let b: Box<dyn Debug> = box ();
let b: Box<dyn Debug> = Box::new(());
let r: Rc<dyn Debug> = Rc::from(b);
assert_eq!(format!("{r:?}"), "()");

View File

@ -343,11 +343,11 @@ impl<T> Arc<T> {
pub fn new(data: T) -> Arc<T> {
// Start the weak pointer count as 1 which is the weak pointer that's
// held by all the strong pointers (kinda), see std/rc.rs for more info
let x: Box<_> = box ArcInner {
let x: Box<_> = Box::new(ArcInner {
strong: atomic::AtomicUsize::new(1),
weak: atomic::AtomicUsize::new(1),
data,
};
});
unsafe { Self::from_inner(Box::leak(x).into()) }
}
@ -411,11 +411,11 @@ impl<T> Arc<T> {
{
// Construct the inner in the "uninitialized" state with a single
// weak reference.
let uninit_ptr: NonNull<_> = Box::leak(box ArcInner {
let uninit_ptr: NonNull<_> = Box::leak(Box::new(ArcInner {
strong: atomic::AtomicUsize::new(0),
weak: atomic::AtomicUsize::new(1),
data: mem::MaybeUninit::<T>::uninit(),
})
}))
.into();
let init_ptr: NonNull<ArcInner<T>> = uninit_ptr.cast();

View File

@ -103,7 +103,7 @@ fn try_unwrap() {
#[test]
fn into_from_raw() {
let x = Arc::new(box "hello");
let x = Arc::new(Box::new("hello"));
let y = x.clone();
let x_ptr = Arc::into_raw(x);
@ -142,7 +142,7 @@ fn test_into_from_raw_unsized() {
#[test]
fn into_from_weak_raw() {
let x = Arc::new(box "hello");
let x = Arc::new(Box::new("hello"));
let y = Arc::downgrade(&x);
let y_ptr = Weak::into_raw(y);
@ -467,7 +467,7 @@ fn test_clone_from_slice_panic() {
#[test]
fn test_from_box() {
let b: Box<u32> = box 123;
let b: Box<u32> = Box::new(123);
let r: Arc<u32> = Arc::from(b);
assert_eq!(*r, 123);
@ -496,7 +496,7 @@ fn test_from_box_trait() {
use std::fmt::Display;
use std::string::ToString;
let b: Box<dyn Display> = box 123;
let b: Box<dyn Display> = Box::new(123);
let r: Arc<dyn Display> = Arc::from(b);
assert_eq!(r.to_string(), "123");
@ -506,7 +506,7 @@ fn test_from_box_trait() {
fn test_from_box_trait_zero_sized() {
use std::fmt::Debug;
let b: Box<dyn Debug> = box ();
let b: Box<dyn Debug> = Box::new(());
let r: Arc<dyn Debug> = Arc::from(b);
assert_eq!(format!("{r:?}"), "()");

View File

@ -268,9 +268,9 @@ fn test_swap_remove_fail() {
fn test_swap_remove_noncopyable() {
// Tests that we don't accidentally run destructors twice.
let mut v: Vec<Box<_>> = Vec::new();
v.push(box 0);
v.push(box 0);
v.push(box 0);
v.push(Box::new(0));
v.push(Box::new(0));
v.push(Box::new(0));
let mut _e = v.swap_remove(0);
assert_eq!(v.len(), 2);
_e = v.swap_remove(1);
@ -296,7 +296,7 @@ fn test_push() {
#[test]
fn test_truncate() {
let mut v: Vec<Box<_>> = vec![box 6, box 5, box 4];
let mut v: Vec<Box<_>> = vec![Box::new(6), Box::new(5), Box::new(4)];
v.truncate(1);
let v = v;
assert_eq!(v.len(), 1);
@ -306,7 +306,7 @@ fn test_truncate() {
#[test]
fn test_clear() {
let mut v: Vec<Box<_>> = vec![box 6, box 5, box 4];
let mut v: Vec<Box<_>> = vec![Box::new(6), Box::new(5), Box::new(4)];
v.clear();
assert_eq!(v.len(), 0);
// If the unsafe block didn't drop things properly, we blow up here.
@ -1516,14 +1516,14 @@ fn test_mut_last() {
#[test]
fn test_to_vec() {
let xs: Box<_> = box [1, 2, 3];
let xs: Box<_> = Box::new([1, 2, 3]);
let ys = xs.to_vec();
assert_eq!(ys, [1, 2, 3]);
}
#[test]
fn test_in_place_iterator_specialization() {
let src: Box<[usize]> = box [1, 2, 3];
let src: Box<[usize]> = Box::new([1, 2, 3]);
let src_ptr = src.as_ptr();
let sink: Box<_> = src.into_vec().into_iter().map(std::convert::identity).collect();
let sink_ptr = sink.as_ptr();

View File

@ -266,8 +266,8 @@ fn test_clone() {
#[test]
fn test_clone_from() {
let mut v = vec![];
let three: Vec<Box<_>> = vec![box 1, box 2, box 3];
let two: Vec<Box<_>> = vec![box 4, box 5];
let three: Vec<Box<_>> = vec![Box::new(1), Box::new(2), Box::new(3)];
let two: Vec<Box<_>> = vec![Box::new(4), Box::new(5)];
// zero, long
v.clone_from(&three);
assert_eq!(v, three);
@ -407,11 +407,11 @@ fn test_dedup_by() {
#[test]
fn test_dedup_unique() {
let mut v0: Vec<Box<_>> = vec![box 1, box 1, box 2, box 3];
let mut v0: Vec<Box<_>> = vec![Box::new(1), Box::new(1), Box::new(2), Box::new(3)];
v0.dedup();
let mut v1: Vec<Box<_>> = vec![box 1, box 2, box 2, box 3];
let mut v1: Vec<Box<_>> = vec![Box::new(1), Box::new(2), Box::new(2), Box::new(3)];
v1.dedup();
let mut v2: Vec<Box<_>> = vec![box 1, box 2, box 3, box 3];
let mut v2: Vec<Box<_>> = vec![Box::new(1), Box::new(2), Box::new(3), Box::new(3)];
v2.dedup();
// If the boxed pointers were leaked or otherwise misused, valgrind
// and/or rt should raise errors.

View File

@ -24,8 +24,11 @@ fn any_referenced() {
#[test]
fn any_owning() {
let (a, b, c) =
(box 5_usize as Box<dyn Any>, box TEST as Box<dyn Any>, box Test as Box<dyn Any>);
let (a, b, c) = (
Box::new(5_usize) as Box<dyn Any>,
Box::new(TEST) as Box<dyn Any>,
Box::new(Test) as Box<dyn Any>,
);
assert!(a.is::<usize>());
assert!(!b.is::<usize>());
@ -58,7 +61,7 @@ fn any_downcast_ref() {
#[test]
fn any_downcast_mut() {
let mut a = 5_usize;
let mut b: Box<_> = box 7_usize;
let mut b: Box<_> = Box::new(7_usize);
let a_r = &mut a as &mut dyn Any;
let tmp: &mut usize = &mut *b;

View File

@ -8,8 +8,8 @@ fn test_borrowed_clone() {
#[test]
fn test_clone_from() {
let a = box 5;
let mut b = box 10;
let a = Box::new(5);
let mut b = Box::new(10);
b.clone_from(&a);
assert_eq!(*b, 5);
}

View File

@ -78,7 +78,8 @@ fn test_rev_rposition() {
#[test]
#[should_panic]
fn test_rposition_panic() {
let v: [(Box<_>, Box<_>); 4] = [(box 0, box 0), (box 0, box 0), (box 0, box 0), (box 0, box 0)];
let u = (Box::new(0), Box::new(0));
let v: [(Box<_>, Box<_>); 4] = [u.clone(), u.clone(), u.clone(), u];
let mut i = 0;
v.iter().rposition(|_elt| {
if i == 2 {

View File

@ -3,7 +3,6 @@
#![feature(array_methods)]
#![feature(array_windows)]
#![feature(bench_black_box)]
#![feature(box_syntax)]
#![feature(cell_update)]
#![feature(const_assume)]
#![feature(const_black_box)]

View File

@ -7,7 +7,7 @@ use core::option::*;
#[test]
fn test_get_ptr() {
unsafe {
let x: Box<_> = box 0;
let x: Box<_> = Box::new(0);
let addr_x: *const isize = mem::transmute(&*x);
let opt = Some(x);
let y = opt.unwrap();
@ -315,7 +315,7 @@ fn test_collect() {
// test that it does not take more elements than it needs
let mut functions: [Box<dyn Fn() -> Option<()>>; 3] =
[box || Some(()), box || None, box || panic!()];
[Box::new(|| Some(())), Box::new(|| None), Box::new(|| panic!())];
let v: Option<Vec<()>> = functions.iter_mut().map(|f| (*f)()).collect();

View File

@ -69,7 +69,7 @@ fn test_collect() {
// test that it does not take more elements than it needs
let mut functions: [Box<dyn Fn() -> Result<(), isize>>; 3] =
[box || Ok(()), box || Err(1), box || panic!()];
[Box::new(|| Ok(())), Box::new(|| Err(1)), Box::new(|| panic!())];
let v: Result<Vec<()>, isize> = functions.iter_mut().map(|f| (*f)()).collect();
assert!(v == Err(1));

View File

@ -17,10 +17,10 @@ fn test_debug_error() {
let msg = error_string(code);
let kind = decode_error_kind(code);
let err = Error {
repr: Repr::new_custom(box Custom {
repr: Repr::new_custom(Box::new(Custom {
kind: ErrorKind::InvalidInput,
error: box Error { repr: super::Repr::new_os(code) },
}),
error: Box::new(Error { repr: super::Repr::new_os(code) }),
})),
};
let expected = format!(
"Custom {{ \

View File

@ -6,8 +6,8 @@ use crate::thread;
#[test]
fn test_full() {
let q: Queue<Box<_>> = Queue::new();
q.push(box 1);
q.push(box 2);
q.push(Box::new(1));
q.push(Box::new(2));
}
#[test]

View File

@ -47,8 +47,8 @@ fn peek() {
fn drop_full() {
unsafe {
let q: Queue<Box<_>> = Queue::with_additions(0, (), ());
q.push(box 1);
q.push(box 2);
q.push(Box::new(1));
q.push(Box::new(2));
}
}

View File

@ -20,7 +20,7 @@ fn smoke() {
#[test]
fn drop_full() {
let (tx, _rx) = sync_channel::<Box<isize>>(1);
tx.send(box 1).unwrap();
tx.send(Box::new(1)).unwrap();
}
#[test]
@ -238,7 +238,7 @@ fn oneshot_single_thread_send_port_close() {
// Testing that the sender cleans up the payload if receiver is closed
let (tx, rx) = sync_channel::<Box<i32>>(0);
drop(rx);
assert!(tx.send(box 0).is_err());
assert!(tx.send(Box::new(0)).is_err());
}
#[test]
@ -257,7 +257,7 @@ fn oneshot_single_thread_recv_chan_close() {
#[test]
fn oneshot_single_thread_send_then_recv() {
let (tx, rx) = sync_channel::<Box<i32>>(1);
tx.send(box 10).unwrap();
tx.send(Box::new(10)).unwrap();
assert!(*rx.recv().unwrap() == 10);
}
@ -333,7 +333,7 @@ fn oneshot_multi_task_recv_then_send() {
assert!(*rx.recv().unwrap() == 10);
});
tx.send(box 10).unwrap();
tx.send(Box::new(10)).unwrap();
}
#[test]
@ -398,7 +398,7 @@ fn oneshot_multi_thread_send_recv_stress() {
for _ in 0..stress_factor() {
let (tx, rx) = sync_channel::<Box<i32>>(0);
let _t = thread::spawn(move || {
tx.send(box 10).unwrap();
tx.send(Box::new(10)).unwrap();
});
assert!(*rx.recv().unwrap() == 10);
}
@ -418,7 +418,7 @@ fn stream_send_recv_stress() {
}
thread::spawn(move || {
tx.send(box i).unwrap();
tx.send(Box::new(i)).unwrap();
send(tx, i + 1);
});
}

View File

@ -20,7 +20,7 @@ fn smoke() {
#[test]
fn drop_full() {
let (tx, _rx) = channel::<Box<isize>>();
tx.send(box 1).unwrap();
tx.send(Box::new(1)).unwrap();
}
#[test]
@ -28,7 +28,7 @@ fn drop_full_shared() {
let (tx, _rx) = channel::<Box<isize>>();
drop(tx.clone());
drop(tx.clone());
tx.send(box 1).unwrap();
tx.send(Box::new(1)).unwrap();
}
#[test]
@ -229,7 +229,7 @@ fn oneshot_single_thread_send_port_close() {
// Testing that the sender cleans up the payload if receiver is closed
let (tx, rx) = channel::<Box<i32>>();
drop(rx);
assert!(tx.send(box 0).is_err());
assert!(tx.send(Box::new(0)).is_err());
}
#[test]
@ -248,7 +248,7 @@ fn oneshot_single_thread_recv_chan_close() {
#[test]
fn oneshot_single_thread_send_then_recv() {
let (tx, rx) = channel::<Box<i32>>();
tx.send(box 10).unwrap();
tx.send(Box::new(10)).unwrap();
assert!(*rx.recv().unwrap() == 10);
}
@ -309,7 +309,7 @@ fn oneshot_multi_task_recv_then_send() {
assert!(*rx.recv().unwrap() == 10);
});
tx.send(box 10).unwrap();
tx.send(Box::new(10)).unwrap();
}
#[test]
@ -374,7 +374,7 @@ fn oneshot_multi_thread_send_recv_stress() {
for _ in 0..stress_factor() {
let (tx, rx) = channel::<Box<isize>>();
let _t = thread::spawn(move || {
tx.send(box 10).unwrap();
tx.send(Box::new(10)).unwrap();
});
assert!(*rx.recv().unwrap() == 10);
}
@ -394,7 +394,7 @@ fn stream_send_recv_stress() {
}
thread::spawn(move || {
tx.send(box i).unwrap();
tx.send(Box::new(i)).unwrap();
send(tx, i + 1);
});
}

View File

@ -127,7 +127,7 @@ where
{
let (tx, rx) = channel();
let x: Box<_> = box 1;
let x: Box<_> = Box::new(1);
let x_in_parent = (&*x) as *const i32 as usize;
spawnfn(Box::new(move || {
@ -219,7 +219,7 @@ fn test_try_panic_any_message_owned_str() {
#[test]
fn test_try_panic_any_message_any() {
match thread::spawn(move || {
panic_any(box 413u16 as Box<dyn Any + Send>);
panic_any(Box::new(413u16) as Box<dyn Any + Send>);
})
.join()
{