Skip to content

Commit fed1249

Browse files
committed
Auto merge of #23002 - pnkfelix:fsk-box-place-runway, r=nikomatsakis
Runway for RFC 809 (overloaded box/placement-in) by adding type annotations or explicit calls to `Box::new` where I found it necessary on PR #22086. I have broken this up into more than one PR because the entire commit chain (see PR #22086) is long, widespread and unwieldy to rebase frequently. To my knowledge this is not a breaking change. Also, there is in principle nothing stopping someone from reverting some/all of these annotations, since without the rest of the commit chain in #22086, the associated code would continue to compile. All I can do is ask: Try to discourage others from removing seemingly "unnecessary" uses of the `Box` type or the `Box::new()` function, until the rest of RFC 809 lands.
2 parents 129173f + cb1b0dd commit fed1249

File tree

283 files changed

+632
-638
lines changed

Some content is hidden

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

283 files changed

+632
-638
lines changed

src/doc/trpl/pointers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ fn main() {
709709
one_hundred: 100,
710710
});
711711

712-
let y = box foo(x);
712+
let y: Box<BigStruct> = box foo(x);
713713
}
714714
```
715715

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/boxed.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ impl<T> Box<T> {
9494
/// let x = Box::new(5);
9595
/// ```
9696
#[stable(feature = "rust1", since = "1.0.0")]
97+
#[inline(always)]
9798
pub fn new(x: T) -> Box<T> {
9899
box x
99100
}
@@ -156,7 +157,7 @@ impl<T: Default> Default for Box<T> {
156157
#[stable(feature = "rust1", since = "1.0.0")]
157158
impl<T> Default for Box<[T]> {
158159
#[stable(feature = "rust1", since = "1.0.0")]
159-
fn default() -> Box<[T]> { box [] }
160+
fn default() -> Box<[T]> { Box::<[T; 0]>::new([]) }
160161
}
161162

162163
#[stable(feature = "rust1", since = "1.0.0")]

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
}

0 commit comments

Comments
 (0)