Skip to content

Commit 067c2e3

Browse files
committed
handle #[bench] functions better
1 parent 8f35141 commit 067c2e3

File tree

1 file changed

+43
-19
lines changed

1 file changed

+43
-19
lines changed

src/libsyntax/test.rs

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -747,9 +747,10 @@ fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> P<ast::Expr> {
747747
visible_path.extend(path);
748748

749749
// Rather than directly give the test function to the test
750-
// harness, we create a wrapper like this:
750+
// harness, we create a wrapper like one of the following:
751751
//
752-
// || test::assert_test_result(real_function())
752+
// || test::assert_test_result(real_function()) // for test
753+
// |b| test::assert_test_result(real_function(b)) // for bench
753754
//
754755
// this will coerce into a fn pointer that is specialized to the
755756
// actual return type of `real_function` (Typically `()`, but not always).
@@ -758,24 +759,47 @@ fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> P<ast::Expr> {
758759
let real_function_expr = ecx.expr_path(ecx.path_global(span, visible_path));
759760
// construct path `test::assert_test_result`
760761
let assert_test_result = test_path("assert_test_result");
761-
// construct `|| {..}`
762-
ecx.lambda(
763-
span,
764-
vec![],
765-
// construct `assert_test_result(..)`
766-
ecx.expr_call(
762+
if test.bench {
763+
// construct `|b| {..}`
764+
let b_ident = Ident::with_empty_ctxt(Symbol::gensym("b"));
765+
let b_expr = ecx.expr_ident(span, b_ident);
766+
ecx.lambda(
767767
span,
768-
ecx.expr_path(assert_test_result),
769-
vec![
770-
// construct `real_function()`
771-
ecx.expr_call(
772-
span,
773-
real_function_expr,
774-
vec![],
775-
)
776-
],
777-
),
778-
)
768+
vec![b_ident],
769+
// construct `assert_test_result(..)`
770+
ecx.expr_call(
771+
span,
772+
ecx.expr_path(assert_test_result),
773+
vec![
774+
// construct `real_function(b)`
775+
ecx.expr_call(
776+
span,
777+
real_function_expr,
778+
vec![b_expr],
779+
)
780+
],
781+
),
782+
)
783+
} else {
784+
// construct `|| {..}`
785+
ecx.lambda(
786+
span,
787+
vec![],
788+
// construct `assert_test_result(..)`
789+
ecx.expr_call(
790+
span,
791+
ecx.expr_path(assert_test_result),
792+
vec![
793+
// construct `real_function()`
794+
ecx.expr_call(
795+
span,
796+
real_function_expr,
797+
vec![],
798+
)
799+
],
800+
),
801+
)
802+
}
779803
};
780804

781805
let variant_name = if test.bench { "StaticBenchFn" } else { "StaticTestFn" };

0 commit comments

Comments
 (0)