Skip to content

Commit

Permalink
Switched to Box::new in many places.
Browse files Browse the repository at this point in the history
Many of the modifications putting in `Box::new` calls also include a
pointer to Issue 22405, which tracks going back to `box <expr>` if
possible in the future.

(Still tried to use `Box<_>` where it sufficed; thus some tests still
have `box_syntax` enabled, as they use a mix of `box` and `Box::new`.)

Precursor for overloaded-`box` and placement-`in`; see Issue 22181.
  • Loading branch information
pnkfelix committed Mar 3, 2015
1 parent b03279a commit 0d5bcb1
Show file tree
Hide file tree
Showing 118 changed files with 349 additions and 373 deletions.
2 changes: 1 addition & 1 deletion src/liballoc/boxed.rs
Expand Up @@ -157,7 +157,7 @@ impl<T: Default> Default for Box<T> {
#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Default for Box<[T]> {
#[stable(feature = "rust1", since = "1.0.0")]
fn default() -> Box<[T]> { box [] }
fn default() -> Box<[T]> { Box::<[T; 0]>::new([]) }
}

#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
3 changes: 2 additions & 1 deletion src/libcoretest/hash/mod.rs
Expand Up @@ -64,7 +64,8 @@ fn test_writer_hasher() {
//assert_eq!(hasher.hash(& s), 97 + 0xFF);
let cs: &[u8] = &[1u8, 2u8, 3u8];
assert_eq!(hash(& cs), 9);
let cs: Box<[u8]> = box [1u8, 2u8, 3u8];
// FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
let cs: Box<[u8]> = Box::new([1u8, 2u8, 3u8]);
assert_eq!(hash(& cs), 9);

// FIXME (#18248) Add tests for hashing Rc<str> and Rc<[T]>
Expand Down
11 changes: 7 additions & 4 deletions src/libcoretest/iter.rs
Expand Up @@ -404,7 +404,8 @@ fn test_collect() {

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

#[test]
fn test_any() {
let v: Box<[int]> = box [1, 2, 3, 4, 5];
// FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
let v: Box<[int]> = Box::new([1, 2, 3, 4, 5]);
assert!(v.iter().any(|&x| x < 10));
assert!(v.iter().any(|&x| x % 2 == 0));
assert!(!v.iter().any(|&x| x > 100));
Expand Down Expand Up @@ -581,8 +583,9 @@ fn test_rposition() {
#[test]
#[should_fail]
fn test_rposition_panic() {
let v = [(box 0, box 0), (box 0, box 0),
(box 0, box 0), (box 0, box 0)];
let v: [(Box<_>, Box<_>); 4] =
[(box 0, box 0), (box 0, box 0),
(box 0, box 0), (box 0, box 0)];
let mut i = 0;
v.iter().rposition(|_elt| {
if i == 2 {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/const_eval.rs
Expand Up @@ -79,7 +79,7 @@ fn lookup_variant_by_id<'a>(tcx: &'a ty::ctxt,
None => {}
}
let expr_id = match csearch::maybe_get_item_ast(tcx, enum_def,
box |a, b, c, d| astencode::decode_inlined_item(a, b, c, d)) {
Box::new(|a, b, c, d| astencode::decode_inlined_item(a, b, c, d))) {
csearch::FoundAst::Found(&ast::IIItem(ref item)) => match item.node {
ast::ItemEnum(ast::EnumDef { ref variants }, _) => {
// NOTE this doesn't do the right thing, it compares inlined
Expand Down Expand Up @@ -119,7 +119,7 @@ pub fn lookup_const_by_id<'a>(tcx: &'a ty::ctxt, def_id: ast::DefId)
None => {}
}
let expr_id = match csearch::maybe_get_item_ast(tcx, def_id,
box |a, b, c, d| astencode::decode_inlined_item(a, b, c, d)) {
Box::new(|a, b, c, d| astencode::decode_inlined_item(a, b, c, d))) {
csearch::FoundAst::Found(&ast::IIItem(ref item)) => match item.node {
ast::ItemConst(_, ref const_expr) => Some(const_expr.id),
_ => None
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/plugin/registry.rs
Expand Up @@ -99,7 +99,7 @@ impl<'a> Registry<'a> {
/// It builds for you a `NormalTT` that calls `expander`,
/// and also takes care of interning the macro's name.
pub fn register_macro(&mut self, name: &str, expander: MacroExpanderFn) {
self.register_syntax_extension(token::intern(name), NormalTT(box expander, None));
self.register_syntax_extension(token::intern(name), NormalTT(Box::new(expander), None));
}

/// Register a compiler lint pass.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/trans/base.rs
Expand Up @@ -2969,7 +2969,7 @@ pub fn write_metadata(cx: &SharedCrateContext, krate: &ast::Crate) -> Vec<u8> {
}

let encode_inlined_item: encoder::EncodeInlinedItem =
box |ecx, rbml_w, ii| astencode::encode_inlined_item(ecx, rbml_w, ii);
Box::new(|ecx, rbml_w, ii| astencode::encode_inlined_item(ecx, rbml_w, ii));

let encode_parms = crate_ctxt_to_encode_parms(cx, encode_inlined_item);
let metadata = encoder::encode_metadata(encode_parms, krate);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/trans/inline.rs
Expand Up @@ -40,7 +40,7 @@ fn instantiate_inline(ccx: &CrateContext, fn_id: ast::DefId)
let csearch_result =
csearch::maybe_get_item_ast(
ccx.tcx(), fn_id,
box |a,b,c,d| astencode::decode_inlined_item(a, b, c, d));
Box::new(|a,b,c,d| astencode::decode_inlined_item(a, b, c, d)));

let inline_def = match csearch_result {
csearch::FoundAst::NotFound => {
Expand Down
12 changes: 6 additions & 6 deletions src/librustc_typeck/check/callee.rs
Expand Up @@ -152,12 +152,12 @@ fn try_overloaded_call_step<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
&closure_ty.sig).0;
fcx.record_deferred_call_resolution(
def_id,
box CallResolution {call_expr: call_expr,
callee_expr: callee_expr,
adjusted_ty: adjusted_ty,
autoderefref: autoderefref,
fn_sig: fn_sig.clone(),
closure_def_id: def_id});
Box::new(CallResolution {call_expr: call_expr,
callee_expr: callee_expr,
adjusted_ty: adjusted_ty,
autoderefref: autoderefref,
fn_sig: fn_sig.clone(),
closure_def_id: def_id}));
return Some(CallStep::DeferredClosure(fn_sig));
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/libstd/old_io/stdio.rs
Expand Up @@ -547,8 +547,9 @@ mod tests {

let (tx, rx) = channel();
let (mut r, w) = (ChanReader::new(rx), ChanWriter::new(tx));
// FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
let _t = thread::spawn(move|| {
set_stdout(box w);
set_stdout(Box::new(w));
println!("hello!");
});
assert_eq!(r.read_to_string().unwrap(), "hello!\n");
Expand All @@ -560,8 +561,9 @@ mod tests {

let (tx, rx) = channel();
let (mut r, w) = (ChanReader::new(rx), ChanWriter::new(tx));
// FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
let _t = thread::spawn(move || -> () {
set_stderr(box w);
set_stderr(Box::new(w));
panic!("my special message");
});
let s = r.read_to_string().unwrap();
Expand Down
5 changes: 3 additions & 2 deletions src/libstd/old_io/timer.rs
Expand Up @@ -15,6 +15,7 @@

// FIXME: These functions take Durations but only pass ms to the backend impls.

use boxed::Box;
use sync::mpsc::{Receiver, Sender, channel};
use time::Duration;
use old_io::IoResult;
Expand Down Expand Up @@ -143,7 +144,7 @@ impl Timer {
let (tx, rx) = channel();
// Short-circuit the timer backend for 0 duration
if in_ms_u64(duration) != 0 {
self.inner.oneshot(in_ms_u64(duration), box TimerCallback { tx: tx });
self.inner.oneshot(in_ms_u64(duration), Box::new(TimerCallback { tx: tx }));
} else {
tx.send(()).unwrap();
}
Expand Down Expand Up @@ -204,7 +205,7 @@ impl Timer {
// not clear what use a 0ms period is anyway...
let ms = if ms == 0 { 1 } else { ms };
let (tx, rx) = channel();
self.inner.period(ms, box TimerCallback { tx: tx });
self.inner.period(ms, Box::new(TimerCallback { tx: tx }));
return rx
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/libstd/rt/unwind.rs
Expand Up @@ -166,7 +166,7 @@ fn rust_panic(cause: Box<Any + Send + 'static>) -> ! {
rtdebug!("begin_unwind()");

unsafe {
let exception = box Exception {
let exception: Box<_> = box Exception {
uwe: uw::_Unwind_Exception {
exception_class: rust_exception_class(),
exception_cleanup: exception_cleanup,
Expand Down Expand Up @@ -506,7 +506,7 @@ pub fn begin_unwind_fmt(msg: fmt::Arguments, file_line: &(&'static str, uint)) -

let mut s = String::new();
let _ = write!(&mut s, "{}", msg);
begin_unwind_inner(box s, file_line)
begin_unwind_inner(Box::new(s), file_line)
}

/// This is the entry point of unwinding for panic!() and assert!().
Expand All @@ -521,7 +521,7 @@ pub fn begin_unwind<M: Any + Send>(msg: M, file_line: &(&'static str, uint)) ->
// panicking.

// see below for why we do the `Any` coercion here.
begin_unwind_inner(box msg, file_line)
begin_unwind_inner(Box::new(msg), file_line)
}

/// The core of the unwinding.
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/thunk.rs
Expand Up @@ -33,7 +33,7 @@ impl<'a,A,R> Thunk<'a,A,R> {
where F : FnOnce(A) -> R, F : Send + 'a
{
Thunk {
invoke: box func
invoke: Box::<F>::new(func)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/diagnostic.rs
Expand Up @@ -223,7 +223,7 @@ pub fn mk_span_handler(handler: Handler, cm: codemap::CodeMap) -> SpanHandler {
pub fn default_handler(color_config: ColorConfig,
registry: Option<diagnostics::registry::Registry>,
can_emit_warnings: bool) -> Handler {
mk_handler(can_emit_warnings, box EmitterWriter::stderr(color_config, registry))
mk_handler(can_emit_warnings, Box::new(EmitterWriter::stderr(color_config, registry)))
}

pub fn mk_handler(can_emit_warnings: bool, e: Box<Emitter + Send>) -> Handler {
Expand Down Expand Up @@ -352,11 +352,11 @@ impl EmitterWriter {
if use_color {
let dst = match term::stderr() {
Some(t) => Terminal(t),
None => Raw(box stderr),
None => Raw(Box::new(stderr)),
};
EmitterWriter { dst: dst, registry: registry }
} else {
EmitterWriter { dst: Raw(box stderr), registry: registry }
EmitterWriter { dst: Raw(Box::new(stderr)), registry: registry }
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax/ext/base.rs
Expand Up @@ -465,7 +465,7 @@ fn initial_syntax_expander_table<'feat>(ecfg: &expand::ExpansionConfig<'feat>)
-> SyntaxEnv {
// utility function to simplify creating NormalTT syntax extensions
fn builtin_normal_expander(f: MacroExpanderFn) -> SyntaxExtension {
NormalTT(box f, None)
NormalTT(Box::new(f), None)
}

let mut syntax_expanders = SyntaxEnv::new();
Expand All @@ -489,9 +489,9 @@ fn initial_syntax_expander_table<'feat>(ecfg: &expand::ExpansionConfig<'feat>)
builtin_normal_expander(
ext::log_syntax::expand_syntax_ext));
syntax_expanders.insert(intern("derive"),
Decorator(box ext::deriving::expand_meta_derive));
Decorator(Box::new(ext::deriving::expand_meta_derive)));
syntax_expanders.insert(intern("deriving"),
Decorator(box ext::deriving::expand_deprecated_deriving));
Decorator(Box::new(ext::deriving::expand_deprecated_deriving)));

if ecfg.enable_quotes() {
// Quasi-quoting expanders
Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/ext/deriving/clone.rs
Expand Up @@ -40,9 +40,9 @@ pub fn expand_deriving_clone<F>(cx: &mut ExtCtxt,
args: Vec::new(),
ret_ty: Self_,
attributes: attrs,
combine_substructure: combine_substructure(box |c, s, sub| {
combine_substructure: combine_substructure(Box::new(|c, s, sub| {
cs_clone("Clone", c, s, sub)
}),
})),
}
),
associated_types: Vec::new(),
Expand Down
8 changes: 4 additions & 4 deletions src/libsyntax/ext/deriving/cmp/eq.rs
Expand Up @@ -40,7 +40,7 @@ pub fn expand_deriving_eq<F>(cx: &mut ExtCtxt,
cx.expr_binary(span, ast::BiAnd, subexpr, eq)
},
cx.expr_bool(span, true),
box |cx, span, _, _| cx.expr_bool(span, false),
Box::new(|cx, span, _, _| cx.expr_bool(span, false)),
cx, span, substr)
}
fn cs_ne(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
Expand All @@ -57,7 +57,7 @@ pub fn expand_deriving_eq<F>(cx: &mut ExtCtxt,
cx.expr_binary(span, ast::BiOr, subexpr, eq)
},
cx.expr_bool(span, false),
box |cx, span, _, _| cx.expr_bool(span, true),
Box::new(|cx, span, _, _| cx.expr_bool(span, true)),
cx, span, substr)
}

Expand All @@ -72,9 +72,9 @@ pub fn expand_deriving_eq<F>(cx: &mut ExtCtxt,
args: vec!(borrowed_self()),
ret_ty: Literal(path_local!(bool)),
attributes: attrs,
combine_substructure: combine_substructure(box |a, b, c| {
combine_substructure: combine_substructure(Box::new(|a, b, c| {
$f(a, b, c)
})
}))
}
} }
}
Expand Down
16 changes: 8 additions & 8 deletions src/libsyntax/ext/deriving/cmp/ord.rs
Expand Up @@ -38,9 +38,9 @@ pub fn expand_deriving_ord<F>(cx: &mut ExtCtxt,
args: vec!(borrowed_self()),
ret_ty: Literal(path_local!(bool)),
attributes: attrs,
combine_substructure: combine_substructure(box |cx, span, substr| {
combine_substructure: combine_substructure(Box::new(|cx, span, substr| {
cs_op($op, $equal, cx, span, substr)
})
}))
}
} }
}
Expand All @@ -61,9 +61,9 @@ pub fn expand_deriving_ord<F>(cx: &mut ExtCtxt,
args: vec![borrowed_self()],
ret_ty: ret_ty,
attributes: attrs,
combine_substructure: combine_substructure(box |cx, span, substr| {
combine_substructure: combine_substructure(Box::new(|cx, span, substr| {
cs_partial_cmp(cx, span, substr)
})
}))
};

let trait_def = TraitDef {
Expand Down Expand Up @@ -175,13 +175,13 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span,
cx.expr_block(cx.block(span, vec!(assign), Some(if_)))
},
equals_expr.clone(),
box |cx, span, (self_args, tag_tuple), _non_self_args| {
Box::new(|cx, span, (self_args, tag_tuple), _non_self_args| {
if self_args.len() != 2 {
cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`")
} else {
some_ordering_collapsed(cx, span, PartialCmpOp, tag_tuple)
}
},
}),
cx, span, substr)
}

Expand Down Expand Up @@ -223,7 +223,7 @@ fn cs_op(less: bool, equal: bool, cx: &mut ExtCtxt,
cx.expr_binary(span, ast::BiOr, cmp, and)
},
cx.expr_bool(span, equal),
box |cx, span, (self_args, tag_tuple), _non_self_args| {
Box::new(|cx, span, (self_args, tag_tuple), _non_self_args| {
if self_args.len() != 2 {
cx.span_bug(span, "not exactly 2 arguments in `derive(PartialOrd)`")
} else {
Expand All @@ -233,6 +233,6 @@ fn cs_op(less: bool, equal: bool, cx: &mut ExtCtxt,
};
some_ordering_collapsed(cx, span, op, tag_tuple)
}
},
}),
cx, span, substr)
}
7 changes: 4 additions & 3 deletions src/libsyntax/ext/deriving/cmp/totaleq.rs
Expand Up @@ -32,7 +32,8 @@ pub fn expand_deriving_totaleq<F>(cx: &mut ExtCtxt,
let block = cx.block(span, stmts, None);
cx.expr_block(block)
},
box |cx, sp, _, _| cx.span_bug(sp, "non matching enums in derive(Eq)?"),
Box::new(|cx, sp, _, _| {
cx.span_bug(sp, "non matching enums in derive(Eq)?") }),
cx,
span,
substr)
Expand All @@ -57,9 +58,9 @@ pub fn expand_deriving_totaleq<F>(cx: &mut ExtCtxt,
args: vec!(),
ret_ty: nil_ty(),
attributes: attrs,
combine_substructure: combine_substructure(box |a, b, c| {
combine_substructure: combine_substructure(Box::new(|a, b, c| {
cs_total_eq_assert(a, b, c)
})
}))
}
),
associated_types: Vec::new(),
Expand Down
8 changes: 4 additions & 4 deletions src/libsyntax/ext/deriving/cmp/totalord.rs
Expand Up @@ -41,9 +41,9 @@ pub fn expand_deriving_totalord<F>(cx: &mut ExtCtxt,
args: vec!(borrowed_self()),
ret_ty: Literal(path_std!(cx, core::cmp::Ordering)),
attributes: attrs,
combine_substructure: combine_substructure(box |a, b, c| {
combine_substructure: combine_substructure(Box::new(|a, b, c| {
cs_cmp(a, b, c)
}),
})),
}
),
associated_types: Vec::new(),
Expand Down Expand Up @@ -131,12 +131,12 @@ pub fn cs_cmp(cx: &mut ExtCtxt, span: Span,
cx.expr_block(cx.block(span, vec!(assign), Some(if_)))
},
cx.expr_path(equals_path.clone()),
box |cx, span, (self_args, tag_tuple), _non_self_args| {
Box::new(|cx, span, (self_args, tag_tuple), _non_self_args| {
if self_args.len() != 2 {
cx.span_bug(span, "not exactly 2 arguments in `derives(Ord)`")
} else {
ordering_collapsed(cx, span, tag_tuple)
}
},
}),
cx, span, substr)
}

0 comments on commit 0d5bcb1

Please sign in to comment.