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