Skip to content

Commit 270f0ee

Browse files
committed
Add : Box<_> or ::Box<_> type annotations to various places.
This is the kind of change that one is expected to need to make to accommodate overloaded-`box`. ---- Note that this is not *all* of the changes necessary to accommodate Issue 22181. It is merely the subset of those cases where there was already a let-binding in place that made it easy to add the necesasry type ascription. (For unnamed intermediate `Box` values, one must go down a different route; `Box::new` is the option that maximizes portability, but has potential inefficiency depending on whether the call is inlined.) ---- There is one place worth note, `run-pass/coerce-match.rs`, where I used an ugly form of `Box<_>` type ascription where I would have preferred to use `Box::new` to accommodate overloaded-`box`. I deliberately did not use `Box::new` here, because that is already done in coerce-match-calls.rs. ---- Precursor for overloaded-`box` and placement-`in`; see Issue 22181.
1 parent 14f0942 commit 270f0ee

File tree

164 files changed

+281
-264
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

164 files changed

+281
-264
lines changed

src/liballoc/arc.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
//! }
7070
//! ```
7171
72+
use boxed::Box;
73+
7274
use core::prelude::*;
7375

7476
use core::atomic;
@@ -170,7 +172,7 @@ impl<T> Arc<T> {
170172
pub fn new(data: T) -> Arc<T> {
171173
// Start the weak pointer count as 1 which is the weak pointer that's
172174
// held by all the strong pointers (kinda), see std/rc.rs for more info
173-
let x = box ArcInner {
175+
let x: Box<_> = box ArcInner {
174176
strong: atomic::AtomicUsize::new(1),
175177
weak: atomic::AtomicUsize::new(1),
176178
data: data,

src/liballoc/heap.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ mod test {
387387
extern crate test;
388388
use self::test::Bencher;
389389
use core::ptr::PtrExt;
390+
use boxed::Box;
390391
use heap;
391392

392393
#[test]
@@ -404,7 +405,7 @@ mod test {
404405
#[bench]
405406
fn alloc_owned_small(b: &mut Bencher) {
406407
b.iter(|| {
407-
box 10
408+
let _: Box<_> = box 10;
408409
})
409410
}
410411
}

src/liballoc/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,15 @@ pub mod heap;
9696

9797
// Primitive types using the heaps above
9898

99+
// Need to conditionally define the mod from `boxed.rs` to avoid
100+
// duplicating the lang-items when building in test cfg; but also need
101+
// to allow code to have `use boxed::HEAP;`
102+
// and `use boxed::Box;` declarations.
99103
#[cfg(not(test))]
100104
pub mod boxed;
101105
#[cfg(test)]
106+
mod boxed { pub use std::boxed::{Box, HEAP}; }
107+
#[cfg(test)]
102108
mod boxed_test;
103109
pub mod arc;
104110
pub mod rc;

src/liballoc/rc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@ impl<T> RcBoxPtr<T> for Weak<T> {
795795
#[cfg(test)]
796796
mod tests {
797797
use super::{Rc, Weak, weak_count, strong_count};
798+
use std::boxed::Box;
798799
use std::cell::RefCell;
799800
use std::option::Option;
800801
use std::option::Option::{Some, None};
@@ -826,7 +827,7 @@ mod tests {
826827

827828
#[test]
828829
fn test_destructor() {
829-
let x = Rc::new(box 5);
830+
let x: Rc<Box<_>> = Rc::new(box 5);
830831
assert_eq!(**x, 5);
831832
}
832833

src/libarena/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,11 @@ mod tests {
581581
#[bench]
582582
pub fn bench_copy_nonarena(b: &mut Bencher) {
583583
b.iter(|| {
584-
box Point {
584+
let _: Box<_> = box Point {
585585
x: 1,
586586
y: 2,
587587
z: 3,
588-
}
588+
};
589589
})
590590
}
591591

@@ -634,10 +634,10 @@ mod tests {
634634
#[bench]
635635
pub fn bench_noncopy_nonarena(b: &mut Bencher) {
636636
b.iter(|| {
637-
box Noncopy {
637+
let _: Box<_> = box Noncopy {
638638
string: "hello world".to_string(),
639639
array: vec!( 1, 2, 3, 4, 5 ),
640-
}
640+
};
641641
})
642642
}
643643

src/libcollections/binary_heap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ mod tests {
790790

791791
#[test]
792792
fn test_push_unique() {
793-
let mut heap = BinaryHeap::from_vec(vec![box 2, box 4, box 9]);
793+
let mut heap = BinaryHeap::<Box<_>>::from_vec(vec![box 2, box 4, box 9]);
794794
assert_eq!(heap.len(), 3);
795795
assert!(*heap.peek().unwrap() == box 9);
796796
heap.push(box 11);

src/libcollections/linked_list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ mod tests {
984984

985985
#[test]
986986
fn test_basic() {
987-
let mut m = LinkedList::new();
987+
let mut m = LinkedList::<Box<_>>::new();
988988
assert_eq!(m.pop_front(), None);
989989
assert_eq!(m.pop_back(), None);
990990
assert_eq!(m.pop_front(), None);

src/libcollections/slice.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,7 @@ fn merge_sort<T, F>(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> Order
15091509

15101510
#[cfg(test)]
15111511
mod tests {
1512+
use alloc::boxed::Box;
15121513
use core::cmp::Ordering::{Greater, Less, Equal};
15131514
use core::prelude::{Some, None, Clone};
15141515
use core::prelude::{Iterator, IteratorExt};
@@ -1799,7 +1800,7 @@ mod tests {
17991800
#[test]
18001801
fn test_swap_remove_noncopyable() {
18011802
// Tests that we don't accidentally run destructors twice.
1802-
let mut v = Vec::new();
1803+
let mut v: Vec<Box<_>> = Vec::new();
18031804
v.push(box 0u8);
18041805
v.push(box 0u8);
18051806
v.push(box 0u8);
@@ -1828,7 +1829,7 @@ mod tests {
18281829

18291830
#[test]
18301831
fn test_truncate() {
1831-
let mut v = vec![box 6,box 5,box 4];
1832+
let mut v: Vec<Box<_>> = vec![box 6,box 5,box 4];
18321833
v.truncate(1);
18331834
let v = v;
18341835
assert_eq!(v.len(), 1);
@@ -1838,7 +1839,7 @@ mod tests {
18381839

18391840
#[test]
18401841
fn test_clear() {
1841-
let mut v = vec![box 6,box 5,box 4];
1842+
let mut v: Vec<Box<_>> = vec![box 6,box 5,box 4];
18421843
v.clear();
18431844
assert_eq!(v.len(), 0);
18441845
// If the unsafe block didn't drop things properly, we blow up here.
@@ -1863,11 +1864,11 @@ mod tests {
18631864

18641865
#[test]
18651866
fn test_dedup_unique() {
1866-
let mut v0 = vec![box 1, box 1, box 2, box 3];
1867+
let mut v0: Vec<Box<_>> = vec![box 1, box 1, box 2, box 3];
18671868
v0.dedup();
1868-
let mut v1 = vec![box 1, box 2, box 2, box 3];
1869+
let mut v1: Vec<Box<_>> = vec![box 1, box 2, box 2, box 3];
18691870
v1.dedup();
1870-
let mut v2 = vec![box 1, box 2, box 3, box 3];
1871+
let mut v2: Vec<Box<_>> = vec![box 1, box 2, box 3, box 3];
18711872
v2.dedup();
18721873
/*
18731874
* If the boxed pointers were leaked or otherwise misused, valgrind
@@ -1877,11 +1878,11 @@ mod tests {
18771878

18781879
#[test]
18791880
fn test_dedup_shared() {
1880-
let mut v0 = vec![box 1, box 1, box 2, box 3];
1881+
let mut v0: Vec<Box<_>> = vec![box 1, box 1, box 2, box 3];
18811882
v0.dedup();
1882-
let mut v1 = vec![box 1, box 2, box 2, box 3];
1883+
let mut v1: Vec<Box<_>> = vec![box 1, box 2, box 2, box 3];
18831884
v1.dedup();
1884-
let mut v2 = vec![box 1, box 2, box 3, box 3];
1885+
let mut v2: Vec<Box<_>> = vec![box 1, box 2, box 3, box 3];
18851886
v2.dedup();
18861887
/*
18871888
* If the pointers were leaked or otherwise misused, valgrind and/or
@@ -2254,8 +2255,9 @@ mod tests {
22542255
#[test]
22552256
#[should_fail]
22562257
fn test_permute_fail() {
2257-
let v = [(box 0, Rc::new(0)), (box 0, Rc::new(0)),
2258-
(box 0, Rc::new(0)), (box 0, Rc::new(0))];
2258+
let v: [(Box<_>, Rc<_>); 4] =
2259+
[(box 0, Rc::new(0)), (box 0, Rc::new(0)),
2260+
(box 0, Rc::new(0)), (box 0, Rc::new(0))];
22592261
let mut i = 0;
22602262
for _ in v.permutations() {
22612263
if i == 2 {
@@ -2849,7 +2851,7 @@ mod tests {
28492851

28502852
#[test]
28512853
fn test_to_vec() {
2852-
let xs = box [1, 2, 3];
2854+
let xs: Box<_> = box [1, 2, 3];
28532855
let ys = xs.to_vec();
28542856
assert_eq!(ys, [1, 2, 3]);
28552857
}

src/libcollections/vec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,8 +2130,8 @@ mod tests {
21302130
#[test]
21312131
fn test_clone_from() {
21322132
let mut v = vec!();
2133-
let three = vec!(box 1, box 2, box 3);
2134-
let two = vec!(box 4, box 5);
2133+
let three: Vec<Box<_>> = vec!(box 1, box 2, box 3);
2134+
let two: Vec<Box<_>> = vec!(box 4, box 5);
21352135
// zero, long
21362136
v.clone_from(&three);
21372137
assert_eq!(v, three);

src/libcollections/vec_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ mod test_map {
12051205

12061206
#[test]
12071207
fn test_move_iter() {
1208-
let mut m = VecMap::new();
1208+
let mut m: VecMap<Box<_>> = VecMap::new();
12091209
m.insert(1, box 2);
12101210
let mut called = false;
12111211
for (k, v) in m {

src/libcoretest/any.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn any_downcast_ref() {
6868
#[test]
6969
fn any_downcast_mut() {
7070
let mut a = 5_usize;
71-
let mut b = box 7_usize;
71+
let mut b: Box<_> = box 7_usize;
7272

7373
let a_r = &mut a as &mut Any;
7474
let tmp: &mut uint = &mut *b;

src/libcoretest/option.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use core::clone::Clone;
1616
#[test]
1717
fn test_get_ptr() {
1818
unsafe {
19-
let x = box 0;
19+
let x: Box<_> = box 0;
2020
let addr_x: *const int = mem::transmute(&*x);
2121
let opt = Some(x);
2222
let y = opt.unwrap();

src/librustc_back/sha2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ mod tests {
606606

607607
let tests = wikipedia_tests;
608608

609-
let mut sh = box Sha256::new();
609+
let mut sh: Box<_> = box Sha256::new();
610610

611611
test_hash(&mut *sh, &tests);
612612
}

src/libstd/old_io/net/ip.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -323,22 +323,22 @@ impl<'a> Parser<'a> {
323323
}
324324

325325
fn read_ip_addr(&mut self) -> Option<IpAddr> {
326-
let ipv4_addr = |p: &mut Parser| p.read_ipv4_addr();
327-
let ipv6_addr = |p: &mut Parser| p.read_ipv6_addr();
328-
self.read_or(&mut [box ipv4_addr, box ipv6_addr])
326+
let ipv4_addr: Box<_> = box |p: &mut Parser| p.read_ipv4_addr();
327+
let ipv6_addr: Box<_> = box |p: &mut Parser| p.read_ipv6_addr();
328+
self.read_or(&mut [ipv4_addr, ipv6_addr])
329329
}
330330

331331
fn read_socket_addr(&mut self) -> Option<SocketAddr> {
332332
let ip_addr = |p: &mut Parser| {
333-
let ipv4_p = |p: &mut Parser| p.read_ip_addr();
334-
let ipv6_p = |p: &mut Parser| {
333+
let ipv4_p: Box<_> = box |p: &mut Parser| p.read_ip_addr();
334+
let ipv6_p: Box<_> = box |p: &mut Parser| {
335335
let open_br = |p: &mut Parser| p.read_given_char('[');
336336
let ip_addr = |p: &mut Parser| p.read_ipv6_addr();
337337
let clos_br = |p: &mut Parser| p.read_given_char(']');
338338
p.read_seq_3::<char, IpAddr, char, _, _, _>(open_br, ip_addr, clos_br)
339339
.map(|t| match t { (_, ip, _) => ip })
340340
};
341-
p.read_or(&mut [box ipv4_p, box ipv6_p])
341+
p.read_or(&mut [ipv4_p, ipv6_p])
342342
};
343343
let colon = |p: &mut Parser| p.read_given_char(':');
344344
let port = |p: &mut Parser| p.read_number(10, 5, 0x10000).map(|n| n as u16);

src/libstd/sync/mpsc/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,13 +1044,13 @@ mod test {
10441044

10451045
#[test]
10461046
fn drop_full() {
1047-
let (tx, _rx) = channel();
1047+
let (tx, _rx) = channel::<Box<int>>();
10481048
tx.send(box 1).unwrap();
10491049
}
10501050

10511051
#[test]
10521052
fn drop_full_shared() {
1053-
let (tx, _rx) = channel();
1053+
let (tx, _rx) = channel::<Box<int>>();
10541054
drop(tx.clone());
10551055
drop(tx.clone());
10561056
tx.send(box 1).unwrap();
@@ -1389,7 +1389,7 @@ mod test {
13891389
#[test]
13901390
fn oneshot_multi_thread_send_recv_stress() {
13911391
for _ in 0..stress_factor() {
1392-
let (tx, rx) = channel();
1392+
let (tx, rx) = channel::<Box<int>>();
13931393
let _t = thread::spawn(move|| {
13941394
tx.send(box 10).unwrap();
13951395
});
@@ -1566,7 +1566,7 @@ mod sync_tests {
15661566

15671567
#[test]
15681568
fn drop_full() {
1569-
let (tx, _rx) = sync_channel(1);
1569+
let (tx, _rx) = sync_channel::<Box<int>>(1);
15701570
tx.send(box 1).unwrap();
15711571
}
15721572

src/libstd/sync/mpsc/mpsc_queue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ mod tests {
164164

165165
#[test]
166166
fn test_full() {
167-
let q = Queue::new();
167+
let q: Queue<Box<_>> = Queue::new();
168168
q.push(box 1);
169169
q.push(box 2);
170170
}

src/libstd/sync/mpsc/spsc_queue.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ mod test {
289289
#[test]
290290
fn drop_full() {
291291
unsafe {
292-
let q = Queue::new(0);
292+
let q: Queue<Box<_>> = Queue::new(0);
293293
q.push(box 1);
294294
q.push(box 2);
295295
}

src/libstd/thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,7 @@ mod test {
804804
fn avoid_copying_the_body<F>(spawnfn: F) where F: FnOnce(Thunk<'static>) {
805805
let (tx, rx) = channel();
806806

807-
let x = box 1;
807+
let x: Box<_> = box 1;
808808
let x_in_parent = (&*x) as *const i32 as usize;
809809

810810
spawnfn(Thunk::new(move|| {

src/test/bench/shootout-k-nucleotide.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ impl Table {
146146
fn search_remainder<C:TableCallback>(item: &mut Entry, key: Code, c: C) {
147147
match item.next {
148148
None => {
149-
let mut entry = box Entry {
149+
let mut entry: Box<_> = box Entry {
150150
code: key,
151151
count: 0,
152152
next: None,
@@ -170,7 +170,7 @@ impl Table {
170170

171171
{
172172
if self.items[index as usize].is_none() {
173-
let mut entry = box Entry {
173+
let mut entry: Box<_> = box Entry {
174174
code: key,
175175
count: 0,
176176
next: None,

src/test/bench/sudoku.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl Sudoku {
124124
fn next_color(&mut self, row: u8, col: u8, start_color: u8) -> bool {
125125
if start_color < 10u8 {
126126
// colors not yet used
127-
let mut avail = box Colors::new(start_color);
127+
let mut avail: Box<_> = box Colors::new(start_color);
128128

129129
// drop colors already in use in neighbourhood
130130
self.drop_colors(&mut *avail, row, col);

src/test/compile-fail/borrow-tuple-fields.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct Foo(Box<isize>, isize);
1616
struct Bar(isize, isize);
1717

1818
fn main() {
19-
let x = (box 1, 2);
19+
let x: (Box<_>, _) = (box 1, 2);
2020
let r = &x.0;
2121
let y = x; //~ ERROR cannot move out of `x` because it is borrowed
2222

src/test/compile-fail/borrowck-bad-nested-calls-free.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn add(v: &usize, w: usize) -> usize {
2323
}
2424

2525
fn implicit() {
26-
let mut a = box 1;
26+
let mut a: Box<_> = box 1;
2727

2828
// Note the danger here:
2929
//
@@ -36,7 +36,7 @@ fn implicit() {
3636
}
3737

3838
fn explicit() {
39-
let mut a = box 1;
39+
let mut a: Box<_> = box 1;
4040
add(
4141
&*a,
4242
rewrite(&mut a)); //~ ERROR cannot borrow

0 commit comments

Comments
 (0)