Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace libtest/lib.rs:FnBox with std::boxed::FnBox. #46636

Merged
merged 1 commit into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustdoc/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ impl Collector {
should_panic: testing::ShouldPanic::No,
allow_fail,
},
testfn: testing::DynTestFn(box move |()| {
testfn: testing::DynTestFn(box move || {
let panic = io::set_panic(None);
let print = io::set_print(None);
match {
Expand Down
50 changes: 20 additions & 30 deletions src/libtest/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#![deny(warnings)]

#![feature(asm)]
#![feature(fnbox)]
#![cfg_attr(unix, feature(libc))]
#![feature(set_stdio)]
#![feature(panic_unwind)]
Expand All @@ -56,6 +57,7 @@ use self::OutputLocation::*;

use std::panic::{catch_unwind, AssertUnwindSafe};
use std::any::Any;
use std::boxed::FnBox;
use std::cmp;
use std::collections::BTreeMap;
use std::env;
Expand Down Expand Up @@ -133,24 +135,14 @@ pub trait TDynBenchFn: Send {
fn run(&self, harness: &mut Bencher);
}

pub trait FnBox<T>: Send + 'static {
fn call_box(self: Box<Self>, t: T);
}

impl<T, F: FnOnce(T) + Send + 'static> FnBox<T> for F {
fn call_box(self: Box<F>, t: T) {
(*self)(t)
}
}

// A function that runs a test. If the function returns successfully,
// the test succeeds; if the function panics then the test fails. We
// may need to come up with a more clever definition of test in order
// to support isolation of tests into threads.
pub enum TestFn {
StaticTestFn(fn()),
StaticBenchFn(fn(&mut Bencher)),
DynTestFn(Box<FnBox<()>>),
DynTestFn(Box<FnBox() + Send>),
DynBenchFn(Box<TDynBenchFn + 'static>),
}

Expand Down Expand Up @@ -1337,14 +1329,14 @@ pub fn convert_benchmarks_to_tests(tests: Vec<TestDescAndFn>) -> Vec<TestDescAnd
tests.into_iter().map(|x| {
let testfn = match x.testfn {
DynBenchFn(bench) => {
DynTestFn(Box::new(move |()| {
DynTestFn(Box::new(move || {
bench::run_once(|b| {
__rust_begin_short_backtrace(|| bench.run(b))
})
}))
}
StaticBenchFn(benchfn) => {
DynTestFn(Box::new(move |()| {
DynTestFn(Box::new(move || {
bench::run_once(|b| {
__rust_begin_short_backtrace(|| benchfn(b))
})
Expand Down Expand Up @@ -1379,7 +1371,7 @@ pub fn run_test(opts: &TestOpts,
fn run_test_inner(desc: TestDesc,
monitor_ch: Sender<MonitorMsg>,
nocapture: bool,
testfn: Box<FnBox<()>>) {
testfn: Box<FnBox() + Send>) {
struct Sink(Arc<Mutex<Vec<u8>>>);
impl Write for Sink {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
Expand All @@ -1405,9 +1397,7 @@ pub fn run_test(opts: &TestOpts,
None
};

let result = catch_unwind(AssertUnwindSafe(|| {
testfn.call_box(())
}));
let result = catch_unwind(AssertUnwindSafe(testfn));

if let Some((printio, panicio)) = oldio {
io::set_print(printio);
Expand Down Expand Up @@ -1449,14 +1439,14 @@ pub fn run_test(opts: &TestOpts,
return;
}
DynTestFn(f) => {
let cb = move |()| {
__rust_begin_short_backtrace(|| f.call_box(()))
let cb = move || {
__rust_begin_short_backtrace(f)
};
run_test_inner(desc, monitor_ch, opts.nocapture, Box::new(cb))
}
StaticTestFn(f) =>
run_test_inner(desc, monitor_ch, opts.nocapture,
Box::new(move |()| __rust_begin_short_backtrace(f))),
Box::new(move || __rust_begin_short_backtrace(f))),
}
}

Expand Down Expand Up @@ -1720,7 +1710,7 @@ mod tests {
should_panic: ShouldPanic::No,
allow_fail: false,
},
testfn: DynTestFn(Box::new(move |()| f())),
testfn: DynTestFn(Box::new(f)),
};
let (tx, rx) = channel();
run_test(&TestOpts::new(), false, desc, tx);
Expand All @@ -1738,7 +1728,7 @@ mod tests {
should_panic: ShouldPanic::No,
allow_fail: false,
},
testfn: DynTestFn(Box::new(move |()| f())),
testfn: DynTestFn(Box::new(f)),
};
let (tx, rx) = channel();
run_test(&TestOpts::new(), false, desc, tx);
Expand All @@ -1758,7 +1748,7 @@ mod tests {
should_panic: ShouldPanic::Yes,
allow_fail: false,
},
testfn: DynTestFn(Box::new(move |()| f())),
testfn: DynTestFn(Box::new(f)),
};
let (tx, rx) = channel();
run_test(&TestOpts::new(), false, desc, tx);
Expand All @@ -1778,7 +1768,7 @@ mod tests {
should_panic: ShouldPanic::YesWithMessage("error message"),
allow_fail: false,
},
testfn: DynTestFn(Box::new(move |()| f())),
testfn: DynTestFn(Box::new(f)),
};
let (tx, rx) = channel();
run_test(&TestOpts::new(), false, desc, tx);
Expand All @@ -1800,7 +1790,7 @@ mod tests {
should_panic: ShouldPanic::YesWithMessage(expected),
allow_fail: false,
},
testfn: DynTestFn(Box::new(move |()| f())),
testfn: DynTestFn(Box::new(f)),
};
let (tx, rx) = channel();
run_test(&TestOpts::new(), false, desc, tx);
Expand All @@ -1818,7 +1808,7 @@ mod tests {
should_panic: ShouldPanic::Yes,
allow_fail: false,
},
testfn: DynTestFn(Box::new(move |()| f())),
testfn: DynTestFn(Box::new(f)),
};
let (tx, rx) = channel();
run_test(&TestOpts::new(), false, desc, tx);
Expand Down Expand Up @@ -1852,7 +1842,7 @@ mod tests {
should_panic: ShouldPanic::No,
allow_fail: false,
},
testfn: DynTestFn(Box::new(move |()| {})),
testfn: DynTestFn(Box::new(move || {})),
},
TestDescAndFn {
desc: TestDesc {
Expand All @@ -1861,7 +1851,7 @@ mod tests {
should_panic: ShouldPanic::No,
allow_fail: false,
},
testfn: DynTestFn(Box::new(move |()| {})),
testfn: DynTestFn(Box::new(move || {})),
}];
let filtered = filter_tests(&opts, tests);

Expand All @@ -1885,7 +1875,7 @@ mod tests {
should_panic: ShouldPanic::No,
allow_fail: false,
},
testfn: DynTestFn(Box::new(move |()| {}))
testfn: DynTestFn(Box::new(move || {}))
})
.collect()
}
Expand Down Expand Up @@ -1967,7 +1957,7 @@ mod tests {
should_panic: ShouldPanic::No,
allow_fail: false,
},
testfn: DynTestFn(Box::new(move |()| testfn())),
testfn: DynTestFn(Box::new(testfn)),
};
tests.push(test);
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/compiletest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ pub fn make_test_name(config: &Config, testpaths: &TestPaths) -> test::TestName
pub fn make_test_closure(config: &Config, testpaths: &TestPaths) -> test::TestFn {
let config = config.clone();
let testpaths = testpaths.clone();
test::DynTestFn(Box::new(move |()| runtest::run(config, &testpaths)))
test::DynTestFn(Box::new(move || runtest::run(config, &testpaths)))
}

/// Returns (Path to GDB, GDB Version, GDB has Rust Support)
Expand Down