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