Skip to main content

rustc_builtin_macros/
test_harness.rs

1// Code that generates a test runner to run all the tests in a crate
2
3use std::mem;
4use std::sync::atomic::Ordering;
5
6use rustc_ast as ast;
7use rustc_ast::attr::contains_name;
8use rustc_ast::entry::EntryPointType;
9use rustc_ast::mut_visit::*;
10use rustc_ast::visit::Visitor;
11use rustc_ast::{ModKind, attr};
12use rustc_attr_parsing::AttributeParser;
13use rustc_expand::base::{ExtCtxt, ResolverExpand};
14use rustc_expand::expand::{AstFragment, ExpansionConfig};
15use rustc_feature::Features;
16use rustc_hir::attrs::AttributeKind;
17use rustc_session::Session;
18use rustc_session::lint::builtin::UNNAMEABLE_TEST_ITEMS;
19use rustc_span::hygiene::{AstPass, SyntaxContext, Transparency};
20use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym};
21use rustc_target::spec::PanicStrategy;
22use smallvec::smallvec;
23use thin_vec::{ThinVec, thin_vec};
24use tracing::debug;
25
26use crate::diagnostics;
27
28#[derive(#[automatically_derived]
impl ::core::clone::Clone for Test {
    #[inline]
    fn clone(&self) -> Test {
        Test {
            span: ::core::clone::Clone::clone(&self.span),
            ident: ::core::clone::Clone::clone(&self.ident),
            name: ::core::clone::Clone::clone(&self.name),
        }
    }
}Clone)]
29struct Test {
30    span: Span,
31    ident: Ident,
32    name: Symbol,
33}
34
35struct TestCtxt<'a> {
36    ext_cx: ExtCtxt<'a>,
37    panic_strategy: PanicStrategy,
38    def_site: Span,
39    test_cases: Vec<Test>,
40    reexport_test_harness_main: Option<Symbol>,
41    test_runner: Option<ast::Path>,
42}
43
44/// Traverse the crate, collecting all the test functions, eliding any
45/// existing main functions, and synthesizing a main test harness
46pub fn inject(
47    krate: &mut ast::Crate,
48    sess: &Session,
49    features: &Features,
50    resolver: &mut dyn ResolverExpand,
51) {
52    let dcx = sess.dcx();
53    let panic_strategy = sess.panic_strategy();
54    let platform_panic_strategy = sess.target.panic_strategy;
55
56    // Check for #![reexport_test_harness_main = "some_name"] which gives the
57    // main test function the name `some_name` without hygiene. This needs to be
58    // unconditional, so that the attribute is still marked as used in
59    // non-test builds.
60    let reexport_test_harness_main =
61        attr::first_attr_value_str_by_name(&krate.attrs, sym::reexport_test_harness_main);
62
63    // Do this here so that the test_runner crate attribute gets marked as used
64    // even in non-test builds
65    let test_runner = get_test_runner(sess, krate);
66
67    if sess.is_test_crate() {
68        let panic_strategy = match (panic_strategy, sess.opts.unstable_opts.panic_abort_tests) {
69            (PanicStrategy::Abort | PanicStrategy::ImmediateAbort, true) => panic_strategy,
70            (PanicStrategy::Abort | PanicStrategy::ImmediateAbort, false) => {
71                if panic_strategy == platform_panic_strategy {
72                    // Silently allow compiling with panic=abort on these platforms,
73                    // but with old behavior (abort if a test fails).
74                } else {
75                    dcx.emit_err(diagnostics::TestsNotSupport {});
76                }
77                PanicStrategy::Unwind
78            }
79            (PanicStrategy::Unwind, _) => PanicStrategy::Unwind,
80        };
81        generate_test_harness(
82            sess,
83            resolver,
84            reexport_test_harness_main,
85            krate,
86            features,
87            panic_strategy,
88            test_runner,
89        )
90    }
91}
92
93struct TestHarnessGenerator<'a> {
94    cx: TestCtxt<'a>,
95    tests: Vec<Test>,
96}
97
98impl TestHarnessGenerator<'_> {
99    fn add_test_cases(&mut self, node_id: ast::NodeId, span: Span, prev_tests: Vec<Test>) {
100        let mut tests = mem::replace(&mut self.tests, prev_tests);
101
102        if !tests.is_empty() {
103            // Create an identifier that will hygienically resolve the test
104            // case name, even in another module.
105            let expn_id = self.cx.ext_cx.resolver.expansion_for_ast_pass(
106                span,
107                AstPass::TestHarness,
108                &[],
109                Some(node_id),
110            );
111            for test in &mut tests {
112                // See the comment on `mk_main` for why we're using
113                // `apply_mark` directly.
114                test.ident.span =
115                    test.ident.span.apply_mark(expn_id.to_expn_id(), Transparency::Opaque);
116            }
117            self.cx.test_cases.extend(tests);
118        }
119    }
120}
121
122impl<'a> MutVisitor for TestHarnessGenerator<'a> {
123    fn visit_crate(&mut self, c: &mut ast::Crate) {
124        let prev_tests = mem::take(&mut self.tests);
125        walk_crate(self, c);
126        self.add_test_cases(ast::CRATE_NODE_ID, c.spans.inner_span, prev_tests);
127
128        // Create a main function to run our tests
129        c.items.push(mk_main(&mut self.cx));
130    }
131
132    fn visit_item(&mut self, item: &mut ast::Item) {
133        if let Some(name) = get_test_name(&item) {
134            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_builtin_macros/src/test_harness.rs:134",
                        "rustc_builtin_macros::test_harness",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_builtin_macros/src/test_harness.rs"),
                        ::tracing_core::__macro_support::Option::Some(134u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_builtin_macros::test_harness"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("this is a test item")
                                            as &dyn Value))])
            });
    } else { ; }
};debug!("this is a test item");
135
136            // `unwrap` is ok because only functions, consts, and static should reach here.
137            let test = Test { span: item.span, ident: item.kind.ident().unwrap(), name };
138            self.tests.push(test);
139        }
140
141        // We don't want to recurse into anything other than mods, since
142        // mods or tests inside of functions will break things
143        if let ast::ItemKind::Mod(
144            _,
145            _,
146            ModKind::Loaded(.., ast::ModSpans { inner_span: span, .. }),
147        ) = item.kind
148        {
149            let prev_tests = mem::take(&mut self.tests);
150            ast::mut_visit::walk_item(self, item);
151            self.add_test_cases(item.id, span, prev_tests);
152        } else {
153            // But in those cases, we emit a lint to warn the user of these missing tests.
154            ast::visit::walk_item(&mut InnerItemLinter { sess: self.cx.ext_cx.sess }, &item);
155        }
156    }
157}
158
159struct InnerItemLinter<'a> {
160    sess: &'a Session,
161}
162
163impl<'a> Visitor<'a> for InnerItemLinter<'_> {
164    fn visit_item(&mut self, i: &'a ast::Item) {
165        if let Some(attr) = attr::find_by_name(&i.attrs, sym::rustc_test_marker) {
166            self.sess.psess.buffer_lint(
167                UNNAMEABLE_TEST_ITEMS,
168                attr.span,
169                i.id,
170                diagnostics::UnnameableTestItems,
171            );
172        }
173    }
174}
175
176fn entry_point_type(item: &ast::Item, at_root: bool) -> EntryPointType {
177    match &item.kind {
178        ast::ItemKind::Fn(fn_) => rustc_ast::entry::entry_point_type(
179            contains_name(&item.attrs, sym::rustc_main),
180            at_root,
181            Some(fn_.ident.name),
182        ),
183        _ => EntryPointType::None,
184    }
185}
186
187/// A folder used to remove any entry points (like fn main) because the harness
188/// coroutine will provide its own
189struct EntryPointCleaner<'a> {
190    // Current depth in the ast
191    sess: &'a Session,
192    depth: usize,
193    def_site: Span,
194}
195
196impl<'a> MutVisitor for EntryPointCleaner<'a> {
197    fn visit_item(&mut self, item: &mut ast::Item) {
198        self.depth += 1;
199        ast::mut_visit::walk_item(self, item);
200        self.depth -= 1;
201
202        // Remove any #[rustc_main] from the AST so it doesn't
203        // clash with the one we're going to add, but mark it as
204        // #[allow(dead_code)] to avoid printing warnings.
205        match entry_point_type(&item, self.depth == 0) {
206            EntryPointType::RustcMainAttr => {
207                let allow_dead_code = attr::mk_attr_nested_word(
208                    &self.sess.psess.attr_id_generator,
209                    ast::AttrStyle::Outer,
210                    ast::Safety::Default,
211                    sym::allow,
212                    sym::dead_code,
213                    self.def_site,
214                );
215                item.attrs.retain(|attr| !attr.has_name(sym::rustc_main));
216                item.attrs.push(allow_dead_code);
217                self.sess.removed_rustc_main_attr.store(true, Ordering::Relaxed);
218            }
219            EntryPointType::None | EntryPointType::MainNamed | EntryPointType::OtherMain => {}
220        };
221    }
222}
223
224/// Crawl over the crate, inserting test reexports and the test main function
225fn generate_test_harness(
226    sess: &Session,
227    resolver: &mut dyn ResolverExpand,
228    reexport_test_harness_main: Option<Symbol>,
229    krate: &mut ast::Crate,
230    features: &Features,
231    panic_strategy: PanicStrategy,
232    test_runner: Option<ast::Path>,
233) {
234    let econfig = ExpansionConfig::default(sym::test, features);
235    let ext_cx = ExtCtxt::new(sess, econfig, resolver, None);
236
237    let expn_id = ext_cx.resolver.expansion_for_ast_pass(
238        DUMMY_SP,
239        AstPass::TestHarness,
240        &[sym::test, sym::rustc_attrs, sym::coverage_attribute],
241        None,
242    );
243    let def_site = DUMMY_SP.with_def_site_ctxt(expn_id.to_expn_id());
244
245    // Remove the entry points
246    let mut cleaner = EntryPointCleaner { sess, depth: 0, def_site };
247    cleaner.visit_crate(krate);
248
249    let cx = TestCtxt {
250        ext_cx,
251        panic_strategy,
252        def_site,
253        test_cases: Vec::new(),
254        reexport_test_harness_main,
255        test_runner,
256    };
257
258    TestHarnessGenerator { cx, tests: Vec::new() }.visit_crate(krate);
259}
260
261/// Creates a function item for use as the main function of a test build.
262/// This function will call the `test_runner` as specified by the crate attribute
263///
264/// By default this expands to
265///
266/// ```ignore (messes with test internals)
267/// #[rustc_main]
268/// pub fn main() {
269///     extern crate test;
270///     test::test_main_static(&[
271///         &test_const1,
272///         &test_const2,
273///         &test_const3,
274///     ]);
275/// }
276/// ```
277///
278/// Most of the Ident have the usual def-site hygiene for the AST pass. The
279/// exception is the `test_const`s. These have a syntax context that has two
280/// opaque marks: one from the expansion of `test` or `test_case`, and one
281/// generated  in `TestHarnessGenerator::visit_item`. When resolving this
282/// identifier after failing to find a matching identifier in the root module
283/// we remove the outer mark, and try resolving at its def-site, which will
284/// then resolve to `test_const`.
285///
286/// The expansion here can be controlled by two attributes:
287///
288/// [`TestCtxt::reexport_test_harness_main`] provides a different name for the `main`
289/// function and [`TestCtxt::test_runner`] provides a path that replaces
290/// `test::test_main_static`.
291fn mk_main(cx: &mut TestCtxt<'_>) -> Box<ast::Item> {
292    let sp = cx.def_site;
293    let ecx = &cx.ext_cx;
294    let test_ident = Ident::new(sym::test, sp);
295
296    let runner_name =
297        if cx.panic_strategy.unwinds() { "test_main_static" } else { "test_main_static_abort" };
298
299    // test::test_main_static(...)
300    let mut test_runner = cx.test_runner.clone().unwrap_or_else(|| {
301        ecx.path(sp, ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [test_ident, Ident::from_str_and_span(runner_name, sp)]))vec![test_ident, Ident::from_str_and_span(runner_name, sp)])
302    });
303
304    test_runner.span = sp;
305
306    let test_main_path_expr = ecx.expr_path(test_runner);
307    let call_test_main = ecx.expr_call(sp, test_main_path_expr, {
    let len = [()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(mk_tests_slice(cx, sp));
    vec
}thin_vec![mk_tests_slice(cx, sp)]);
308    let call_test_main = ecx.stmt_expr(call_test_main);
309
310    // extern crate test
311    let test_extern_stmt = ecx.stmt_item(
312        sp,
313        ecx.item(sp, ast::AttrVec::new(), ast::ItemKind::ExternCrate(None, test_ident)),
314    );
315
316    // #[rustc_main]
317    let main_attr = ecx.attr_word(sym::rustc_main, sp);
318    // #[coverage(off)]
319    let coverage_attr = ecx.attr_nested_word(sym::coverage, sym::off, sp);
320    // #[doc(hidden)]
321    let doc_hidden_attr = ecx.attr_nested_word(sym::doc, sym::hidden, sp);
322
323    // pub fn main() { ... }
324    let main_ret_ty = ecx.ty(sp, ast::TyKind::Tup(ThinVec::new()));
325
326    // If no test runner is provided we need to import the test crate
327    let main_body = if cx.test_runner.is_none() {
328        ecx.block(sp, {
    let len = [(), ()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(test_extern_stmt);
    vec.push(call_test_main);
    vec
}thin_vec![test_extern_stmt, call_test_main])
329    } else {
330        ecx.block(sp, {
    let len = [()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(call_test_main);
    vec
}thin_vec![call_test_main])
331    };
332
333    let decl = ecx.fn_decl(ThinVec::new(), ast::FnRetTy::Ty(main_ret_ty));
334    let sig = ast::FnSig { decl, header: ast::FnHeader::default(), span: sp };
335    let defaultness = ast::Defaultness::Implicit;
336
337    // Honor the reexport_test_harness_main attribute
338    let main_ident = match cx.reexport_test_harness_main {
339        Some(sym) => Ident::new(sym, sp.with_ctxt(SyntaxContext::root())),
340        None => Ident::new(sym::main, sp),
341    };
342
343    let main = ast::ItemKind::Fn(Box::new(ast::Fn {
344        defaultness,
345        sig,
346        ident: main_ident,
347        generics: ast::Generics::default(),
348        contract: None,
349        body: Some(main_body),
350        define_opaque: None,
351        eii_impls: ThinVec::new(),
352    }));
353
354    let main = Box::new(ast::Item {
355        attrs: {
    let len = [(), (), ()].len();
    let mut vec = ::thin_vec::ThinVec::with_capacity(len);
    vec.push(main_attr);
    vec.push(coverage_attr);
    vec.push(doc_hidden_attr);
    vec
}thin_vec![main_attr, coverage_attr, doc_hidden_attr],
356        id: ast::DUMMY_NODE_ID,
357        kind: main,
358        vis: ast::Visibility { span: sp, kind: ast::VisibilityKind::Public, tokens: None },
359        span: sp,
360        tokens: None,
361    });
362
363    // Integrate the new item into existing module structures.
364    let main = AstFragment::Items({
    let count = 0usize + 1usize;
    let mut vec = ::smallvec::SmallVec::new();
    if count <= vec.inline_size() {
        vec.push(main);
        vec
    } else {
        ::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
                    [main])))
    }
}smallvec![main]);
365    cx.ext_cx.monotonic_expander().fully_expand_fragment(main).make_items().pop().unwrap()
366}
367
368/// Creates a slice containing every test like so:
369/// &[&test1, &test2]
370fn mk_tests_slice(cx: &TestCtxt<'_>, sp: Span) -> Box<ast::Expr> {
371    {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_builtin_macros/src/test_harness.rs:371",
                        "rustc_builtin_macros::test_harness",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_builtin_macros/src/test_harness.rs"),
                        ::tracing_core::__macro_support::Option::Some(371u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_builtin_macros::test_harness"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("building test vector from {0} tests",
                                                    cx.test_cases.len()) as &dyn Value))])
            });
    } else { ; }
};debug!("building test vector from {} tests", cx.test_cases.len());
372    let ecx = &cx.ext_cx;
373
374    let mut tests = cx.test_cases.clone();
375    // Note that this sort is load-bearing: the libtest harness uses binary search to find tests by
376    // name.
377    tests.sort_by(|a, b| a.name.as_str().cmp(b.name.as_str()));
378
379    ecx.expr_array_ref(
380        sp,
381        tests
382            .iter()
383            .map(|test| {
384                ecx.expr_addr_of(test.span, ecx.expr_path(ecx.path(test.span, ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [test.ident]))vec![test.ident])))
385            })
386            .collect(),
387    )
388}
389
390fn get_test_name(i: &ast::Item) -> Option<Symbol> {
391    attr::first_attr_value_str_by_name(&i.attrs, sym::rustc_test_marker)
392}
393
394fn get_test_runner(sess: &Session, krate: &ast::Crate) -> Option<ast::Path> {
395    match AttributeParser::parse_limited(sess, &krate.attrs, &[sym::test_runner]) {
396        Some(rustc_hir::Attribute::Parsed(AttributeKind::TestRunner(path))) => Some(path),
397        _ => None,
398    }
399}