2011-07-06 23:28:07 +00:00
|
|
|
// Code that generates a test runner to run all the tests in a crate
|
|
|
|
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast as ast;
|
2020-07-30 01:27:50 +00:00
|
|
|
use rustc_ast::entry::EntryPointType;
|
2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::mut_visit::{ExpectOne, *};
|
|
|
|
use rustc_ast::ptr::P;
|
2021-02-16 21:56:07 +00:00
|
|
|
use rustc_ast::{attr, ModKind};
|
2020-06-27 20:51:28 +00:00
|
|
|
use rustc_expand::base::{ExtCtxt, ResolverExpand};
|
2019-12-29 14:23:55 +00:00
|
|
|
use rustc_expand::expand::{AstFragment, ExpansionConfig};
|
2019-11-29 23:23:38 +00:00
|
|
|
use rustc_feature::Features;
|
2020-07-30 01:27:50 +00:00
|
|
|
use rustc_session::Session;
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::hygiene::{AstPass, SyntaxContext, Transparency};
|
2020-04-19 11:00:18 +00:00
|
|
|
use rustc_span::symbol::{sym, Ident, Symbol};
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::{Span, DUMMY_SP};
|
2019-09-12 00:13:34 +00:00
|
|
|
use rustc_target::spec::PanicStrategy;
|
2019-12-22 22:42:04 +00:00
|
|
|
use smallvec::{smallvec, SmallVec};
|
2020-08-05 11:35:53 +00:00
|
|
|
use tracing::debug;
|
2019-07-18 18:29:15 +00:00
|
|
|
|
|
|
|
use std::{iter, mem};
|
2013-02-13 19:46:14 +00:00
|
|
|
|
2013-02-19 07:40:42 +00:00
|
|
|
struct Test {
|
2013-08-31 16:13:04 +00:00
|
|
|
span: Span,
|
2019-08-25 20:03:24 +00:00
|
|
|
ident: Ident,
|
2013-02-19 07:40:42 +00:00
|
|
|
}
|
2013-02-04 22:02:01 +00:00
|
|
|
|
2013-12-25 18:10:33 +00:00
|
|
|
struct TestCtxt<'a> {
|
|
|
|
ext_cx: ExtCtxt<'a>,
|
2019-09-12 00:13:34 +00:00
|
|
|
panic_strategy: PanicStrategy,
|
2019-08-28 21:47:52 +00:00
|
|
|
def_site: Span,
|
2018-07-21 01:04:02 +00:00
|
|
|
test_cases: Vec<Test>,
|
2016-11-16 10:52:37 +00:00
|
|
|
reexport_test_harness_main: Option<Symbol>,
|
2018-07-21 01:04:02 +00:00
|
|
|
test_runner: Option<ast::Path>,
|
2013-02-04 22:02:01 +00:00
|
|
|
}
|
2011-07-06 21:29:50 +00:00
|
|
|
|
|
|
|
// Traverse the crate, collecting all the test functions, eliding any
|
|
|
|
// existing main functions, and synthesizing a main test harness
|
2020-07-30 01:27:50 +00:00
|
|
|
pub fn inject(sess: &Session, resolver: &mut dyn ResolverExpand, krate: &mut ast::Crate) {
|
|
|
|
let span_diagnostic = sess.diagnostic();
|
|
|
|
let panic_strategy = sess.panic_strategy();
|
2020-11-08 11:27:51 +00:00
|
|
|
let platform_panic_strategy = sess.target.panic_strategy;
|
2020-07-30 01:27:50 +00:00
|
|
|
|
2019-08-25 20:03:24 +00:00
|
|
|
// Check for #![reexport_test_harness_main = "some_name"] which gives the
|
|
|
|
// main test function the name `some_name` without hygiene. This needs to be
|
2014-08-08 14:01:05 +00:00
|
|
|
// unconditional, so that the attribute is still marked as used in
|
|
|
|
// non-test builds.
|
|
|
|
let reexport_test_harness_main =
|
2020-07-30 01:27:50 +00:00
|
|
|
sess.first_attr_value_str_by_name(&krate.attrs, sym::reexport_test_harness_main);
|
2014-08-08 14:01:05 +00:00
|
|
|
|
2018-07-21 01:04:02 +00:00
|
|
|
// Do this here so that the test_runner crate attribute gets marked as used
|
|
|
|
// even in non-test builds
|
2020-07-30 01:27:50 +00:00
|
|
|
let test_runner = get_test_runner(sess, span_diagnostic, &krate);
|
2018-07-21 01:04:02 +00:00
|
|
|
|
2020-07-30 01:27:50 +00:00
|
|
|
if sess.opts.test {
|
|
|
|
let panic_strategy = match (panic_strategy, sess.opts.debugging_opts.panic_abort_tests) {
|
2019-12-22 22:42:04 +00:00
|
|
|
(PanicStrategy::Abort, true) => PanicStrategy::Abort,
|
2019-09-20 02:33:38 +00:00
|
|
|
(PanicStrategy::Abort, false) => {
|
2020-07-30 01:27:50 +00:00
|
|
|
if panic_strategy == platform_panic_strategy {
|
|
|
|
// Silently allow compiling with panic=abort on these platforms,
|
|
|
|
// but with old behavior (abort if a test fails).
|
|
|
|
} else {
|
|
|
|
span_diagnostic.err(
|
|
|
|
"building tests with panic=abort is not supported \
|
|
|
|
without `-Zpanic_abort_tests`",
|
|
|
|
);
|
|
|
|
}
|
2019-09-20 02:33:38 +00:00
|
|
|
PanicStrategy::Unwind
|
|
|
|
}
|
|
|
|
(PanicStrategy::Unwind, _) => PanicStrategy::Unwind,
|
|
|
|
};
|
2019-12-22 22:42:04 +00:00
|
|
|
generate_test_harness(
|
|
|
|
sess,
|
|
|
|
resolver,
|
|
|
|
reexport_test_harness_main,
|
|
|
|
krate,
|
2020-07-30 01:27:50 +00:00
|
|
|
&sess.features_untracked(),
|
2019-12-22 22:42:04 +00:00
|
|
|
panic_strategy,
|
|
|
|
test_runner,
|
|
|
|
)
|
2012-01-06 01:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-25 18:10:33 +00:00
|
|
|
struct TestHarnessGenerator<'a> {
|
|
|
|
cx: TestCtxt<'a>,
|
2019-08-25 20:03:24 +00:00
|
|
|
tests: Vec<Test>,
|
2013-08-29 19:10:02 +00:00
|
|
|
}
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
impl<'a> MutVisitor for TestHarnessGenerator<'a> {
|
|
|
|
fn visit_crate(&mut self, c: &mut ast::Crate) {
|
|
|
|
noop_visit_crate(c, self);
|
2013-08-29 19:10:02 +00:00
|
|
|
|
2018-07-21 01:04:02 +00:00
|
|
|
// Create a main function to run our tests
|
2021-02-14 18:14:12 +00:00
|
|
|
c.items.push(mk_main(&mut self.cx));
|
2013-08-29 19:10:02 +00:00
|
|
|
}
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
fn flat_map_item(&mut self, i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
|
2018-07-21 01:04:02 +00:00
|
|
|
let mut item = i.into_inner();
|
2020-07-30 01:27:50 +00:00
|
|
|
if is_test_case(&self.cx.ext_cx.sess, &item) {
|
2018-07-21 01:04:02 +00:00
|
|
|
debug!("this is a test item");
|
2018-05-17 05:55:18 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
let test = Test { span: item.span, ident: item.ident };
|
2019-08-25 20:03:24 +00:00
|
|
|
self.tests.push(test);
|
2016-09-23 07:23:01 +00:00
|
|
|
}
|
2013-08-29 19:10:02 +00:00
|
|
|
|
2014-07-21 05:10:11 +00:00
|
|
|
// We don't want to recurse into anything other than mods, since
|
|
|
|
// mods or tests inside of functions will break things
|
2021-02-14 18:14:12 +00:00
|
|
|
if let ast::ItemKind::Mod(..) = item.kind {
|
2019-06-30 18:30:01 +00:00
|
|
|
let tests = mem::take(&mut self.tests);
|
2021-02-14 18:14:12 +00:00
|
|
|
noop_visit_item_kind(&mut item.kind, self);
|
2019-08-25 20:03:24 +00:00
|
|
|
let mut tests = mem::replace(&mut self.tests, tests);
|
2016-09-23 07:23:01 +00:00
|
|
|
|
2019-08-25 20:03:24 +00:00
|
|
|
if !tests.is_empty() {
|
2019-12-22 22:42:04 +00:00
|
|
|
let parent =
|
|
|
|
if item.id == ast::DUMMY_NODE_ID { ast::CRATE_NODE_ID } else { item.id };
|
2019-08-25 20:03:24 +00:00
|
|
|
// Create an identifier that will hygienically resolve the test
|
|
|
|
// case name, even in another module.
|
2021-02-16 21:56:07 +00:00
|
|
|
let inner_span = match item.kind {
|
|
|
|
ast::ItemKind::Mod(_, ModKind::Loaded(.., span)) => span,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2019-08-28 09:41:29 +00:00
|
|
|
let expn_id = self.cx.ext_cx.resolver.expansion_for_ast_pass(
|
2021-02-16 21:56:07 +00:00
|
|
|
inner_span,
|
2019-08-25 20:03:24 +00:00
|
|
|
AstPass::TestHarness,
|
|
|
|
&[],
|
|
|
|
Some(parent),
|
|
|
|
);
|
|
|
|
for test in &mut tests {
|
2019-08-28 09:41:29 +00:00
|
|
|
// See the comment on `mk_main` for why we're using
|
|
|
|
// `apply_mark` directly.
|
|
|
|
test.ident.span = test.ident.span.apply_mark(expn_id, Transparency::Opaque);
|
2016-09-23 07:23:01 +00:00
|
|
|
}
|
2019-08-25 20:03:24 +00:00
|
|
|
self.cx.test_cases.extend(tests);
|
2016-09-23 07:23:01 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-13 19:15:16 +00:00
|
|
|
smallvec![P(item)]
|
2014-07-21 05:10:11 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-29 19:10:02 +00:00
|
|
|
|
2020-07-30 01:27:50 +00:00
|
|
|
// Beware, this is duplicated in librustc_passes/entry.rs (with
|
|
|
|
// `rustc_hir::Item`), so make sure to keep them in sync.
|
|
|
|
fn entry_point_type(sess: &Session, item: &ast::Item, depth: usize) -> EntryPointType {
|
|
|
|
match item.kind {
|
|
|
|
ast::ItemKind::Fn(..) => {
|
|
|
|
if sess.contains_name(&item.attrs, sym::start) {
|
|
|
|
EntryPointType::Start
|
2021-04-08 13:37:38 +00:00
|
|
|
} else if sess.contains_name(&item.attrs, sym::rustc_main) {
|
2020-07-30 01:27:50 +00:00
|
|
|
EntryPointType::MainAttr
|
|
|
|
} else if item.ident.name == sym::main {
|
|
|
|
if depth == 1 {
|
|
|
|
// This is a top-level function so can be 'main'
|
|
|
|
EntryPointType::MainNamed
|
|
|
|
} else {
|
|
|
|
EntryPointType::OtherMain
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
EntryPointType::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => EntryPointType::None,
|
|
|
|
}
|
|
|
|
}
|
2018-07-21 01:04:02 +00:00
|
|
|
/// A folder used to remove any entry points (like fn main) because the harness
|
|
|
|
/// generator will provide its own
|
2020-07-30 01:27:50 +00:00
|
|
|
struct EntryPointCleaner<'a> {
|
2015-08-24 15:34:04 +00:00
|
|
|
// Current depth in the ast
|
2020-07-30 01:27:50 +00:00
|
|
|
sess: &'a Session,
|
2015-08-24 15:34:04 +00:00
|
|
|
depth: usize,
|
2019-08-28 21:47:52 +00:00
|
|
|
def_site: Span,
|
2015-08-24 15:34:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 01:27:50 +00:00
|
|
|
impl<'a> MutVisitor for EntryPointCleaner<'a> {
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
fn flat_map_item(&mut self, i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
|
2015-08-24 15:34:04 +00:00
|
|
|
self.depth += 1;
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
let item = noop_flat_map_item(i, self).expect_one("noop did something");
|
2015-08-24 15:34:04 +00:00
|
|
|
self.depth -= 1;
|
|
|
|
|
2015-08-24 18:33:22 +00:00
|
|
|
// Remove any #[main] or #[start] from the AST so it doesn't
|
|
|
|
// clash with the one we're going to add, but mark it as
|
2015-08-24 15:34:04 +00:00
|
|
|
// #[allow(dead_code)] to avoid printing warnings.
|
2020-07-30 01:27:50 +00:00
|
|
|
let item = match entry_point_type(self.sess, &item, self.depth) {
|
2019-12-22 22:42:04 +00:00
|
|
|
EntryPointType::MainNamed | EntryPointType::MainAttr | EntryPointType::Start => item
|
2020-02-23 09:24:30 +00:00
|
|
|
.map(|ast::Item { id, ident, attrs, kind, vis, span, tokens }| {
|
2019-08-28 21:47:52 +00:00
|
|
|
let allow_ident = Ident::new(sym::allow, self.def_site);
|
2020-07-08 01:04:10 +00:00
|
|
|
let dc_nested =
|
|
|
|
attr::mk_nested_word_item(Ident::new(sym::dead_code, self.def_site));
|
2019-08-04 21:59:06 +00:00
|
|
|
let allow_dead_code_item = attr::mk_list_item(allow_ident, vec![dc_nested]);
|
2019-07-30 18:18:19 +00:00
|
|
|
let allow_dead_code = attr::mk_attr_outer(allow_dead_code_item);
|
2020-02-22 02:29:17 +00:00
|
|
|
let attrs = attrs
|
|
|
|
.into_iter()
|
2020-07-30 01:27:50 +00:00
|
|
|
.filter(|attr| {
|
2021-04-08 13:37:38 +00:00
|
|
|
!self.sess.check_name(attr, sym::rustc_main)
|
2020-07-30 01:27:50 +00:00
|
|
|
&& !self.sess.check_name(attr, sym::start)
|
|
|
|
})
|
2020-02-22 02:29:17 +00:00
|
|
|
.chain(iter::once(allow_dead_code))
|
|
|
|
.collect();
|
2015-08-24 15:34:04 +00:00
|
|
|
|
2020-02-23 09:24:30 +00:00
|
|
|
ast::Item { id, ident, attrs, kind, vis, span, tokens }
|
2015-08-24 15:34:04 +00:00
|
|
|
}),
|
2019-12-22 22:42:04 +00:00
|
|
|
EntryPointType::None | EntryPointType::OtherMain => item,
|
2015-08-24 15:34:04 +00:00
|
|
|
};
|
|
|
|
|
Overhaul `syntax::fold::Folder`.
This commit changes `syntax::fold::Folder` from a functional style
(where most methods take a `T` and produce a new `T`) to a more
imperative style (where most methods take and modify a `&mut T`), and
renames it `syntax::mut_visit::MutVisitor`.
The first benefit is speed. The functional style does not require any
reallocations, due to the use of `P::map` and
`MoveMap::move_{,flat_}map`. However, every field in the AST must be
overwritten; even those fields that are unchanged are overwritten with
the same value. This causes a lot of unnecessary memory writes. The
imperative style reduces instruction counts by 1--3% across a wide range
of workloads, particularly incremental workloads.
The second benefit is conciseness; the imperative style is usually more
concise. E.g. compare the old functional style:
```
fn fold_abc(&mut self, abc: ABC) {
ABC {
a: fold_a(abc.a),
b: fold_b(abc.b),
c: abc.c,
}
}
```
with the imperative style:
```
fn visit_abc(&mut self, ABC { a, b, c: _ }: &mut ABC) {
visit_a(a);
visit_b(b);
}
```
(The reductions get larger in more complex examples.)
Overall, the patch removes over 200 lines of code -- even though the new
code has more comments -- and a lot of the remaining lines have fewer
characters.
Some notes:
- The old style used methods called `fold_*`. The new style mostly uses
methods called `visit_*`, but there are a few methods that map a `T`
to something other than a `T`, which are called `flat_map_*` (`T` maps
to multiple `T`s) or `filter_map_*` (`T` maps to 0 or 1 `T`s).
- `move_map.rs`/`MoveMap`/`move_map`/`move_flat_map` are renamed
`map_in_place.rs`/`MapInPlace`/`map_in_place`/`flat_map_in_place` to
reflect their slightly changed signatures.
- Although this commit renames the `fold` module as `mut_visit`, it
keeps it in the `fold.rs` file, so as not to confuse git. The next
commit will rename the file.
2019-02-05 04:20:55 +00:00
|
|
|
smallvec![item]
|
2015-08-24 15:34:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 01:04:02 +00:00
|
|
|
/// Crawl over the crate, inserting test reexports and the test main function
|
2019-12-22 22:42:04 +00:00
|
|
|
fn generate_test_harness(
|
2020-07-30 01:27:50 +00:00
|
|
|
sess: &Session,
|
2020-06-27 20:51:28 +00:00
|
|
|
resolver: &mut dyn ResolverExpand,
|
2019-12-22 22:42:04 +00:00
|
|
|
reexport_test_harness_main: Option<Symbol>,
|
|
|
|
krate: &mut ast::Crate,
|
|
|
|
features: &Features,
|
|
|
|
panic_strategy: PanicStrategy,
|
|
|
|
test_runner: Option<ast::Path>,
|
|
|
|
) {
|
2018-02-08 22:16:39 +00:00
|
|
|
let mut econfig = ExpansionConfig::default("test".to_string());
|
|
|
|
econfig.features = Some(features);
|
|
|
|
|
2020-03-15 23:43:37 +00:00
|
|
|
let ext_cx = ExtCtxt::new(sess, econfig, resolver, None);
|
2019-08-28 21:47:52 +00:00
|
|
|
|
|
|
|
let expn_id = ext_cx.resolver.expansion_for_ast_pass(
|
|
|
|
DUMMY_SP,
|
|
|
|
AstPass::TestHarness,
|
2021-04-08 13:37:38 +00:00
|
|
|
&[sym::test, sym::rustc_attrs],
|
2019-08-28 21:47:52 +00:00
|
|
|
None,
|
|
|
|
);
|
|
|
|
let def_site = DUMMY_SP.with_def_site_ctxt(expn_id);
|
|
|
|
|
|
|
|
// Remove the entry points
|
2020-07-30 01:27:50 +00:00
|
|
|
let mut cleaner = EntryPointCleaner { sess, depth: 0, def_site };
|
2019-08-28 21:47:52 +00:00
|
|
|
cleaner.visit_crate(krate);
|
|
|
|
|
2017-12-06 18:50:55 +00:00
|
|
|
let cx = TestCtxt {
|
2019-08-28 21:47:52 +00:00
|
|
|
ext_cx,
|
2019-09-12 00:13:34 +00:00
|
|
|
panic_strategy,
|
2019-08-28 21:47:52 +00:00
|
|
|
def_site,
|
2018-07-21 01:04:02 +00:00
|
|
|
test_cases: Vec::new(),
|
2017-08-07 05:54:09 +00:00
|
|
|
reexport_test_harness_main,
|
2019-12-22 22:42:04 +00:00
|
|
|
test_runner,
|
2013-02-04 22:02:01 +00:00
|
|
|
};
|
2011-07-27 12:19:39 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
TestHarnessGenerator { cx, tests: Vec::new() }.visit_crate(krate);
|
2011-07-06 21:29:50 +00:00
|
|
|
}
|
|
|
|
|
2018-07-21 01:04:02 +00:00
|
|
|
/// Creates a function item for use as the main function of a test build.
|
|
|
|
/// This function will call the `test_runner` as specified by the crate attribute
|
2019-08-28 21:47:52 +00:00
|
|
|
///
|
|
|
|
/// By default this expands to
|
|
|
|
///
|
2020-05-01 20:32:33 +00:00
|
|
|
/// ```
|
2021-04-08 13:37:38 +00:00
|
|
|
/// #[rustc_main]
|
2019-08-28 21:47:52 +00:00
|
|
|
/// pub fn main() {
|
|
|
|
/// extern crate test;
|
|
|
|
/// test::test_main_static(&[
|
|
|
|
/// &test_const1,
|
|
|
|
/// &test_const2,
|
|
|
|
/// &test_const3,
|
|
|
|
/// ]);
|
|
|
|
/// }
|
2020-05-01 20:32:33 +00:00
|
|
|
/// ```
|
2019-08-28 21:47:52 +00:00
|
|
|
///
|
|
|
|
/// Most of the Ident have the usual def-site hygiene for the AST pass. The
|
|
|
|
/// exception is the `test_const`s. These have a syntax context that has two
|
|
|
|
/// opaque marks: one from the expansion of `test` or `test_case`, and one
|
|
|
|
/// generated in `TestHarnessGenerator::flat_map_item`. When resolving this
|
|
|
|
/// identifier after failing to find a matching identifier in the root module
|
|
|
|
/// we remove the outer mark, and try resolving at its def-site, which will
|
|
|
|
/// then resolve to `test_const`.
|
|
|
|
///
|
|
|
|
/// The expansion here can be controlled by two attributes:
|
|
|
|
///
|
2020-05-01 20:32:33 +00:00
|
|
|
/// [`TestCtxt::reexport_test_harness_main`] provides a different name for the `main`
|
|
|
|
/// function and [`TestCtxt::test_runner`] provides a path that replaces
|
2019-08-28 21:47:52 +00:00
|
|
|
/// `test::test_main_static`.
|
2019-02-06 17:33:01 +00:00
|
|
|
fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
|
2019-08-28 21:47:52 +00:00
|
|
|
let sp = cx.def_site;
|
2015-01-22 02:13:08 +00:00
|
|
|
let ecx = &cx.ext_cx;
|
2019-08-25 20:03:24 +00:00
|
|
|
let test_id = Ident::new(sym::test, sp);
|
2016-11-16 08:21:52 +00:00
|
|
|
|
2019-09-12 00:13:34 +00:00
|
|
|
let runner_name = match cx.panic_strategy {
|
|
|
|
PanicStrategy::Unwind => "test_main_static",
|
|
|
|
PanicStrategy::Abort => "test_main_static_abort",
|
|
|
|
};
|
|
|
|
|
2015-01-22 02:13:08 +00:00
|
|
|
// test::test_main_static(...)
|
2019-12-22 22:42:04 +00:00
|
|
|
let mut test_runner = cx
|
|
|
|
.test_runner
|
|
|
|
.clone()
|
2020-11-06 21:24:55 +00:00
|
|
|
.unwrap_or_else(|| ecx.path(sp, vec![test_id, Ident::from_str_and_span(runner_name, sp)]));
|
2018-07-21 01:04:02 +00:00
|
|
|
|
|
|
|
test_runner.span = sp;
|
|
|
|
|
2018-10-25 18:11:11 +00:00
|
|
|
let test_main_path_expr = ecx.expr_path(test_runner);
|
2019-12-22 22:42:04 +00:00
|
|
|
let call_test_main = ecx.expr_call(sp, test_main_path_expr, vec![mk_tests_slice(cx, sp)]);
|
2015-01-22 02:13:08 +00:00
|
|
|
let call_test_main = ecx.stmt_expr(call_test_main);
|
2018-07-21 01:04:02 +00:00
|
|
|
|
2019-08-25 20:03:24 +00:00
|
|
|
// extern crate test
|
2019-12-22 22:42:04 +00:00
|
|
|
let test_extern_stmt =
|
|
|
|
ecx.stmt_item(sp, ecx.item(sp, test_id, vec![], ast::ItemKind::ExternCrate(None)));
|
2018-07-21 01:04:02 +00:00
|
|
|
|
2021-04-08 13:37:38 +00:00
|
|
|
// #[rustc_main]
|
|
|
|
let main_meta = ecx.meta_word(sp, sym::rustc_main);
|
2019-08-28 21:47:52 +00:00
|
|
|
let main_attr = ecx.attribute(main_meta);
|
|
|
|
|
2015-01-22 02:13:08 +00:00
|
|
|
// pub fn main() { ... }
|
2016-02-08 15:53:21 +00:00
|
|
|
let main_ret_ty = ecx.ty(sp, ast::TyKind::Tup(vec![]));
|
2018-07-21 01:04:02 +00:00
|
|
|
|
|
|
|
// If no test runner is provided we need to import the test crate
|
|
|
|
let main_body = if cx.test_runner.is_none() {
|
|
|
|
ecx.block(sp, vec![test_extern_stmt, call_test_main])
|
|
|
|
} else {
|
|
|
|
ecx.block(sp, vec![call_test_main])
|
|
|
|
};
|
|
|
|
|
2020-02-15 03:10:59 +00:00
|
|
|
let decl = ecx.fn_decl(vec![], ast::FnRetTy::Ty(main_ret_ty));
|
2020-08-12 21:02:14 +00:00
|
|
|
let sig = ast::FnSig { decl, header: ast::FnHeader::default(), span: sp };
|
2020-02-23 09:24:30 +00:00
|
|
|
let def = ast::Defaultness::Final;
|
2021-01-29 07:31:08 +00:00
|
|
|
let main =
|
|
|
|
ast::ItemKind::Fn(box ast::FnKind(def, sig, ast::Generics::default(), Some(main_body)));
|
2018-07-21 01:04:02 +00:00
|
|
|
|
|
|
|
// Honor the reexport_test_harness_main attribute
|
2019-05-17 00:44:51 +00:00
|
|
|
let main_id = match cx.reexport_test_harness_main {
|
2019-08-25 20:03:24 +00:00
|
|
|
Some(sym) => Ident::new(sym, sp.with_ctxt(SyntaxContext::root())),
|
2019-09-14 20:14:03 +00:00
|
|
|
None => Ident::new(sym::main, sp),
|
2019-05-17 00:44:51 +00:00
|
|
|
};
|
2018-07-21 01:04:02 +00:00
|
|
|
|
2019-08-13 23:30:09 +00:00
|
|
|
let main = P(ast::Item {
|
2018-07-21 01:04:02 +00:00
|
|
|
ident: main_id,
|
2015-01-22 02:13:08 +00:00
|
|
|
attrs: vec![main_attr],
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2019-09-26 16:51:36 +00:00
|
|
|
kind: main,
|
2020-08-21 23:11:00 +00:00
|
|
|
vis: ast::Visibility { span: sp, kind: ast::VisibilityKind::Public, tokens: None },
|
2017-07-11 00:44:46 +00:00
|
|
|
span: sp,
|
|
|
|
tokens: None,
|
2019-08-13 23:30:09 +00:00
|
|
|
});
|
2013-08-15 06:06:33 +00:00
|
|
|
|
2019-08-13 23:30:09 +00:00
|
|
|
// Integrate the new item into existing module structures.
|
|
|
|
let main = AstFragment::Items(smallvec![main]);
|
|
|
|
cx.ext_cx.monotonic_expander().fully_expand_fragment(main).make_items().pop().unwrap()
|
2012-12-28 01:53:04 +00:00
|
|
|
}
|
|
|
|
|
2018-07-21 01:04:02 +00:00
|
|
|
/// Creates a slice containing every test like so:
|
2019-08-28 21:47:52 +00:00
|
|
|
/// &[&test1, &test2]
|
2019-08-25 20:03:24 +00:00
|
|
|
fn mk_tests_slice(cx: &TestCtxt<'_>, sp: Span) -> P<ast::Expr> {
|
2018-07-21 01:04:02 +00:00
|
|
|
debug!("building test vector from {} tests", cx.test_cases.len());
|
2020-03-05 12:56:01 +00:00
|
|
|
let ecx = &cx.ext_cx;
|
2018-07-21 01:04:02 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
ecx.expr_vec_slice(
|
|
|
|
sp,
|
|
|
|
cx.test_cases
|
|
|
|
.iter()
|
|
|
|
.map(|test| {
|
|
|
|
ecx.expr_addr_of(test.span, ecx.expr_path(ecx.path(test.span, vec![test.ident])))
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
)
|
2013-08-15 06:06:33 +00:00
|
|
|
}
|
2013-03-14 18:22:51 +00:00
|
|
|
|
2020-07-30 01:27:50 +00:00
|
|
|
fn is_test_case(sess: &Session, i: &ast::Item) -> bool {
|
|
|
|
sess.contains_name(&i.attrs, sym::rustc_test_marker)
|
2018-07-21 01:04:02 +00:00
|
|
|
}
|
2014-07-25 00:01:42 +00:00
|
|
|
|
2020-07-30 01:27:50 +00:00
|
|
|
fn get_test_runner(
|
|
|
|
sess: &Session,
|
|
|
|
sd: &rustc_errors::Handler,
|
|
|
|
krate: &ast::Crate,
|
|
|
|
) -> Option<ast::Path> {
|
|
|
|
let test_attr = sess.find_by_name(&krate.attrs, sym::test_runner)?;
|
2020-03-17 12:19:06 +00:00
|
|
|
let meta_list = test_attr.meta_item_list()?;
|
|
|
|
let span = test_attr.span;
|
|
|
|
match &*meta_list {
|
|
|
|
[single] => match single.meta_item() {
|
|
|
|
Some(meta_item) if meta_item.is_word() => return Some(meta_item.path.clone()),
|
|
|
|
_ => sd.struct_span_err(span, "`test_runner` argument must be a path").emit(),
|
|
|
|
},
|
|
|
|
_ => sd.struct_span_err(span, "`#![test_runner(..)]` accepts exactly 1 argument").emit(),
|
|
|
|
}
|
|
|
|
None
|
2013-08-15 06:06:33 +00:00
|
|
|
}
|