]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/test.rs
Rollup merge of #64472 - Mark-Simulacrum:unused-parens-attr, r=varkor
[rust.git] / src / libsyntax_ext / 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
4 use syntax::ast;
5 use syntax::attr::{self, check_builtin_macro_attribute};
6 use syntax::ext::base::*;
7 use syntax::print::pprust;
8 use syntax::source_map::respan;
9 use syntax::symbol::{Symbol, sym};
10 use syntax_pos::Span;
11
12 use std::iter;
13
14 // #[test_case] is used by custom test authors to mark tests
15 // When building for test, it needs to make the item public and gensym the name
16 // Otherwise, we'll omit the item. This behavior means that any item annotated
17 // with #[test_case] is never addressable.
18 //
19 // We mark item with an inert attribute "rustc_test_marker" which the test generation
20 // logic will pick up on.
21 pub fn expand_test_case(
22     ecx: &mut ExtCtxt<'_>,
23     attr_sp: Span,
24     meta_item: &ast::MetaItem,
25     anno_item: Annotatable
26 ) -> Vec<Annotatable> {
27     check_builtin_macro_attribute(ecx, meta_item, sym::test_case);
28
29     if !ecx.ecfg.should_test { return vec![]; }
30
31     let sp = ecx.with_def_site_ctxt(attr_sp);
32     let mut item = anno_item.expect_item();
33     item = item.map(|mut item| {
34         item.vis = respan(item.vis.span, ast::VisibilityKind::Public);
35         item.ident.span = item.ident.span.with_ctxt(sp.ctxt());
36         item.attrs.push(
37             ecx.attribute(ecx.meta_word(sp, sym::rustc_test_marker))
38         );
39         item
40     });
41
42     return vec![Annotatable::Item(item)]
43 }
44
45 pub fn expand_test(
46     cx: &mut ExtCtxt<'_>,
47     attr_sp: Span,
48     meta_item: &ast::MetaItem,
49     item: Annotatable,
50 ) -> Vec<Annotatable> {
51     check_builtin_macro_attribute(cx, meta_item, sym::test);
52     expand_test_or_bench(cx, attr_sp, item, false)
53 }
54
55 pub fn expand_bench(
56     cx: &mut ExtCtxt<'_>,
57     attr_sp: Span,
58     meta_item: &ast::MetaItem,
59     item: Annotatable,
60 ) -> Vec<Annotatable> {
61     check_builtin_macro_attribute(cx, meta_item, sym::bench);
62     expand_test_or_bench(cx, attr_sp, item, true)
63 }
64
65 pub fn expand_test_or_bench(
66     cx: &mut ExtCtxt<'_>,
67     attr_sp: Span,
68     item: Annotatable,
69     is_bench: bool
70 ) -> Vec<Annotatable> {
71     // If we're not in test configuration, remove the annotated item
72     if !cx.ecfg.should_test { return vec![]; }
73
74     let item =
75         if let Annotatable::Item(i) = item { i }
76         else {
77             cx.parse_sess.span_diagnostic.span_fatal(item.span(),
78                 "`#[test]` attribute is only allowed on non associated functions").raise();
79         };
80
81     if let ast::ItemKind::Mac(_) = item.node {
82         cx.parse_sess.span_diagnostic.span_warn(item.span,
83             "`#[test]` attribute should not be used on macros. Use `#[cfg(test)]` instead.");
84         return vec![Annotatable::Item(item)];
85     }
86
87     // has_*_signature will report any errors in the type so compilation
88     // will fail. We shouldn't try to expand in this case because the errors
89     // would be spurious.
90     if (!is_bench && !has_test_signature(cx, &item)) ||
91         (is_bench && !has_bench_signature(cx, &item)) {
92         return vec![Annotatable::Item(item)];
93     }
94
95     let (sp, attr_sp) = (cx.with_def_site_ctxt(item.span), cx.with_def_site_ctxt(attr_sp));
96
97     let test_id = ast::Ident::new(sym::test, attr_sp);
98
99     // creates test::$name
100     let test_path = |name| {
101         cx.path(sp, vec![test_id, cx.ident_of(name, sp)])
102     };
103
104     // creates test::ShouldPanic::$name
105     let should_panic_path = |name| {
106         cx.path(sp, vec![test_id, cx.ident_of("ShouldPanic", sp), cx.ident_of(name, sp)])
107     };
108
109     // creates $name: $expr
110     let field = |name, expr| cx.field_imm(sp, cx.ident_of(name, sp), expr);
111
112     let test_fn = if is_bench {
113         // A simple ident for a lambda
114         let b = cx.ident_of("b", attr_sp);
115
116         cx.expr_call(sp, cx.expr_path(test_path("StaticBenchFn")), vec![
117             // |b| self::test::assert_test_result(
118             cx.lambda1(sp,
119                 cx.expr_call(sp, cx.expr_path(test_path("assert_test_result")), vec![
120                     // super::$test_fn(b)
121                     cx.expr_call(sp,
122                         cx.expr_path(cx.path(sp, vec![item.ident])),
123                         vec![cx.expr_ident(sp, b)])
124                 ]),
125                 b
126             )
127             // )
128         ])
129     } else {
130         cx.expr_call(sp, cx.expr_path(test_path("StaticTestFn")), vec![
131             // || {
132             cx.lambda0(sp,
133                 // test::assert_test_result(
134                 cx.expr_call(sp, cx.expr_path(test_path("assert_test_result")), vec![
135                     // $test_fn()
136                     cx.expr_call(sp, cx.expr_path(cx.path(sp, vec![item.ident])), vec![])
137                 // )
138                 ])
139             // }
140             )
141         // )
142         ])
143     };
144
145     let mut test_const = cx.item(sp, ast::Ident::new(item.ident.name, sp),
146         vec![
147             // #[cfg(test)]
148             cx.attribute(cx.meta_list(attr_sp, sym::cfg, vec![
149                 cx.meta_list_item_word(attr_sp, sym::test)
150             ])),
151             // #[rustc_test_marker]
152             cx.attribute(cx.meta_word(attr_sp, sym::rustc_test_marker)),
153         ],
154         // const $ident: test::TestDescAndFn =
155         ast::ItemKind::Const(cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
156             // test::TestDescAndFn {
157             cx.expr_struct(sp, test_path("TestDescAndFn"), vec![
158                 // desc: test::TestDesc {
159                 field("desc", cx.expr_struct(sp, test_path("TestDesc"), vec![
160                     // name: "path::to::test"
161                     field("name", cx.expr_call(sp, cx.expr_path(test_path("StaticTestName")),
162                         vec![
163                             cx.expr_str(sp, Symbol::intern(&item_path(
164                                 // skip the name of the root module
165                                 &cx.current_expansion.module.mod_path[1..],
166                                 &item.ident
167                             )))
168                         ])),
169                     // ignore: true | false
170                     field("ignore", cx.expr_bool(sp, should_ignore(&item))),
171                     // allow_fail: true | false
172                     field("allow_fail", cx.expr_bool(sp, should_fail(&item))),
173                     // should_panic: ...
174                     field("should_panic", match should_panic(cx, &item) {
175                         // test::ShouldPanic::No
176                         ShouldPanic::No => cx.expr_path(should_panic_path("No")),
177                         // test::ShouldPanic::Yes
178                         ShouldPanic::Yes(None) => cx.expr_path(should_panic_path("Yes")),
179                         // test::ShouldPanic::YesWithMessage("...")
180                         ShouldPanic::Yes(Some(sym)) => cx.expr_call(sp,
181                             cx.expr_path(should_panic_path("YesWithMessage")),
182                             vec![cx.expr_str(sp, sym)]),
183                     }),
184                 // },
185                 ])),
186                 // testfn: test::StaticTestFn(...) | test::StaticBenchFn(...)
187                 field("testfn", test_fn)
188             // }
189             ])
190         // }
191         ));
192     test_const = test_const.map(|mut tc| { tc.vis.node = ast::VisibilityKind::Public; tc});
193
194     // extern crate test
195     let test_extern = cx.item(sp,
196         test_id,
197         vec![],
198         ast::ItemKind::ExternCrate(None)
199     );
200
201     log::debug!("synthetic test item:\n{}\n", pprust::item_to_string(&test_const));
202
203     vec![
204         // Access to libtest under a hygienic name
205         Annotatable::Item(test_extern),
206         // The generated test case
207         Annotatable::Item(test_const),
208         // The original item
209         Annotatable::Item(item)
210     ]
211 }
212
213 fn item_path(mod_path: &[ast::Ident], item_ident: &ast::Ident) -> String {
214     mod_path.iter().chain(iter::once(item_ident))
215         .map(|x| x.to_string()).collect::<Vec<String>>().join("::")
216 }
217
218 enum ShouldPanic {
219     No,
220     Yes(Option<Symbol>),
221 }
222
223 fn should_ignore(i: &ast::Item) -> bool {
224     attr::contains_name(&i.attrs, sym::ignore)
225 }
226
227 fn should_fail(i: &ast::Item) -> bool {
228     attr::contains_name(&i.attrs, sym::allow_fail)
229 }
230
231 fn should_panic(cx: &ExtCtxt<'_>, i: &ast::Item) -> ShouldPanic {
232     match attr::find_by_name(&i.attrs, sym::should_panic) {
233         Some(attr) => {
234             let ref sd = cx.parse_sess.span_diagnostic;
235
236             match attr.meta_item_list() {
237                 // Handle #[should_panic(expected = "foo")]
238                 Some(list) => {
239                     let msg = list.iter()
240                         .find(|mi| mi.check_name(sym::expected))
241                         .and_then(|mi| mi.meta_item())
242                         .and_then(|mi| mi.value_str());
243                     if list.len() != 1 || msg.is_none() {
244                         sd.struct_span_warn(
245                             attr.span,
246                             "argument must be of the form: \
247                              `expected = \"error message\"`"
248                         ).note("Errors in this attribute were erroneously \
249                                 allowed and will become a hard error in a \
250                                 future release.").emit();
251                         ShouldPanic::Yes(None)
252                     } else {
253                         ShouldPanic::Yes(msg)
254                     }
255                 },
256                 // Handle #[should_panic] and #[should_panic = "expected"]
257                 None => ShouldPanic::Yes(attr.value_str())
258             }
259         }
260         None => ShouldPanic::No,
261     }
262 }
263
264 fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
265     let has_should_panic_attr = attr::contains_name(&i.attrs, sym::should_panic);
266     let ref sd = cx.parse_sess.span_diagnostic;
267     if let ast::ItemKind::Fn(ref decl, ref header, ref generics, _) = i.node {
268         if header.unsafety == ast::Unsafety::Unsafe {
269             sd.span_err(
270                 i.span,
271                 "unsafe functions cannot be used for tests"
272             );
273             return false
274         }
275         if header.asyncness.node.is_async() {
276             sd.span_err(
277                 i.span,
278                 "async functions cannot be used for tests"
279             );
280             return false
281         }
282
283
284         // If the termination trait is active, the compiler will check that the output
285         // type implements the `Termination` trait as `libtest` enforces that.
286         let has_output = match decl.output {
287             ast::FunctionRetTy::Default(..) => false,
288             ast::FunctionRetTy::Ty(ref t) if t.node.is_unit() => false,
289             _ => true
290         };
291
292         if !decl.inputs.is_empty() {
293             sd.span_err(i.span, "functions used as tests can not have any arguments");
294             return false;
295         }
296
297         match (has_output, has_should_panic_attr) {
298             (true, true) => {
299                 sd.span_err(i.span, "functions using `#[should_panic]` must return `()`");
300                 false
301             },
302             (true, false) => if !generics.params.is_empty() {
303                 sd.span_err(i.span,
304                                 "functions used as tests must have signature fn() -> ()");
305                 false
306             } else {
307                 true
308             },
309             (false, _) => true
310         }
311     } else {
312         sd.span_err(i.span, "only functions may be used as tests");
313         false
314     }
315 }
316
317 fn has_bench_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
318     let has_sig = if let ast::ItemKind::Fn(ref decl, _, _, _) = i.node {
319         // N.B., inadequate check, but we're running
320         // well before resolve, can't get too deep.
321         decl.inputs.len() == 1
322     } else {
323         false
324     };
325
326     if !has_sig {
327         cx.parse_sess.span_diagnostic.span_err(i.span, "functions used as benches must have \
328             signature `fn(&mut Bencher) -> impl Termination`");
329     }
330
331     has_sig
332 }