]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/test.rs
Merge VariantData and VariantData_
[rust.git] / src / libsyntax / test.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // Code that generates a test runner to run all the tests in a crate
12
13 #![allow(dead_code)]
14 #![allow(unused_imports)]
15 use self::HasTestSignature::*;
16
17 use std::iter;
18 use std::slice;
19 use std::mem;
20 use std::vec;
21 use ast_util::*;
22 use attr::AttrMetaMethods;
23 use attr;
24 use codemap::{DUMMY_SP, Span, ExpnInfo, NameAndSpan, MacroAttribute};
25 use codemap;
26 use diagnostic;
27 use config;
28 use entry::{self, EntryPointType};
29 use ext::base::ExtCtxt;
30 use ext::build::AstBuilder;
31 use ext::expand::ExpansionConfig;
32 use fold::{Folder, MoveMap};
33 use fold;
34 use owned_slice::OwnedSlice;
35 use parse::token::{intern, InternedString};
36 use parse::{token, ParseSess};
37 use print::pprust;
38 use {ast, ast_util};
39 use ptr::P;
40 use util::small_vector::SmallVector;
41
42 enum ShouldPanic {
43     No,
44     Yes(Option<InternedString>),
45 }
46
47 struct Test {
48     span: Span,
49     path: Vec<ast::Ident> ,
50     bench: bool,
51     ignore: bool,
52     should_panic: ShouldPanic
53 }
54
55 struct TestCtxt<'a> {
56     sess: &'a ParseSess,
57     span_diagnostic: &'a diagnostic::SpanHandler,
58     path: Vec<ast::Ident>,
59     ext_cx: ExtCtxt<'a>,
60     testfns: Vec<Test>,
61     reexport_test_harness_main: Option<InternedString>,
62     is_test_crate: bool,
63     config: ast::CrateConfig,
64
65     // top-level re-export submodule, filled out after folding is finished
66     toplevel_reexport: Option<ast::Ident>,
67 }
68
69 // Traverse the crate, collecting all the test functions, eliding any
70 // existing main functions, and synthesizing a main test harness
71 pub fn modify_for_testing(sess: &ParseSess,
72                           cfg: &ast::CrateConfig,
73                           krate: ast::Crate,
74                           span_diagnostic: &diagnostic::SpanHandler) -> ast::Crate {
75     // We generate the test harness when building in the 'test'
76     // configuration, either with the '--test' or '--cfg test'
77     // command line options.
78     let should_test = attr::contains_name(&krate.config, "test");
79
80     // Check for #[reexport_test_harness_main = "some_name"] which
81     // creates a `use some_name = __test::main;`. This needs to be
82     // unconditional, so that the attribute is still marked as used in
83     // non-test builds.
84     let reexport_test_harness_main =
85         attr::first_attr_value_str_by_name(&krate.attrs,
86                                            "reexport_test_harness_main");
87
88     if should_test {
89         generate_test_harness(sess, reexport_test_harness_main, krate, cfg, span_diagnostic)
90     } else {
91         strip_test_functions(krate)
92     }
93 }
94
95 struct TestHarnessGenerator<'a> {
96     cx: TestCtxt<'a>,
97     tests: Vec<ast::Ident>,
98
99     // submodule name, gensym'd identifier for re-exports
100     tested_submods: Vec<(ast::Ident, ast::Ident)>,
101 }
102
103 impl<'a> fold::Folder for TestHarnessGenerator<'a> {
104     fn fold_crate(&mut self, c: ast::Crate) -> ast::Crate {
105         let mut folded = fold::noop_fold_crate(c, self);
106
107         // Add a special __test module to the crate that will contain code
108         // generated for the test harness
109         let (mod_, reexport) = mk_test_module(&mut self.cx);
110         match reexport {
111             Some(re) => folded.module.items.push(re),
112             None => {}
113         }
114         folded.module.items.push(mod_);
115         folded
116     }
117
118     fn fold_item(&mut self, i: P<ast::Item>) -> SmallVector<P<ast::Item>> {
119         let ident = i.ident;
120         if ident.name != token::special_idents::invalid.name {
121             self.cx.path.push(ident);
122         }
123         debug!("current path: {}",
124                ast_util::path_name_i(&self.cx.path));
125
126         let i = if is_test_fn(&self.cx, &*i) || is_bench_fn(&self.cx, &*i) {
127             match i.node {
128                 ast::ItemFn(_, ast::Unsafety::Unsafe, _, _, _, _) => {
129                     let diag = self.cx.span_diagnostic;
130                     panic!(diag.span_fatal(i.span, "unsafe functions cannot be used for tests"));
131                 }
132                 _ => {
133                     debug!("this is a test function");
134                     let test = Test {
135                         span: i.span,
136                         path: self.cx.path.clone(),
137                         bench: is_bench_fn(&self.cx, &*i),
138                         ignore: is_ignored(&*i),
139                         should_panic: should_panic(&*i)
140                     };
141                     self.cx.testfns.push(test);
142                     self.tests.push(i.ident);
143                     // debug!("have {} test/bench functions",
144                     //        cx.testfns.len());
145
146                     // Make all tests public so we can call them from outside
147                     // the module (note that the tests are re-exported and must
148                     // be made public themselves to avoid privacy errors).
149                     i.map(|mut i| {
150                         i.vis = ast::Public;
151                         i
152                     })
153                 }
154             }
155         } else {
156             i
157         };
158
159         // We don't want to recurse into anything other than mods, since
160         // mods or tests inside of functions will break things
161         let res = match i.node {
162             ast::ItemMod(..) => fold::noop_fold_item(i, self),
163             _ => SmallVector::one(i),
164         };
165         if ident.name != token::special_idents::invalid.name {
166             self.cx.path.pop();
167         }
168         res
169     }
170
171     fn fold_mod(&mut self, m: ast::Mod) -> ast::Mod {
172         let tests = mem::replace(&mut self.tests, Vec::new());
173         let tested_submods = mem::replace(&mut self.tested_submods, Vec::new());
174         let mut mod_folded = fold::noop_fold_mod(m, self);
175         let tests = mem::replace(&mut self.tests, tests);
176         let tested_submods = mem::replace(&mut self.tested_submods, tested_submods);
177
178         if !tests.is_empty() || !tested_submods.is_empty() {
179             let (it, sym) = mk_reexport_mod(&mut self.cx, tests, tested_submods);
180             mod_folded.items.push(it);
181
182             if !self.cx.path.is_empty() {
183                 self.tested_submods.push((self.cx.path[self.cx.path.len()-1], sym));
184             } else {
185                 debug!("pushing nothing, sym: {:?}", sym);
186                 self.cx.toplevel_reexport = Some(sym);
187             }
188         }
189
190         mod_folded
191     }
192 }
193
194 struct EntryPointCleaner {
195     // Current depth in the ast
196     depth: usize,
197 }
198
199 impl fold::Folder for EntryPointCleaner {
200     fn fold_item(&mut self, i: P<ast::Item>) -> SmallVector<P<ast::Item>> {
201         self.depth += 1;
202         let folded = fold::noop_fold_item(i, self).expect_one("noop did something");
203         self.depth -= 1;
204
205         // Remove any #[main] or #[start] from the AST so it doesn't
206         // clash with the one we're going to add, but mark it as
207         // #[allow(dead_code)] to avoid printing warnings.
208         let folded = match entry::entry_point_type(&*folded, self.depth) {
209             EntryPointType::MainNamed |
210             EntryPointType::MainAttr |
211             EntryPointType::Start =>
212                 folded.map(|ast::Item {id, ident, attrs, node, vis, span}| {
213                     let allow_str = InternedString::new("allow");
214                     let dead_code_str = InternedString::new("dead_code");
215                     let allow_dead_code_item =
216                         attr::mk_list_item(allow_str,
217                                            vec![attr::mk_word_item(dead_code_str)]);
218                     let allow_dead_code = attr::mk_attr_outer(attr::mk_attr_id(),
219                                                               allow_dead_code_item);
220
221                     ast::Item {
222                         id: id,
223                         ident: ident,
224                         attrs: attrs.into_iter()
225                             .filter(|attr| {
226                                 !attr.check_name("main") && !attr.check_name("start")
227                             })
228                             .chain(iter::once(allow_dead_code))
229                             .collect(),
230                         node: node,
231                         vis: vis,
232                         span: span
233                     }
234                 }),
235             EntryPointType::None |
236             EntryPointType::OtherMain => folded,
237         };
238
239         SmallVector::one(folded)
240     }
241 }
242
243 fn mk_reexport_mod(cx: &mut TestCtxt, tests: Vec<ast::Ident>,
244                    tested_submods: Vec<(ast::Ident, ast::Ident)>) -> (P<ast::Item>, ast::Ident) {
245     let super_ = token::str_to_ident("super");
246
247     let items = tests.into_iter().map(|r| {
248         cx.ext_cx.item_use_simple(DUMMY_SP, ast::Public,
249                                   cx.ext_cx.path(DUMMY_SP, vec![super_, r]))
250     }).chain(tested_submods.into_iter().map(|(r, sym)| {
251         let path = cx.ext_cx.path(DUMMY_SP, vec![super_, r, sym]);
252         cx.ext_cx.item_use_simple_(DUMMY_SP, ast::Public, r, path)
253     }));
254
255     let reexport_mod = ast::Mod {
256         inner: DUMMY_SP,
257         items: items.collect(),
258     };
259
260     let sym = token::gensym_ident("__test_reexports");
261     let it = P(ast::Item {
262         ident: sym.clone(),
263         attrs: Vec::new(),
264         id: ast::DUMMY_NODE_ID,
265         node: ast::ItemMod(reexport_mod),
266         vis: ast::Public,
267         span: DUMMY_SP,
268     });
269
270     (it, sym)
271 }
272
273 fn generate_test_harness(sess: &ParseSess,
274                          reexport_test_harness_main: Option<InternedString>,
275                          krate: ast::Crate,
276                          cfg: &ast::CrateConfig,
277                          sd: &diagnostic::SpanHandler) -> ast::Crate {
278     // Remove the entry points
279     let mut cleaner = EntryPointCleaner { depth: 0 };
280     let krate = cleaner.fold_crate(krate);
281
282     let mut feature_gated_cfgs = vec![];
283     let mut cx: TestCtxt = TestCtxt {
284         sess: sess,
285         span_diagnostic: sd,
286         ext_cx: ExtCtxt::new(sess, cfg.clone(),
287                              ExpansionConfig::default("test".to_string()),
288                              &mut feature_gated_cfgs),
289         path: Vec::new(),
290         testfns: Vec::new(),
291         reexport_test_harness_main: reexport_test_harness_main,
292         is_test_crate: is_test_crate(&krate),
293         config: krate.config.clone(),
294         toplevel_reexport: None,
295     };
296     cx.ext_cx.crate_root = Some("std");
297
298     cx.ext_cx.bt_push(ExpnInfo {
299         call_site: DUMMY_SP,
300         callee: NameAndSpan {
301             format: MacroAttribute(intern("test")),
302             span: None,
303             allow_internal_unstable: false,
304         }
305     });
306
307     let mut fold = TestHarnessGenerator {
308         cx: cx,
309         tests: Vec::new(),
310         tested_submods: Vec::new(),
311     };
312     let res = fold.fold_crate(krate);
313     fold.cx.ext_cx.bt_pop();
314     return res;
315 }
316
317 fn strip_test_functions(krate: ast::Crate) -> ast::Crate {
318     // When not compiling with --test we should not compile the
319     // #[test] functions
320     config::strip_items(krate, |attrs| {
321         !attr::contains_name(&attrs[..], "test") &&
322         !attr::contains_name(&attrs[..], "bench")
323     })
324 }
325
326 /// Craft a span that will be ignored by the stability lint's
327 /// call to codemap's is_internal check.
328 /// The expanded code calls some unstable functions in the test crate.
329 fn ignored_span(cx: &TestCtxt, sp: Span) -> Span {
330     let info = ExpnInfo {
331         call_site: DUMMY_SP,
332         callee: NameAndSpan {
333             format: MacroAttribute(intern("test")),
334             span: None,
335             allow_internal_unstable: true,
336         }
337     };
338     let expn_id = cx.sess.codemap().record_expansion(info);
339     let mut sp = sp;
340     sp.expn_id = expn_id;
341     return sp;
342 }
343
344 #[derive(PartialEq)]
345 enum HasTestSignature {
346     Yes,
347     No,
348     NotEvenAFunction,
349 }
350
351
352 fn is_test_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
353     let has_test_attr = attr::contains_name(&i.attrs, "test");
354
355     fn has_test_signature(i: &ast::Item) -> HasTestSignature {
356         match &i.node {
357           &ast::ItemFn(ref decl, _, _, _, ref generics, _) => {
358             let no_output = match decl.output {
359                 ast::DefaultReturn(..) => true,
360                 ast::Return(ref t) if t.node == ast::TyTup(vec![]) => true,
361                 _ => false
362             };
363             if decl.inputs.is_empty()
364                    && no_output
365                    && !generics.is_parameterized() {
366                 Yes
367             } else {
368                 No
369             }
370           }
371           _ => NotEvenAFunction,
372         }
373     }
374
375     if has_test_attr {
376         let diag = cx.span_diagnostic;
377         match has_test_signature(i) {
378             Yes => {},
379             No => diag.span_err(i.span, "functions used as tests must have signature fn() -> ()"),
380             NotEvenAFunction => diag.span_err(i.span,
381                                               "only functions may be used as tests"),
382         }
383     }
384
385     return has_test_attr && has_test_signature(i) == Yes;
386 }
387
388 fn is_bench_fn(cx: &TestCtxt, i: &ast::Item) -> bool {
389     let has_bench_attr = attr::contains_name(&i.attrs, "bench");
390
391     fn has_test_signature(i: &ast::Item) -> bool {
392         match i.node {
393             ast::ItemFn(ref decl, _, _, _, ref generics, _) => {
394                 let input_cnt = decl.inputs.len();
395                 let no_output = match decl.output {
396                     ast::DefaultReturn(..) => true,
397                     ast::Return(ref t) if t.node == ast::TyTup(vec![]) => true,
398                     _ => false
399                 };
400                 let tparm_cnt = generics.ty_params.len();
401                 // NB: inadequate check, but we're running
402                 // well before resolve, can't get too deep.
403                 input_cnt == 1
404                     && no_output && tparm_cnt == 0
405             }
406           _ => false
407         }
408     }
409
410     if has_bench_attr && !has_test_signature(i) {
411         let diag = cx.span_diagnostic;
412         diag.span_err(i.span, "functions used as benches must have signature \
413                       `fn(&mut Bencher) -> ()`");
414     }
415
416     return has_bench_attr && has_test_signature(i);
417 }
418
419 fn is_ignored(i: &ast::Item) -> bool {
420     i.attrs.iter().any(|attr| attr.check_name("ignore"))
421 }
422
423 fn should_panic(i: &ast::Item) -> ShouldPanic {
424     match i.attrs.iter().find(|attr| attr.check_name("should_panic")) {
425         Some(attr) => {
426             let msg = attr.meta_item_list()
427                 .and_then(|list| list.iter().find(|mi| mi.check_name("expected")))
428                 .and_then(|mi| mi.value_str());
429             ShouldPanic::Yes(msg)
430         }
431         None => ShouldPanic::No,
432     }
433 }
434
435 /*
436
437 We're going to be building a module that looks more or less like:
438
439 mod __test {
440   extern crate test (name = "test", vers = "...");
441   fn main() {
442     test::test_main_static(&::os::args()[], tests)
443   }
444
445   static tests : &'static [test::TestDescAndFn] = &[
446     ... the list of tests in the crate ...
447   ];
448 }
449
450 */
451
452 fn mk_std(cx: &TestCtxt) -> P<ast::Item> {
453     let id_test = token::str_to_ident("test");
454     let (vi, vis, ident) = if cx.is_test_crate {
455         (ast::ItemUse(
456             P(nospan(ast::ViewPathSimple(id_test,
457                                          path_node(vec!(id_test)))))),
458          ast::Public, token::special_idents::invalid)
459     } else {
460         (ast::ItemExternCrate(None), ast::Inherited, id_test)
461     };
462     P(ast::Item {
463         id: ast::DUMMY_NODE_ID,
464         ident: ident,
465         node: vi,
466         attrs: vec![],
467         vis: vis,
468         span: DUMMY_SP
469     })
470 }
471
472 fn mk_main(cx: &mut TestCtxt) -> P<ast::Item> {
473     // Writing this out by hand with 'ignored_span':
474     //        pub fn main() {
475     //            #![main]
476     //            use std::slice::AsSlice;
477     //            test::test_main_static(::std::os::args().as_slice(), TESTS);
478     //        }
479
480     let sp = ignored_span(cx, DUMMY_SP);
481     let ecx = &cx.ext_cx;
482
483     // test::test_main_static
484     let test_main_path = ecx.path(sp, vec![token::str_to_ident("test"),
485                                            token::str_to_ident("test_main_static")]);
486     // test::test_main_static(...)
487     let test_main_path_expr = ecx.expr_path(test_main_path);
488     let tests_ident_expr = ecx.expr_ident(sp, token::str_to_ident("TESTS"));
489     let call_test_main = ecx.expr_call(sp, test_main_path_expr,
490                                        vec![tests_ident_expr]);
491     let call_test_main = ecx.stmt_expr(call_test_main);
492     // #![main]
493     let main_meta = ecx.meta_word(sp, token::intern_and_get_ident("main"));
494     let main_attr = ecx.attribute(sp, main_meta);
495     // pub fn main() { ... }
496     let main_ret_ty = ecx.ty(sp, ast::TyTup(vec![]));
497     let main_body = ecx.block_all(sp, vec![call_test_main], None);
498     let main = ast::ItemFn(ecx.fn_decl(vec![], main_ret_ty),
499                            ast::Unsafety::Normal,
500                            ast::Constness::NotConst,
501                            ::abi::Rust, empty_generics(), main_body);
502     let main = P(ast::Item {
503         ident: token::str_to_ident("main"),
504         attrs: vec![main_attr],
505         id: ast::DUMMY_NODE_ID,
506         node: main,
507         vis: ast::Public,
508         span: sp
509     });
510
511     return main;
512 }
513
514 fn mk_test_module(cx: &mut TestCtxt) -> (P<ast::Item>, Option<P<ast::Item>>) {
515     // Link to test crate
516     let import = mk_std(cx);
517
518     // A constant vector of test descriptors.
519     let tests = mk_tests(cx);
520
521     // The synthesized main function which will call the console test runner
522     // with our list of tests
523     let mainfn = mk_main(cx);
524
525     let testmod = ast::Mod {
526         inner: DUMMY_SP,
527         items: vec![import, mainfn, tests],
528     };
529     let item_ = ast::ItemMod(testmod);
530
531     let mod_ident = token::gensym_ident("__test");
532     let item = P(ast::Item {
533         id: ast::DUMMY_NODE_ID,
534         ident: mod_ident,
535         attrs: vec![],
536         node: item_,
537         vis: ast::Public,
538         span: DUMMY_SP,
539     });
540     let reexport = cx.reexport_test_harness_main.as_ref().map(|s| {
541         // building `use <ident> = __test::main`
542         let reexport_ident = token::str_to_ident(&s);
543
544         let use_path =
545             nospan(ast::ViewPathSimple(reexport_ident,
546                                        path_node(vec![mod_ident, token::str_to_ident("main")])));
547
548         P(ast::Item {
549             id: ast::DUMMY_NODE_ID,
550             ident: token::special_idents::invalid,
551             attrs: vec![],
552             node: ast::ItemUse(P(use_path)),
553             vis: ast::Inherited,
554             span: DUMMY_SP
555         })
556     });
557
558     debug!("Synthetic test module:\n{}\n", pprust::item_to_string(&*item));
559
560     (item, reexport)
561 }
562
563 fn nospan<T>(t: T) -> codemap::Spanned<T> {
564     codemap::Spanned { node: t, span: DUMMY_SP }
565 }
566
567 fn path_node(ids: Vec<ast::Ident> ) -> ast::Path {
568     ast::Path {
569         span: DUMMY_SP,
570         global: false,
571         segments: ids.into_iter().map(|identifier| ast::PathSegment {
572             identifier: identifier,
573             parameters: ast::PathParameters::none(),
574         }).collect()
575     }
576 }
577
578 fn mk_tests(cx: &TestCtxt) -> P<ast::Item> {
579     // The vector of test_descs for this crate
580     let test_descs = mk_test_descs(cx);
581
582     // FIXME #15962: should be using quote_item, but that stringifies
583     // __test_reexports, causing it to be reinterned, losing the
584     // gensym information.
585     let sp = DUMMY_SP;
586     let ecx = &cx.ext_cx;
587     let struct_type = ecx.ty_path(ecx.path(sp, vec![ecx.ident_of("self"),
588                                                     ecx.ident_of("test"),
589                                                     ecx.ident_of("TestDescAndFn")]));
590     let static_lt = ecx.lifetime(sp, token::special_idents::static_lifetime.name);
591     // &'static [self::test::TestDescAndFn]
592     let static_type = ecx.ty_rptr(sp,
593                                   ecx.ty(sp, ast::TyVec(struct_type)),
594                                   Some(static_lt),
595                                   ast::MutImmutable);
596     // static TESTS: $static_type = &[...];
597     ecx.item_const(sp,
598                    ecx.ident_of("TESTS"),
599                    static_type,
600                    test_descs)
601 }
602
603 fn is_test_crate(krate: &ast::Crate) -> bool {
604     match attr::find_crate_name(&krate.attrs) {
605         Some(ref s) if "test" == &s[..] => true,
606         _ => false
607     }
608 }
609
610 fn mk_test_descs(cx: &TestCtxt) -> P<ast::Expr> {
611     debug!("building test vector from {} tests", cx.testfns.len());
612
613     P(ast::Expr {
614         id: ast::DUMMY_NODE_ID,
615         node: ast::ExprAddrOf(ast::MutImmutable,
616             P(ast::Expr {
617                 id: ast::DUMMY_NODE_ID,
618                 node: ast::ExprVec(cx.testfns.iter().map(|test| {
619                     mk_test_desc_and_fn_rec(cx, test)
620                 }).collect()),
621                 span: DUMMY_SP,
622             })),
623         span: DUMMY_SP,
624     })
625 }
626
627 fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> P<ast::Expr> {
628     // FIXME #15962: should be using quote_expr, but that stringifies
629     // __test_reexports, causing it to be reinterned, losing the
630     // gensym information.
631
632     let span = ignored_span(cx, test.span);
633     let path = test.path.clone();
634     let ecx = &cx.ext_cx;
635     let self_id = ecx.ident_of("self");
636     let test_id = ecx.ident_of("test");
637
638     // creates self::test::$name
639     let test_path = |name| {
640         ecx.path(span, vec![self_id, test_id, ecx.ident_of(name)])
641     };
642     // creates $name: $expr
643     let field = |name, expr| ecx.field_imm(span, ecx.ident_of(name), expr);
644
645     debug!("encoding {}", ast_util::path_name_i(&path[..]));
646
647     // path to the #[test] function: "foo::bar::baz"
648     let path_string = ast_util::path_name_i(&path[..]);
649     let name_expr = ecx.expr_str(span, token::intern_and_get_ident(&path_string[..]));
650
651     // self::test::StaticTestName($name_expr)
652     let name_expr = ecx.expr_call(span,
653                                   ecx.expr_path(test_path("StaticTestName")),
654                                   vec![name_expr]);
655
656     let ignore_expr = ecx.expr_bool(span, test.ignore);
657     let should_panic_path = |name| {
658         ecx.path(span, vec![self_id, test_id, ecx.ident_of("ShouldPanic"), ecx.ident_of(name)])
659     };
660     let fail_expr = match test.should_panic {
661         ShouldPanic::No => ecx.expr_path(should_panic_path("No")),
662         ShouldPanic::Yes(ref msg) => {
663             match *msg {
664                 Some(ref msg) => {
665                     let msg = ecx.expr_str(span, msg.clone());
666                     let path = should_panic_path("YesWithMessage");
667                     ecx.expr_call(span, ecx.expr_path(path), vec![msg])
668                 }
669                 None => ecx.expr_path(should_panic_path("Yes")),
670             }
671         }
672     };
673
674     // self::test::TestDesc { ... }
675     let desc_expr = ecx.expr_struct(
676         span,
677         test_path("TestDesc"),
678         vec![field("name", name_expr),
679              field("ignore", ignore_expr),
680              field("should_panic", fail_expr)]);
681
682
683     let mut visible_path = match cx.toplevel_reexport {
684         Some(id) => vec![id],
685         None => {
686             let diag = cx.span_diagnostic;
687             diag.handler.bug("expected to find top-level re-export name, but found None");
688         }
689     };
690     visible_path.extend(path);
691
692     let fn_expr = ecx.expr_path(ecx.path_global(span, visible_path));
693
694     let variant_name = if test.bench { "StaticBenchFn" } else { "StaticTestFn" };
695     // self::test::$variant_name($fn_expr)
696     let testfn_expr = ecx.expr_call(span, ecx.expr_path(test_path(variant_name)), vec![fn_expr]);
697
698     // self::test::TestDescAndFn { ... }
699     ecx.expr_struct(span,
700                     test_path("TestDescAndFn"),
701                     vec![field("desc", desc_expr),
702                          field("testfn", testfn_expr)])
703 }