]> git.lizzy.rs Git - rust.git/blob - src/librustc_builtin_macros/test.rs
Fix missed same-sized member clash in ClashingExternDeclarations.
[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_ast::ast;
6 use rustc_ast::attr;
7 use rustc_ast_pretty::pprust;
8 use rustc_expand::base::*;
9 use rustc_span::source_map::respan;
10 use rustc_span::symbol::{sym, Ident, Symbol};
11 use rustc_span::Span;
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 = match item {
78         Annotatable::Item(i) => i,
79         other => {
80             cx.struct_span_err(
81                 other.span(),
82                 "`#[test]` attribute is only allowed on non associated functions",
83             )
84             .emit();
85             return vec![other];
86         }
87     };
88
89     if let ast::ItemKind::MacCall(_) = 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 = Ident::new(sym::test, attr_sp);
109
110     // creates test::$name
111     let test_path = |name| cx.path(sp, vec![test_id, Ident::from_str_and_span(name, sp)]);
112
113     // creates test::ShouldPanic::$name
114     let should_panic_path = |name| {
115         cx.path(
116             sp,
117             vec![
118                 test_id,
119                 Ident::from_str_and_span("ShouldPanic", sp),
120                 Ident::from_str_and_span(name, sp),
121             ],
122         )
123     };
124
125     // creates test::TestType::$name
126     let test_type_path = |name| {
127         cx.path(
128             sp,
129             vec![
130                 test_id,
131                 Ident::from_str_and_span("TestType", sp),
132                 Ident::from_str_and_span(name, sp),
133             ],
134         )
135     };
136
137     // creates $name: $expr
138     let field = |name, expr| cx.field_imm(sp, Ident::from_str_and_span(name, sp), expr);
139
140     let test_fn = if is_bench {
141         // A simple ident for a lambda
142         let b = Ident::from_str_and_span("b", attr_sp);
143
144         cx.expr_call(
145             sp,
146             cx.expr_path(test_path("StaticBenchFn")),
147             vec![
148                 // |b| self::test::assert_test_result(
149                 cx.lambda1(
150                     sp,
151                     cx.expr_call(
152                         sp,
153                         cx.expr_path(test_path("assert_test_result")),
154                         vec![
155                             // super::$test_fn(b)
156                             cx.expr_call(
157                                 sp,
158                                 cx.expr_path(cx.path(sp, vec![item.ident])),
159                                 vec![cx.expr_ident(sp, b)],
160                             ),
161                         ],
162                     ),
163                     b,
164                 ), // )
165             ],
166         )
167     } else {
168         cx.expr_call(
169             sp,
170             cx.expr_path(test_path("StaticTestFn")),
171             vec![
172                 // || {
173                 cx.lambda0(
174                     sp,
175                     // test::assert_test_result(
176                     cx.expr_call(
177                         sp,
178                         cx.expr_path(test_path("assert_test_result")),
179                         vec![
180                             // $test_fn()
181                             cx.expr_call(sp, cx.expr_path(cx.path(sp, vec![item.ident])), vec![]), // )
182                         ],
183                     ), // }
184                 ), // )
185             ],
186         )
187     };
188
189     let mut test_const = cx.item(
190         sp,
191         Ident::new(item.ident.name, sp),
192         vec![
193             // #[cfg(test)]
194             cx.attribute(attr::mk_list_item(
195                 Ident::new(sym::cfg, attr_sp),
196                 vec![attr::mk_nested_word_item(Ident::new(sym::test, attr_sp))],
197             )),
198             // #[rustc_test_marker]
199             cx.attribute(cx.meta_word(attr_sp, sym::rustc_test_marker)),
200         ],
201         // const $ident: test::TestDescAndFn =
202         ast::ItemKind::Const(
203             ast::Defaultness::Final,
204             cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
205             // test::TestDescAndFn {
206             Some(
207                 cx.expr_struct(
208                     sp,
209                     test_path("TestDescAndFn"),
210                     vec![
211                         // desc: test::TestDesc {
212                         field(
213                             "desc",
214                             cx.expr_struct(
215                                 sp,
216                                 test_path("TestDesc"),
217                                 vec![
218                                     // name: "path::to::test"
219                                     field(
220                                         "name",
221                                         cx.expr_call(
222                                             sp,
223                                             cx.expr_path(test_path("StaticTestName")),
224                                             vec![cx.expr_str(
225                                                 sp,
226                                                 Symbol::intern(&item_path(
227                                                     // skip the name of the root module
228                                                     &cx.current_expansion.module.mod_path[1..],
229                                                     &item.ident,
230                                                 )),
231                                             )],
232                                         ),
233                                     ),
234                                     // ignore: true | false
235                                     field("ignore", cx.expr_bool(sp, should_ignore(&item))),
236                                     // allow_fail: true | false
237                                     field("allow_fail", cx.expr_bool(sp, should_fail(&item))),
238                                     // should_panic: ...
239                                     field(
240                                         "should_panic",
241                                         match should_panic(cx, &item) {
242                                             // test::ShouldPanic::No
243                                             ShouldPanic::No => {
244                                                 cx.expr_path(should_panic_path("No"))
245                                             }
246                                             // test::ShouldPanic::Yes
247                                             ShouldPanic::Yes(None) => {
248                                                 cx.expr_path(should_panic_path("Yes"))
249                                             }
250                                             // test::ShouldPanic::YesWithMessage("...")
251                                             ShouldPanic::Yes(Some(sym)) => cx.expr_call(
252                                                 sp,
253                                                 cx.expr_path(should_panic_path("YesWithMessage")),
254                                                 vec![cx.expr_str(sp, sym)],
255                                             ),
256                                         },
257                                     ),
258                                     // test_type: ...
259                                     field(
260                                         "test_type",
261                                         match test_type(cx) {
262                                             // test::TestType::UnitTest
263                                             TestType::UnitTest => {
264                                                 cx.expr_path(test_type_path("UnitTest"))
265                                             }
266                                             // test::TestType::IntegrationTest
267                                             TestType::IntegrationTest => {
268                                                 cx.expr_path(test_type_path("IntegrationTest"))
269                                             }
270                                             // test::TestPath::Unknown
271                                             TestType::Unknown => {
272                                                 cx.expr_path(test_type_path("Unknown"))
273                                             }
274                                         },
275                                     ),
276                                     // },
277                                 ],
278                             ),
279                         ),
280                         // testfn: test::StaticTestFn(...) | test::StaticBenchFn(...)
281                         field("testfn", test_fn), // }
282                     ],
283                 ), // }
284             ),
285         ),
286     );
287     test_const = test_const.map(|mut tc| {
288         tc.vis.node = ast::VisibilityKind::Public;
289         tc
290     });
291
292     // extern crate test
293     let test_extern = cx.item(sp, test_id, vec![], ast::ItemKind::ExternCrate(None));
294
295     log::debug!("synthetic test item:\n{}\n", pprust::item_to_string(&test_const));
296
297     vec![
298         // Access to libtest under a hygienic name
299         Annotatable::Item(test_extern),
300         // The generated test case
301         Annotatable::Item(test_const),
302         // The original item
303         Annotatable::Item(item),
304     ]
305 }
306
307 fn item_path(mod_path: &[Ident], item_ident: &Ident) -> String {
308     mod_path
309         .iter()
310         .chain(iter::once(item_ident))
311         .map(|x| x.to_string())
312         .collect::<Vec<String>>()
313         .join("::")
314 }
315
316 enum ShouldPanic {
317     No,
318     Yes(Option<Symbol>),
319 }
320
321 fn should_ignore(i: &ast::Item) -> bool {
322     attr::contains_name(&i.attrs, sym::ignore)
323 }
324
325 fn should_fail(i: &ast::Item) -> bool {
326     attr::contains_name(&i.attrs, sym::allow_fail)
327 }
328
329 fn should_panic(cx: &ExtCtxt<'_>, i: &ast::Item) -> ShouldPanic {
330     match attr::find_by_name(&i.attrs, sym::should_panic) {
331         Some(attr) => {
332             let sd = &cx.parse_sess.span_diagnostic;
333
334             match attr.meta_item_list() {
335                 // Handle #[should_panic(expected = "foo")]
336                 Some(list) => {
337                     let msg = list
338                         .iter()
339                         .find(|mi| mi.check_name(sym::expected))
340                         .and_then(|mi| mi.meta_item())
341                         .and_then(|mi| mi.value_str());
342                     if list.len() != 1 || msg.is_none() {
343                         sd.struct_span_warn(
344                             attr.span,
345                             "argument must be of the form: \
346                              `expected = \"error message\"`",
347                         )
348                         .note(
349                             "errors in this attribute were erroneously \
350                                 allowed and will become a hard error in a \
351                                 future release.",
352                         )
353                         .emit();
354                         ShouldPanic::Yes(None)
355                     } else {
356                         ShouldPanic::Yes(msg)
357                     }
358                 }
359                 // Handle #[should_panic] and #[should_panic = "expected"]
360                 None => ShouldPanic::Yes(attr.value_str()),
361             }
362         }
363         None => ShouldPanic::No,
364     }
365 }
366
367 enum TestType {
368     UnitTest,
369     IntegrationTest,
370     Unknown,
371 }
372
373 /// Attempts to determine the type of test.
374 /// Since doctests are created without macro expanding, only possible variants here
375 /// are `UnitTest`, `IntegrationTest` or `Unknown`.
376 fn test_type(cx: &ExtCtxt<'_>) -> TestType {
377     // Root path from context contains the topmost sources directory of the crate.
378     // I.e., for `project` with sources in `src` and tests in `tests` folders
379     // (no matter how many nested folders lie inside),
380     // there will be two different root paths: `/project/src` and `/project/tests`.
381     let crate_path = cx.root_path.as_path();
382
383     if crate_path.ends_with("src") {
384         // `/src` folder contains unit-tests.
385         TestType::UnitTest
386     } else if crate_path.ends_with("tests") {
387         // `/tests` folder contains integration tests.
388         TestType::IntegrationTest
389     } else {
390         // Crate layout doesn't match expected one, test type is unknown.
391         TestType::Unknown
392     }
393 }
394
395 fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
396     let has_should_panic_attr = attr::contains_name(&i.attrs, sym::should_panic);
397     let sd = &cx.parse_sess.span_diagnostic;
398     if let ast::ItemKind::Fn(_, ref sig, ref generics, _) = i.kind {
399         if let ast::Unsafe::Yes(span) = sig.header.unsafety {
400             sd.struct_span_err(i.span, "unsafe functions cannot be used for tests")
401                 .span_label(span, "`unsafe` because of this")
402                 .emit();
403             return false;
404         }
405         if let ast::Async::Yes { span, .. } = sig.header.asyncness {
406             sd.struct_span_err(i.span, "async functions cannot be used for tests")
407                 .span_label(span, "`async` because of this")
408                 .emit();
409             return false;
410         }
411
412         // If the termination trait is active, the compiler will check that the output
413         // type implements the `Termination` trait as `libtest` enforces that.
414         let has_output = match sig.decl.output {
415             ast::FnRetTy::Default(..) => false,
416             ast::FnRetTy::Ty(ref t) if t.kind.is_unit() => false,
417             _ => true,
418         };
419
420         if !sig.decl.inputs.is_empty() {
421             sd.span_err(i.span, "functions used as tests can not have any arguments");
422             return false;
423         }
424
425         match (has_output, has_should_panic_attr) {
426             (true, true) => {
427                 sd.span_err(i.span, "functions using `#[should_panic]` must return `()`");
428                 false
429             }
430             (true, false) => {
431                 if !generics.params.is_empty() {
432                     sd.span_err(i.span, "functions used as tests must have signature fn() -> ()");
433                     false
434                 } else {
435                     true
436                 }
437             }
438             (false, _) => true,
439         }
440     } else {
441         sd.span_err(i.span, "only functions may be used as tests");
442         false
443     }
444 }
445
446 fn has_bench_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
447     let has_sig = if let ast::ItemKind::Fn(_, ref sig, _, _) = i.kind {
448         // N.B., inadequate check, but we're running
449         // well before resolve, can't get too deep.
450         sig.decl.inputs.len() == 1
451     } else {
452         false
453     };
454
455     if !has_sig {
456         cx.parse_sess.span_diagnostic.span_err(
457             i.span,
458             "functions used as benches must have \
459             signature `fn(&mut Bencher) -> impl Termination`",
460         );
461     }
462
463     has_sig
464 }