]> git.lizzy.rs Git - rust.git/blob - src/librustc_builtin_macros/test.rs
Rollup merge of #68374 - gitletH:patch-1, r=nikomatsakis
[rust.git] / src / librustc_builtin_macros / test.rs
1 /// The expansion from a test function to the appropriate test struct for libtest
2 /// Ideally, this code would be in libtest but for efficiency and error messages it lives here.
3 use crate::util::check_builtin_macro_attribute;
4
5 use rustc_expand::base::*;
6 use rustc_span::source_map::respan;
7 use rustc_span::symbol::{sym, Symbol};
8 use rustc_span::Span;
9 use syntax::ast;
10 use syntax::attr;
11 use syntax::print::pprust;
12
13 use std::iter;
14
15 // #[test_case] is used by custom test authors to mark tests
16 // When building for test, it needs to make the item public and gensym the name
17 // Otherwise, we'll omit the item. This behavior means that any item annotated
18 // with #[test_case] is never addressable.
19 //
20 // We mark item with an inert attribute "rustc_test_marker" which the test generation
21 // logic will pick up on.
22 pub fn expand_test_case(
23     ecx: &mut ExtCtxt<'_>,
24     attr_sp: Span,
25     meta_item: &ast::MetaItem,
26     anno_item: Annotatable,
27 ) -> Vec<Annotatable> {
28     check_builtin_macro_attribute(ecx, meta_item, sym::test_case);
29
30     if !ecx.ecfg.should_test {
31         return vec![];
32     }
33
34     let sp = ecx.with_def_site_ctxt(attr_sp);
35     let mut item = anno_item.expect_item();
36     item = item.map(|mut item| {
37         item.vis = respan(item.vis.span, ast::VisibilityKind::Public);
38         item.ident.span = item.ident.span.with_ctxt(sp.ctxt());
39         item.attrs.push(ecx.attribute(ecx.meta_word(sp, sym::rustc_test_marker)));
40         item
41     });
42
43     return vec![Annotatable::Item(item)];
44 }
45
46 pub fn expand_test(
47     cx: &mut ExtCtxt<'_>,
48     attr_sp: Span,
49     meta_item: &ast::MetaItem,
50     item: Annotatable,
51 ) -> Vec<Annotatable> {
52     check_builtin_macro_attribute(cx, meta_item, sym::test);
53     expand_test_or_bench(cx, attr_sp, item, false)
54 }
55
56 pub fn expand_bench(
57     cx: &mut ExtCtxt<'_>,
58     attr_sp: Span,
59     meta_item: &ast::MetaItem,
60     item: Annotatable,
61 ) -> Vec<Annotatable> {
62     check_builtin_macro_attribute(cx, meta_item, sym::bench);
63     expand_test_or_bench(cx, attr_sp, item, true)
64 }
65
66 pub fn expand_test_or_bench(
67     cx: &mut ExtCtxt<'_>,
68     attr_sp: Span,
69     item: Annotatable,
70     is_bench: bool,
71 ) -> Vec<Annotatable> {
72     // If we're not in test configuration, remove the annotated item
73     if !cx.ecfg.should_test {
74         return vec![];
75     }
76
77     let item = if let Annotatable::Item(i) = item {
78         i
79     } else {
80         cx.parse_sess
81             .span_diagnostic
82             .span_fatal(
83                 item.span(),
84                 "`#[test]` attribute is only allowed on non associated functions",
85             )
86             .raise();
87     };
88
89     if let ast::ItemKind::Mac(_) = item.kind {
90         cx.parse_sess.span_diagnostic.span_warn(
91             item.span,
92             "`#[test]` attribute should not be used on macros. Use `#[cfg(test)]` instead.",
93         );
94         return vec![Annotatable::Item(item)];
95     }
96
97     // has_*_signature will report any errors in the type so compilation
98     // will fail. We shouldn't try to expand in this case because the errors
99     // would be spurious.
100     if (!is_bench && !has_test_signature(cx, &item))
101         || (is_bench && !has_bench_signature(cx, &item))
102     {
103         return vec![Annotatable::Item(item)];
104     }
105
106     let (sp, attr_sp) = (cx.with_def_site_ctxt(item.span), cx.with_def_site_ctxt(attr_sp));
107
108     let test_id = ast::Ident::new(sym::test, attr_sp);
109
110     // creates test::$name
111     let test_path = |name| cx.path(sp, vec![test_id, cx.ident_of(name, sp)]);
112
113     // creates test::ShouldPanic::$name
114     let should_panic_path =
115         |name| cx.path(sp, vec![test_id, cx.ident_of("ShouldPanic", sp), cx.ident_of(name, sp)]);
116
117     // creates test::TestType::$name
118     let test_type_path =
119         |name| cx.path(sp, vec![test_id, cx.ident_of("TestType", sp), cx.ident_of(name, sp)]);
120
121     // creates $name: $expr
122     let field = |name, expr| cx.field_imm(sp, cx.ident_of(name, sp), expr);
123
124     let test_fn = if is_bench {
125         // A simple ident for a lambda
126         let b = cx.ident_of("b", attr_sp);
127
128         cx.expr_call(
129             sp,
130             cx.expr_path(test_path("StaticBenchFn")),
131             vec![
132                 // |b| self::test::assert_test_result(
133                 cx.lambda1(
134                     sp,
135                     cx.expr_call(
136                         sp,
137                         cx.expr_path(test_path("assert_test_result")),
138                         vec![
139                             // super::$test_fn(b)
140                             cx.expr_call(
141                                 sp,
142                                 cx.expr_path(cx.path(sp, vec![item.ident])),
143                                 vec![cx.expr_ident(sp, b)],
144                             ),
145                         ],
146                     ),
147                     b,
148                 ), // )
149             ],
150         )
151     } else {
152         cx.expr_call(
153             sp,
154             cx.expr_path(test_path("StaticTestFn")),
155             vec![
156                 // || {
157                 cx.lambda0(
158                     sp,
159                     // test::assert_test_result(
160                     cx.expr_call(
161                         sp,
162                         cx.expr_path(test_path("assert_test_result")),
163                         vec![
164                             // $test_fn()
165                             cx.expr_call(sp, cx.expr_path(cx.path(sp, vec![item.ident])), vec![]), // )
166                         ],
167                     ), // }
168                 ), // )
169             ],
170         )
171     };
172
173     let mut test_const = cx.item(
174         sp,
175         ast::Ident::new(item.ident.name, sp),
176         vec![
177             // #[cfg(test)]
178             cx.attribute(attr::mk_list_item(
179                 ast::Ident::new(sym::cfg, attr_sp),
180                 vec![attr::mk_nested_word_item(ast::Ident::new(sym::test, attr_sp))],
181             )),
182             // #[rustc_test_marker]
183             cx.attribute(cx.meta_word(attr_sp, sym::rustc_test_marker)),
184         ],
185         // const $ident: test::TestDescAndFn =
186         ast::ItemKind::Const(
187             cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
188             // test::TestDescAndFn {
189             cx.expr_struct(
190                 sp,
191                 test_path("TestDescAndFn"),
192                 vec![
193                     // desc: test::TestDesc {
194                     field(
195                         "desc",
196                         cx.expr_struct(
197                             sp,
198                             test_path("TestDesc"),
199                             vec![
200                                 // name: "path::to::test"
201                                 field(
202                                     "name",
203                                     cx.expr_call(
204                                         sp,
205                                         cx.expr_path(test_path("StaticTestName")),
206                                         vec![cx.expr_str(
207                                             sp,
208                                             Symbol::intern(&item_path(
209                                                 // skip the name of the root module
210                                                 &cx.current_expansion.module.mod_path[1..],
211                                                 &item.ident,
212                                             )),
213                                         )],
214                                     ),
215                                 ),
216                                 // ignore: true | false
217                                 field("ignore", cx.expr_bool(sp, should_ignore(&item))),
218                                 // allow_fail: true | false
219                                 field("allow_fail", cx.expr_bool(sp, should_fail(&item))),
220                                 // should_panic: ...
221                                 field(
222                                     "should_panic",
223                                     match should_panic(cx, &item) {
224                                         // test::ShouldPanic::No
225                                         ShouldPanic::No => cx.expr_path(should_panic_path("No")),
226                                         // test::ShouldPanic::Yes
227                                         ShouldPanic::Yes(None) => {
228                                             cx.expr_path(should_panic_path("Yes"))
229                                         }
230                                         // test::ShouldPanic::YesWithMessage("...")
231                                         ShouldPanic::Yes(Some(sym)) => cx.expr_call(
232                                             sp,
233                                             cx.expr_path(should_panic_path("YesWithMessage")),
234                                             vec![cx.expr_str(sp, sym)],
235                                         ),
236                                     },
237                                 ),
238                                 // test_type: ...
239                                 field(
240                                     "test_type",
241                                     match test_type(cx) {
242                                         // test::TestType::UnitTest
243                                         TestType::UnitTest => {
244                                             cx.expr_path(test_type_path("UnitTest"))
245                                         }
246                                         // test::TestType::IntegrationTest
247                                         TestType::IntegrationTest => {
248                                             cx.expr_path(test_type_path("IntegrationTest"))
249                                         }
250                                         // test::TestPath::Unknown
251                                         TestType::Unknown => {
252                                             cx.expr_path(test_type_path("Unknown"))
253                                         }
254                                     },
255                                 ),
256                                 // },
257                             ],
258                         ),
259                     ),
260                     // testfn: test::StaticTestFn(...) | test::StaticBenchFn(...)
261                     field("testfn", test_fn), // }
262                 ],
263             ), // }
264         ),
265     );
266     test_const = test_const.map(|mut tc| {
267         tc.vis.node = ast::VisibilityKind::Public;
268         tc
269     });
270
271     // extern crate test
272     let test_extern = cx.item(sp, test_id, vec![], ast::ItemKind::ExternCrate(None));
273
274     log::debug!("synthetic test item:\n{}\n", pprust::item_to_string(&test_const));
275
276     vec![
277         // Access to libtest under a hygienic name
278         Annotatable::Item(test_extern),
279         // The generated test case
280         Annotatable::Item(test_const),
281         // The original item
282         Annotatable::Item(item),
283     ]
284 }
285
286 fn item_path(mod_path: &[ast::Ident], item_ident: &ast::Ident) -> String {
287     mod_path
288         .iter()
289         .chain(iter::once(item_ident))
290         .map(|x| x.to_string())
291         .collect::<Vec<String>>()
292         .join("::")
293 }
294
295 enum ShouldPanic {
296     No,
297     Yes(Option<Symbol>),
298 }
299
300 fn should_ignore(i: &ast::Item) -> bool {
301     attr::contains_name(&i.attrs, sym::ignore)
302 }
303
304 fn should_fail(i: &ast::Item) -> bool {
305     attr::contains_name(&i.attrs, sym::allow_fail)
306 }
307
308 fn should_panic(cx: &ExtCtxt<'_>, i: &ast::Item) -> ShouldPanic {
309     match attr::find_by_name(&i.attrs, sym::should_panic) {
310         Some(attr) => {
311             let ref sd = cx.parse_sess.span_diagnostic;
312
313             match attr.meta_item_list() {
314                 // Handle #[should_panic(expected = "foo")]
315                 Some(list) => {
316                     let msg = list
317                         .iter()
318                         .find(|mi| mi.check_name(sym::expected))
319                         .and_then(|mi| mi.meta_item())
320                         .and_then(|mi| mi.value_str());
321                     if list.len() != 1 || msg.is_none() {
322                         sd.struct_span_warn(
323                             attr.span,
324                             "argument must be of the form: \
325                              `expected = \"error message\"`",
326                         )
327                         .note(
328                             "errors in this attribute were erroneously \
329                                 allowed and will become a hard error in a \
330                                 future release.",
331                         )
332                         .emit();
333                         ShouldPanic::Yes(None)
334                     } else {
335                         ShouldPanic::Yes(msg)
336                     }
337                 }
338                 // Handle #[should_panic] and #[should_panic = "expected"]
339                 None => ShouldPanic::Yes(attr.value_str()),
340             }
341         }
342         None => ShouldPanic::No,
343     }
344 }
345
346 enum TestType {
347     UnitTest,
348     IntegrationTest,
349     Unknown,
350 }
351
352 /// Attempts to determine the type of test.
353 /// Since doctests are created without macro expanding, only possible variants here
354 /// are `UnitTest`, `IntegrationTest` or `Unknown`.
355 fn test_type(cx: &ExtCtxt<'_>) -> TestType {
356     // Root path from context contains the topmost sources directory of the crate.
357     // I.e., for `project` with sources in `src` and tests in `tests` folders
358     // (no matter how many nested folders lie inside),
359     // there will be two different root paths: `/project/src` and `/project/tests`.
360     let crate_path = cx.root_path.as_path();
361
362     if crate_path.ends_with("src") {
363         // `/src` folder contains unit-tests.
364         TestType::UnitTest
365     } else if crate_path.ends_with("tests") {
366         // `/tests` folder contains integration tests.
367         TestType::IntegrationTest
368     } else {
369         // Crate layout doesn't match expected one, test type is unknown.
370         TestType::Unknown
371     }
372 }
373
374 fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
375     let has_should_panic_attr = attr::contains_name(&i.attrs, sym::should_panic);
376     let ref sd = cx.parse_sess.span_diagnostic;
377     if let ast::ItemKind::Fn(ref sig, ref generics, _) = i.kind {
378         if sig.header.unsafety == ast::Unsafety::Unsafe {
379             sd.span_err(i.span, "unsafe functions cannot be used for tests");
380             return false;
381         }
382         if sig.header.asyncness.node.is_async() {
383             sd.span_err(i.span, "async functions cannot be used for tests");
384             return false;
385         }
386
387         // If the termination trait is active, the compiler will check that the output
388         // type implements the `Termination` trait as `libtest` enforces that.
389         let has_output = match sig.decl.output {
390             ast::FunctionRetTy::Default(..) => false,
391             ast::FunctionRetTy::Ty(ref t) if t.kind.is_unit() => false,
392             _ => true,
393         };
394
395         if !sig.decl.inputs.is_empty() {
396             sd.span_err(i.span, "functions used as tests can not have any arguments");
397             return false;
398         }
399
400         match (has_output, has_should_panic_attr) {
401             (true, true) => {
402                 sd.span_err(i.span, "functions using `#[should_panic]` must return `()`");
403                 false
404             }
405             (true, false) => {
406                 if !generics.params.is_empty() {
407                     sd.span_err(i.span, "functions used as tests must have signature fn() -> ()");
408                     false
409                 } else {
410                     true
411                 }
412             }
413             (false, _) => true,
414         }
415     } else {
416         sd.span_err(i.span, "only functions may be used as tests");
417         false
418     }
419 }
420
421 fn has_bench_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
422     let has_sig = if let ast::ItemKind::Fn(ref sig, _, _) = i.kind {
423         // N.B., inadequate check, but we're running
424         // well before resolve, can't get too deep.
425         sig.decl.inputs.len() == 1
426     } else {
427         false
428     };
429
430     if !has_sig {
431         cx.parse_sess.span_diagnostic.span_err(
432             i.span,
433             "functions used as benches must have \
434             signature `fn(&mut Bencher) -> impl Termination`",
435         );
436     }
437
438     has_sig
439 }