use crate::util::check_builtin_macro_attribute;
use rustc_ast as ast;
use rustc_ast::attr;
use rustc_ast_pretty::pprust;
use rustc_expand::base::*;
use rustc_session::Session;
use rustc_span::symbol::{sym, Ident, Symbol};
use rustc_span::Span;
use std::iter;
pub fn expand_test_case(
ecx: &mut ExtCtxt<'_>,
attr_sp: Span,
meta_item: &ast::MetaItem,
anno_item: Annotatable,
) -> Vec<Annotatable> {
check_builtin_macro_attribute(ecx, meta_item, sym::test_case);
if !ecx.ecfg.should_test {
return vec![];
}
let sp = ecx.with_def_site_ctxt(attr_sp);
let mut item = anno_item.expect_item();
item = item.map(|mut item| {
item.vis = ast::Visibility {
span: item.vis.span,
kind: ast::VisibilityKind::Public,
tokens: None,
};
item.ident.span = item.ident.span.with_ctxt(sp.ctxt());
item.attrs.push(ecx.attribute(ecx.meta_word(sp, sym::rustc_test_marker)));
item
});
return vec![Annotatable::Item(item)];
}
pub fn expand_test(
cx: &mut ExtCtxt<'_>,
attr_sp: Span,
meta_item: &ast::MetaItem,
item: Annotatable,
) -> Vec<Annotatable> {
check_builtin_macro_attribute(cx, meta_item, sym::test);
expand_test_or_bench(cx, attr_sp, item, false)
}
pub fn expand_bench(
cx: &mut ExtCtxt<'_>,
attr_sp: Span,
meta_item: &ast::MetaItem,
item: Annotatable,
) -> Vec<Annotatable> {
check_builtin_macro_attribute(cx, meta_item, sym::bench);
expand_test_or_bench(cx, attr_sp, item, true)
}
pub fn expand_test_or_bench(
cx: &mut ExtCtxt<'_>,
attr_sp: Span,
item: Annotatable,
is_bench: bool,
) -> Vec<Annotatable> {
if !cx.ecfg.should_test {
return vec![];
}
let item = match item {
Annotatable::Item(i) => i,
other => {
cx.struct_span_err(
other.span(),
"`#[test]` attribute is only allowed on non associated functions",
)
.emit();
return vec![other];
}
};
if let ast::ItemKind::MacCall(_) = item.kind {
cx.sess.parse_sess.span_diagnostic.span_warn(
item.span,
"`#[test]` attribute should not be used on macros. Use `#[cfg(test)]` instead.",
);
return vec![Annotatable::Item(item)];
}
if (!is_bench && !has_test_signature(cx, &item))
|| (is_bench && !has_bench_signature(cx, &item))
{
return vec![Annotatable::Item(item)];
}
let (sp, attr_sp) = (cx.with_def_site_ctxt(item.span), cx.with_def_site_ctxt(attr_sp));
let test_id = Ident::new(sym::test, attr_sp);
let test_path = |name| cx.path(sp, vec![test_id, Ident::from_str_and_span(name, sp)]);
let should_panic_path = |name| {
cx.path(
sp,
vec![
test_id,
Ident::from_str_and_span("ShouldPanic", sp),
Ident::from_str_and_span(name, sp),
],
)
};
let test_type_path = |name| {
cx.path(
sp,
vec![
test_id,
Ident::from_str_and_span("TestType", sp),
Ident::from_str_and_span(name, sp),
],
)
};
let field = |name, expr| cx.field_imm(sp, Ident::from_str_and_span(name, sp), expr);
let test_fn = if is_bench {
let b = Ident::from_str_and_span("b", attr_sp);
cx.expr_call(
sp,
cx.expr_path(test_path("StaticBenchFn")),
vec![
cx.lambda1(
sp,
cx.expr_call(
sp,
cx.expr_path(test_path("assert_test_result")),
vec![
cx.expr_call(
sp,
cx.expr_path(cx.path(sp, vec![item.ident])),
vec![cx.expr_ident(sp, b)],
),
],
),
b,
),
],
)
} else {
cx.expr_call(
sp,
cx.expr_path(test_path("StaticTestFn")),
vec![
cx.lambda0(
sp,
cx.expr_call(
sp,
cx.expr_path(test_path("assert_test_result")),
vec![
cx.expr_call(sp, cx.expr_path(cx.path(sp, vec![item.ident])), vec![]),
],
),
),
],
)
};
let mut test_const = cx.item(
sp,
Ident::new(item.ident.name, sp),
vec![
cx.attribute(attr::mk_list_item(
Ident::new(sym::cfg, attr_sp),
vec![attr::mk_nested_word_item(Ident::new(sym::test, attr_sp))],
)),
cx.attribute(cx.meta_word(attr_sp, sym::rustc_test_marker)),
],
ast::ItemKind::Const(
ast::Defaultness::Final,
cx.ty(sp, ast::TyKind::Path(None, test_path("TestDescAndFn"))),
Some(
cx.expr_struct(
sp,
test_path("TestDescAndFn"),
vec![
field(
"desc",
cx.expr_struct(
sp,
test_path("TestDesc"),
vec![
field(
"name",
cx.expr_call(
sp,
cx.expr_path(test_path("StaticTestName")),
vec![cx.expr_str(
sp,
Symbol::intern(&item_path(
&cx.current_expansion.module.mod_path[1..],
&item.ident,
)),
)],
),
),
field(
"ignore",
cx.expr_bool(sp, should_ignore(&cx.sess, &item)),
),
field(
"allow_fail",
cx.expr_bool(sp, should_fail(&cx.sess, &item)),
),
field(
"should_panic",
match should_panic(cx, &item) {
ShouldPanic::No => {
cx.expr_path(should_panic_path("No"))
}
ShouldPanic::Yes(None) => {
cx.expr_path(should_panic_path("Yes"))
}
ShouldPanic::Yes(Some(sym)) => cx.expr_call(
sp,
cx.expr_path(should_panic_path("YesWithMessage")),
vec![cx.expr_str(sp, sym)],
),
},
),
field(
"test_type",
match test_type(cx) {
TestType::UnitTest => {
cx.expr_path(test_type_path("UnitTest"))
}
TestType::IntegrationTest => {
cx.expr_path(test_type_path("IntegrationTest"))
}
TestType::Unknown => {
cx.expr_path(test_type_path("Unknown"))
}
},
),
],
),
),
field("testfn", test_fn),
],
),
),
),
);
test_const = test_const.map(|mut tc| {
tc.vis.kind = ast::VisibilityKind::Public;
tc
});
let test_extern = cx.item(sp, test_id, vec![], ast::ItemKind::ExternCrate(None));
tracing::debug!("synthetic test item:\n{}\n", pprust::item_to_string(&test_const));
vec![
Annotatable::Item(test_extern),
Annotatable::Item(test_const),
Annotatable::Item(item),
]
}
fn item_path(mod_path: &[Ident], item_ident: &Ident) -> String {
mod_path
.iter()
.chain(iter::once(item_ident))
.map(|x| x.to_string())
.collect::<Vec<String>>()
.join("::")
}
enum ShouldPanic {
No,
Yes(Option<Symbol>),
}
fn should_ignore(sess: &Session, i: &ast::Item) -> bool {
sess.contains_name(&i.attrs, sym::ignore)
}
fn should_fail(sess: &Session, i: &ast::Item) -> bool {
sess.contains_name(&i.attrs, sym::allow_fail)
}
fn should_panic(cx: &ExtCtxt<'_>, i: &ast::Item) -> ShouldPanic {
match cx.sess.find_by_name(&i.attrs, sym::should_panic) {
Some(attr) => {
let sd = &cx.sess.parse_sess.span_diagnostic;
match attr.meta_item_list() {
Some(list) => {
let msg = list
.iter()
.find(|mi| mi.has_name(sym::expected))
.and_then(|mi| mi.meta_item())
.and_then(|mi| mi.value_str());
if list.len() != 1 || msg.is_none() {
sd.struct_span_warn(
attr.span,
"argument must be of the form: \
`expected = \"error message\"`",
)
.note(
"errors in this attribute were erroneously \
allowed and will become a hard error in a \
future release.",
)
.emit();
ShouldPanic::Yes(None)
} else {
ShouldPanic::Yes(msg)
}
}
None => ShouldPanic::Yes(attr.value_str()),
}
}
None => ShouldPanic::No,
}
}
enum TestType {
UnitTest,
IntegrationTest,
Unknown,
}
fn test_type(cx: &ExtCtxt<'_>) -> TestType {
let crate_path = cx.root_path.as_path();
if crate_path.ends_with("src") {
TestType::UnitTest
} else if crate_path.ends_with("tests") {
TestType::IntegrationTest
} else {
TestType::Unknown
}
}
fn has_test_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
let has_should_panic_attr = cx.sess.contains_name(&i.attrs, sym::should_panic);
let sd = &cx.sess.parse_sess.span_diagnostic;
if let ast::ItemKind::Fn(_, ref sig, ref generics, _) = i.kind {
if let ast::Unsafe::Yes(span) = sig.header.unsafety {
sd.struct_span_err(i.span, "unsafe functions cannot be used for tests")
.span_label(span, "`unsafe` because of this")
.emit();
return false;
}
if let ast::Async::Yes { span, .. } = sig.header.asyncness {
sd.struct_span_err(i.span, "async functions cannot be used for tests")
.span_label(span, "`async` because of this")
.emit();
return false;
}
let has_output = match sig.decl.output {
ast::FnRetTy::Default(..) => false,
ast::FnRetTy::Ty(ref t) if t.kind.is_unit() => false,
_ => true,
};
if !sig.decl.inputs.is_empty() {
sd.span_err(i.span, "functions used as tests can not have any arguments");
return false;
}
match (has_output, has_should_panic_attr) {
(true, true) => {
sd.span_err(i.span, "functions using `#[should_panic]` must return `()`");
false
}
(true, false) => {
if !generics.params.is_empty() {
sd.span_err(i.span, "functions used as tests must have signature fn() -> ()");
false
} else {
true
}
}
(false, _) => true,
}
} else {
sd.span_err(i.span, "only functions may be used as tests");
false
}
}
fn has_bench_signature(cx: &ExtCtxt<'_>, i: &ast::Item) -> bool {
let has_sig = if let ast::ItemKind::Fn(_, ref sig, _, _) = i.kind {
sig.decl.inputs.len() == 1
} else {
false
};
if !has_sig {
cx.sess.parse_sess.span_diagnostic.span_err(
i.span,
"functions used as benches must have \
signature `fn(&mut Bencher) -> impl Termination`",
);
}
has_sig
}