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};
7 use rustc_ast_pretty::pprust;
8 use rustc_errors::Applicability;
9 use rustc_expand::base::*;
10 use rustc_session::Session;
11 use rustc_span::symbol::{sym, Ident, Symbol};
14 use thin_vec::thin_vec;
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.
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<'_>,
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);
32 if !ecx.ecfg.should_test {
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 let test_path_symbol = Symbol::intern(&item_path(
40 // skip the name of the root module
41 &ecx.current_expansion.module.mod_path[1..],
44 item.vis = ast::Visibility {
46 kind: ast::VisibilityKind::Public,
49 item.ident.span = item.ident.span.with_ctxt(sp.ctxt());
50 item.attrs.push(ecx.attribute(attr::mk_name_value_item_str(
51 Ident::new(sym::rustc_test_marker, sp),
58 return vec![Annotatable::Item(item)];
64 meta_item: &ast::MetaItem,
66 ) -> Vec<Annotatable> {
67 check_builtin_macro_attribute(cx, meta_item, sym::test);
68 warn_on_duplicate_attribute(&cx, &item, sym::test);
69 expand_test_or_bench(cx, attr_sp, item, false)
75 meta_item: &ast::MetaItem,
77 ) -> Vec<Annotatable> {
78 check_builtin_macro_attribute(cx, meta_item, sym::bench);
79 warn_on_duplicate_attribute(&cx, &item, sym::bench);
80 expand_test_or_bench(cx, attr_sp, item, true)
83 pub fn expand_test_or_bench(
88 ) -> Vec<Annotatable> {
89 // If we're not in test configuration, remove the annotated item
90 if !cx.ecfg.should_test {
94 let (item, is_stmt) = match item {
95 Annotatable::Item(i) => (i, false),
96 Annotatable::Stmt(stmt) if matches!(stmt.kind, ast::StmtKind::Item(_)) => {
97 // FIXME: Use an 'if let' guard once they are implemented
98 if let ast::StmtKind::Item(i) = stmt.into_inner().kind {
107 "`#[test]` attribute is only allowed on non associated functions",
114 // Note: non-associated fn items are already handled by `expand_test_or_bench`
115 if !matches!(item.kind, ast::ItemKind::Fn(_)) {
116 let diag = &cx.sess.parse_sess.span_diagnostic;
117 let msg = "the `#[test]` attribute may only be used on a non-associated function";
118 let mut err = match item.kind {
119 // These were a warning before #92959 and need to continue being that to avoid breaking
120 // stable user code (#94508).
121 ast::ItemKind::MacCall(_) => diag.struct_span_warn(attr_sp, msg),
122 // `.forget_guarantee()` needed to get these two arms to match types. Because of how
123 // locally close the `.emit()` call is I'm comfortable with it, but if it can be
124 // reworked in the future to not need it, it'd be nice.
125 _ => diag.struct_span_err(attr_sp, msg).forget_guarantee(),
127 err.span_label(attr_sp, "the `#[test]` macro causes a function to be run on a test and has no effect on non-functions")
128 .span_label(item.span, format!("expected a non-associated function, found {} {}", item.kind.article(), item.kind.descr()))
129 .span_suggestion(attr_sp, "replace with conditional compilation to make the item only exist when tests are being run", "#[cfg(test)]", Applicability::MaybeIncorrect)
132 return vec![Annotatable::Item(item)];
135 // has_*_signature will report any errors in the type so compilation
136 // will fail. We shouldn't try to expand in this case because the errors
137 // would be spurious.
138 if (!is_bench && !has_test_signature(cx, &item))
139 || (is_bench && !has_bench_signature(cx, &item))
141 return vec![Annotatable::Item(item)];
144 let (sp, attr_sp) = (cx.with_def_site_ctxt(item.span), cx.with_def_site_ctxt(attr_sp));
146 let test_id = Ident::new(sym::test, attr_sp);
148 // creates test::$name
149 let test_path = |name| cx.path(sp, vec![test_id, Ident::from_str_and_span(name, sp)]);
151 // creates test::ShouldPanic::$name
152 let should_panic_path = |name| {
157 Ident::from_str_and_span("ShouldPanic", sp),
158 Ident::from_str_and_span(name, sp),
163 // creates test::TestType::$name
164 let test_type_path = |name| {
169 Ident::from_str_and_span("TestType", sp),
170 Ident::from_str_and_span(name, sp),
175 // creates $name: $expr
176 let field = |name, expr| cx.field_imm(sp, Ident::from_str_and_span(name, sp), expr);
178 let test_fn = if is_bench {
179 // A simple ident for a lambda
180 let b = Ident::from_str_and_span("b", attr_sp);
184 cx.expr_path(test_path("StaticBenchFn")),
186 // |b| self::test::assert_test_result(
191 cx.expr_path(test_path("assert_test_result")),
193 // super::$test_fn(b)
196 cx.expr_path(cx.path(sp, vec![item.ident])),
197 vec![cx.expr_ident(sp, b)],
208 cx.expr_path(test_path("StaticTestFn")),
213 // test::assert_test_result(
216 cx.expr_path(test_path("assert_test_result")),
219 cx.expr_call(sp, cx.expr_path(cx.path(sp, vec![item.ident])), vec![]), // )
227 let test_path_symbol = Symbol::intern(&item_path(
228 // skip the name of the root module
229 &cx.current_expansion.module.mod_path[1..],
233 let mut test_const = cx.item(
235 Ident::new(item.ident.name, sp),
238 cx.attribute(attr::mk_list_item(
239 Ident::new(sym::cfg, attr_sp),
240 vec![attr::mk_nested_word_item(Ident::new(sym::test, attr_sp))],
242 // #[rustc_test_marker = "test_case_sort_key"]
243 cx.attribute(attr::mk_name_value_item_str(
244 Ident::new(sym::rustc_test_marker, attr_sp),
250 // const $ident: test::TestDescAndFn =
251 ast::ItemKind::Const(
252 ast::Defaultness::Final,
253 cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
254 // test::TestDescAndFn {
258 test_path("TestDescAndFn"),
260 // desc: test::TestDesc {
265 test_path("TestDesc"),
267 // name: "path::to::test"
272 cx.expr_path(test_path("StaticTestName")),
273 vec![cx.expr_str(sp, test_path_symbol)],
276 // ignore: true | false
279 cx.expr_bool(sp, should_ignore(&cx.sess, &item)),
281 // ignore_message: Some("...") | None
284 if let Some(msg) = should_ignore_message(cx, &item) {
285 cx.expr_some(sp, cx.expr_str(sp, msg))
290 // compile_fail: true | false
291 field("compile_fail", cx.expr_bool(sp, false)),
292 // no_run: true | false
293 field("no_run", cx.expr_bool(sp, false)),
297 match should_panic(cx, &item) {
298 // test::ShouldPanic::No
300 cx.expr_path(should_panic_path("No"))
302 // test::ShouldPanic::Yes
303 ShouldPanic::Yes(None) => {
304 cx.expr_path(should_panic_path("Yes"))
306 // test::ShouldPanic::YesWithMessage("...")
307 ShouldPanic::Yes(Some(sym)) => cx.expr_call(
309 cx.expr_path(should_panic_path("YesWithMessage")),
310 vec![cx.expr_str(sp, sym)],
317 match test_type(cx) {
318 // test::TestType::UnitTest
319 TestType::UnitTest => {
320 cx.expr_path(test_type_path("UnitTest"))
322 // test::TestType::IntegrationTest
323 TestType::IntegrationTest => {
324 cx.expr_path(test_type_path("IntegrationTest"))
326 // test::TestPath::Unknown
327 TestType::Unknown => {
328 cx.expr_path(test_type_path("Unknown"))
336 // testfn: test::StaticTestFn(...) | test::StaticBenchFn(...)
337 field("testfn", test_fn), // }
343 test_const = test_const.map(|mut tc| {
344 tc.vis.kind = ast::VisibilityKind::Public;
349 let test_extern = cx.item(sp, test_id, ast::AttrVec::new(), ast::ItemKind::ExternCrate(None));
351 debug!("synthetic test item:\n{}\n", pprust::item_to_string(&test_const));
355 // Access to libtest under a hygienic name
356 Annotatable::Stmt(P(cx.stmt_item(sp, test_extern))),
357 // The generated test case
358 Annotatable::Stmt(P(cx.stmt_item(sp, test_const))),
360 Annotatable::Stmt(P(cx.stmt_item(sp, item))),
364 // Access to libtest under a hygienic name
365 Annotatable::Item(test_extern),
366 // The generated test case
367 Annotatable::Item(test_const),
369 Annotatable::Item(item),
374 fn item_path(mod_path: &[Ident], item_ident: &Ident) -> String {
377 .chain(iter::once(item_ident))
378 .map(|x| x.to_string())
379 .collect::<Vec<String>>()
388 fn should_ignore(sess: &Session, i: &ast::Item) -> bool {
389 sess.contains_name(&i.attrs, sym::ignore)
392 fn should_ignore_message(cx: &ExtCtxt<'_>, i: &ast::Item) -> Option<Symbol> {
393 match cx.sess.find_by_name(&i.attrs, sym::ignore) {
395 match attr.meta_item_list() {
396 // Handle #[ignore(bar = "foo")]
398 // Handle #[ignore] and #[ignore = "message"]
399 None => attr.value_str(),
406 fn should_panic(cx: &ExtCtxt<'_>, i: &ast::Item) -> ShouldPanic {
407 match cx.sess.find_by_name(&i.attrs, sym::should_panic) {
409 let sd = &cx.sess.parse_sess.span_diagnostic;
411 match attr.meta_item_list() {
412 // Handle #[should_panic(expected = "foo")]
416 .find(|mi| mi.has_name(sym::expected))
417 .and_then(|mi| mi.meta_item())
418 .and_then(|mi| mi.value_str());
419 if list.len() != 1 || msg.is_none() {
422 "argument must be of the form: \
423 `expected = \"error message\"`",
426 "errors in this attribute were erroneously \
427 allowed and will become a hard error in a \
431 ShouldPanic::Yes(None)
433 ShouldPanic::Yes(msg)
436 // Handle #[should_panic] and #[should_panic = "expected"]
437 None => ShouldPanic::Yes(attr.value_str()),
440 None => ShouldPanic::No,
450 /// Attempts to determine the type of test.
451 /// Since doctests are created without macro expanding, only possible variants here
452 /// are `UnitTest`, `IntegrationTest` or `Unknown`.
453 fn test_type(cx: &ExtCtxt<'_>) -> TestType {
454 // Root path from context contains the topmost sources directory of the crate.
455 // I.e., for `project` with sources in `src` and tests in `tests` folders
456 // (no matter how many nested folders lie inside),
457 // there will be two different root paths: `/project/src` and `/project/tests`.
458 let crate_path = cx.root_path.as_path();
460 if crate_path.ends_with("src") {
461 // `/src` folder contains unit-tests.
463 } else if crate_path.ends_with("tests") {
464 // `/tests` folder contains integration tests.
465 TestType::IntegrationTest
467 // Crate layout doesn't match expected one, test type is unknown.
472 fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
473 let has_should_panic_attr = cx.sess.contains_name(&i.attrs, sym::should_panic);
474 let sd = &cx.sess.parse_sess.span_diagnostic;
475 if let ast::ItemKind::Fn(box ast::Fn { ref sig, ref generics, .. }) = i.kind {
476 if let ast::Unsafe::Yes(span) = sig.header.unsafety {
477 sd.struct_span_err(i.span, "unsafe functions cannot be used for tests")
478 .span_label(span, "`unsafe` because of this")
482 if let ast::Async::Yes { span, .. } = sig.header.asyncness {
483 sd.struct_span_err(i.span, "async functions cannot be used for tests")
484 .span_label(span, "`async` because of this")
489 // If the termination trait is active, the compiler will check that the output
490 // type implements the `Termination` trait as `libtest` enforces that.
491 let has_output = match sig.decl.output {
492 ast::FnRetTy::Default(..) => false,
493 ast::FnRetTy::Ty(ref t) if t.kind.is_unit() => false,
497 if !sig.decl.inputs.is_empty() {
498 sd.span_err(i.span, "functions used as tests can not have any arguments");
502 match (has_output, has_should_panic_attr) {
504 sd.span_err(i.span, "functions using `#[should_panic]` must return `()`");
508 if !generics.params.is_empty() {
509 sd.span_err(i.span, "functions used as tests must have signature fn() -> ()");
518 // should be unreachable because `is_test_fn_item` should catch all non-fn items
523 fn has_bench_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
524 let has_sig = if let ast::ItemKind::Fn(box ast::Fn { ref sig, .. }) = i.kind {
525 // N.B., inadequate check, but we're running
526 // well before resolve, can't get too deep.
527 sig.decl.inputs.len() == 1
533 cx.sess.parse_sess.span_diagnostic.span_err(
535 "functions used as benches must have \
536 signature `fn(&mut Bencher) -> impl Termination`",