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

+1-1
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

+3-1
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

+2-1
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

+2-1
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

+6
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

+2-1
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

+4-4
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

+1-1
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

+1-1
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

+14-12
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

+2-2
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

+1-1
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

+1-1
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/hash/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ fn test_writer_hasher() {
6464
//assert_eq!(hasher.hash(& s), 97 + 0xFF);
6565
let cs: &[u8] = &[1u8, 2u8, 3u8];
6666
assert_eq!(hash(& cs), 9);
67-
let cs: Box<[u8]> = box [1u8, 2u8, 3u8];
67+
// FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
68+
let cs: Box<[u8]> = Box::new([1u8, 2u8, 3u8]);
6869
assert_eq!(hash(& cs), 9);
6970

7071
// FIXME (#18248) Add tests for hashing Rc<str> and Rc<[T]>

src/libcoretest/iter.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,8 @@ fn test_collect() {
404404

405405
#[test]
406406
fn test_all() {
407-
let v: Box<[int]> = box [1, 2, 3, 4, 5];
407+
// FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
408+
let v: Box<[int]> = Box::new([1, 2, 3, 4, 5]);
408409
assert!(v.iter().all(|&x| x < 10));
409410
assert!(!v.iter().all(|&x| x % 2 == 0));
410411
assert!(!v.iter().all(|&x| x > 100));
@@ -413,7 +414,8 @@ fn test_all() {
413414

414415
#[test]
415416
fn test_any() {
416-
let v: Box<[int]> = box [1, 2, 3, 4, 5];
417+
// FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
418+
let v: Box<[int]> = Box::new([1, 2, 3, 4, 5]);
417419
assert!(v.iter().any(|&x| x < 10));
418420
assert!(v.iter().any(|&x| x % 2 == 0));
419421
assert!(!v.iter().any(|&x| x > 100));
@@ -581,8 +583,9 @@ fn test_rposition() {
581583
#[test]
582584
#[should_fail]
583585
fn test_rposition_panic() {
584-
let v = [(box 0, box 0), (box 0, box 0),
585-
(box 0, box 0), (box 0, box 0)];
586+
let v: [(Box<_>, Box<_>); 4] =
587+
[(box 0, box 0), (box 0, box 0),
588+
(box 0, box 0), (box 0, box 0)];
586589
let mut i = 0;
587590
v.iter().rposition(|_elt| {
588591
if i == 2 {

src/libcoretest/option.rs

+1-1
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/middle/const_eval.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn lookup_variant_by_id<'a>(tcx: &'a ty::ctxt,
7979
None => {}
8080
}
8181
let expr_id = match csearch::maybe_get_item_ast(tcx, enum_def,
82-
box |a, b, c, d| astencode::decode_inlined_item(a, b, c, d)) {
82+
Box::new(|a, b, c, d| astencode::decode_inlined_item(a, b, c, d))) {
8383
csearch::FoundAst::Found(&ast::IIItem(ref item)) => match item.node {
8484
ast::ItemEnum(ast::EnumDef { ref variants }, _) => {
8585
// NOTE this doesn't do the right thing, it compares inlined
@@ -119,7 +119,7 @@ pub fn lookup_const_by_id<'a>(tcx: &'a ty::ctxt, def_id: ast::DefId)
119119
None => {}
120120
}
121121
let expr_id = match csearch::maybe_get_item_ast(tcx, def_id,
122-
box |a, b, c, d| astencode::decode_inlined_item(a, b, c, d)) {
122+
Box::new(|a, b, c, d| astencode::decode_inlined_item(a, b, c, d))) {
123123
csearch::FoundAst::Found(&ast::IIItem(ref item)) => match item.node {
124124
ast::ItemConst(_, ref const_expr) => Some(const_expr.id),
125125
_ => None

src/librustc/plugin/registry.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl<'a> Registry<'a> {
9999
/// It builds for you a `NormalTT` that calls `expander`,
100100
/// and also takes care of interning the macro's name.
101101
pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
102-
self.register_syntax_extension(token::intern(name), NormalTT(box expander, None));
102+
self.register_syntax_extension(token::intern(name), NormalTT(Box::new(expander), None));
103103
}
104104

105105
/// Register a compiler lint pass.

src/librustc_back/sha2.rs

+1-1
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/librustc_trans/trans/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2969,7 +2969,7 @@ pub fn write_metadata(cx: &SharedCrateContext, krate: &ast::Crate) -> Vec<u8> {
29692969
}
29702970

29712971
let encode_inlined_item: encoder::EncodeInlinedItem =
2972-
box |ecx, rbml_w, ii| astencode::encode_inlined_item(ecx, rbml_w, ii);
2972+
Box::new(|ecx, rbml_w, ii| astencode::encode_inlined_item(ecx, rbml_w, ii));
29732973

29742974
let encode_parms = crate_ctxt_to_encode_parms(cx, encode_inlined_item);
29752975
let metadata = encoder::encode_metadata(encode_parms, krate);

src/librustc_trans/trans/inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
4040
let csearch_result =
4141
csearch::maybe_get_item_ast(
4242
ccx.tcx(), fn_id,
43-
box |a,b,c,d| astencode::decode_inlined_item(a, b, c, d));
43+
Box::new(|a,b,c,d| astencode::decode_inlined_item(a, b, c, d)));
4444

4545
let inline_def = match csearch_result {
4646
csearch::FoundAst::NotFound => {

src/librustc_typeck/check/callee.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,12 @@ fn try_overloaded_call_step<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
152152
&closure_ty.sig).0;
153153
fcx.record_deferred_call_resolution(
154154
def_id,
155-
box CallResolution {call_expr: call_expr,
156-
callee_expr: callee_expr,
157-
adjusted_ty: adjusted_ty,
158-
autoderefref: autoderefref,
159-
fn_sig: fn_sig.clone(),
160-
closure_def_id: def_id});
155+
Box::new(CallResolution {call_expr: call_expr,
156+
callee_expr: callee_expr,
157+
adjusted_ty: adjusted_ty,
158+
autoderefref: autoderefref,
159+
fn_sig: fn_sig.clone(),
160+
closure_def_id: def_id}));
161161
return Some(CallStep::DeferredClosure(fn_sig));
162162
}
163163
}

0 commit comments

Comments
 (0)