handle #[bench] functions better

This commit is contained in:
Niko Matsakis 2018-02-22 20:09:10 -05:00
parent 8f35141fba
commit 067c2e3d03

View File

@ -747,9 +747,10 @@ fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> P<ast::Expr> {
visible_path.extend(path);
// Rather than directly give the test function to the test
// harness, we create a wrapper like this:
// harness, we create a wrapper like one of the following:
//
// || test::assert_test_result(real_function())
// || test::assert_test_result(real_function()) // for test
// |b| test::assert_test_result(real_function(b)) // for bench
//
// this will coerce into a fn pointer that is specialized to the
// actual return type of `real_function` (Typically `()`, but not always).
@ -758,6 +759,28 @@ fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> P<ast::Expr> {
let real_function_expr = ecx.expr_path(ecx.path_global(span, visible_path));
// construct path `test::assert_test_result`
let assert_test_result = test_path("assert_test_result");
if test.bench {
// construct `|b| {..}`
let b_ident = Ident::with_empty_ctxt(Symbol::gensym("b"));
let b_expr = ecx.expr_ident(span, b_ident);
ecx.lambda(
span,
vec![b_ident],
// construct `assert_test_result(..)`
ecx.expr_call(
span,
ecx.expr_path(assert_test_result),
vec![
// construct `real_function(b)`
ecx.expr_call(
span,
real_function_expr,
vec![b_expr],
)
],
),
)
} else {
// construct `|| {..}`
ecx.lambda(
span,
@ -776,6 +799,7 @@ fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> P<ast::Expr> {
],
),
)
}
};
let variant_name = if test.bench { "StaticBenchFn" } else { "StaticTestFn" };