2011-07-06 23:28:07 +00:00
|
|
|
// Code that generates a test runner to run all the tests in a crate
|
|
|
|
|
2019-02-06 17:33:01 +00:00
|
|
|
use log::debug;
|
|
|
|
use smallvec::{smallvec, SmallVec};
|
2019-07-18 18:29:15 +00:00
|
|
|
use syntax::ast::{self, Ident};
|
|
|
|
use syntax::attr;
|
|
|
|
use syntax::entry::{self, EntryPointType};
|
2019-08-25 20:03:24 +00:00
|
|
|
use syntax::ext::base::{ExtCtxt, Resolver};
|
2019-08-13 23:30:09 +00:00
|
|
|
use syntax::ext::expand::{AstFragment, ExpansionConfig};
|
2019-07-18 18:29:15 +00:00
|
|
|
use syntax::feature_gate::Features;
|
|
|
|
use syntax::mut_visit::{*, ExpectOne};
|
|
|
|
use syntax::parse::ParseSess;
|
|
|
|
use syntax::ptr::P;
|
2019-08-25 20:03:24 +00:00
|
|
|
use syntax::source_map::respan;
|
|
|
|
use syntax::symbol::{sym, Symbol};
|
2019-07-18 18:29:15 +00:00
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
2019-08-25 20:03:24 +00:00
|
|
|
use syntax_pos::hygiene::{AstPass, SyntaxContext, Transparency};
|
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-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
|
2019-07-18 20:29:57 +00:00
|
|
|
pub fn inject(
|
|
|
|
sess: &ParseSess,
|
|
|
|
resolver: &mut dyn Resolver,
|
|
|
|
should_test: bool,
|
|
|
|
krate: &mut ast::Crate,
|
|
|
|
span_diagnostic: &errors::Handler,
|
|
|
|
features: &Features,
|
|
|
|
) {
|
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 =
|
2019-05-08 03:21:18 +00:00
|
|
|
attr::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
|
|
|
|
let test_runner = get_test_runner(span_diagnostic, &krate);
|
|
|
|
|
2012-11-07 07:26:44 +00:00
|
|
|
if should_test {
|
2018-02-08 22:16:39 +00:00
|
|
|
generate_test_harness(sess, resolver, reexport_test_harness_main,
|
2019-08-25 20:03:24 +00:00
|
|
|
krate, features, 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
|
2019-08-13 23:30:09 +00:00
|
|
|
c.module.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();
|
|
|
|
if is_test_case(&item) {
|
|
|
|
debug!("this is a test item");
|
2018-05-17 05:55:18 +00:00
|
|
|
|
|
|
|
let test = Test {
|
2018-07-21 01:04:02 +00:00
|
|
|
span: item.span,
|
2019-08-25 20:03:24 +00:00
|
|
|
ident: item.ident,
|
2018-05-17 05:55:18 +00:00
|
|
|
};
|
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
|
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
|
|
|
if let ast::ItemKind::Mod(mut module) = item.node {
|
2019-06-30 18:30:01 +00:00
|
|
|
let tests = mem::take(&mut self.tests);
|
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
|
|
|
noop_visit_mod(&mut module, 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() {
|
|
|
|
let parent = if item.id == ast::DUMMY_NODE_ID {
|
|
|
|
ast::CRATE_NODE_ID
|
2016-09-23 07:23:01 +00:00
|
|
|
} else {
|
2019-08-25 20:03:24 +00:00
|
|
|
item.id
|
|
|
|
};
|
|
|
|
// Create an identifier that will hygienically resolve the test
|
|
|
|
// case name, even in another module.
|
2019-08-28 09:41:29 +00:00
|
|
|
let expn_id = self.cx.ext_cx.resolver.expansion_for_ast_pass(
|
2019-08-25 20:03:24 +00:00
|
|
|
module.inner,
|
|
|
|
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
|
|
|
}
|
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
|
|
|
item.node = ast::ItemKind::Mod(module);
|
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
|
|
|
}
|
2016-06-26 03:32:45 +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 visit_mac(&mut self, _mac: &mut ast::Mac) {
|
|
|
|
// Do nothing.
|
|
|
|
}
|
2014-07-21 05:10:11 +00:00
|
|
|
}
|
2013-08-29 19:10:02 +00:00
|
|
|
|
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
|
2015-08-24 15:34:04 +00:00
|
|
|
struct EntryPointCleaner {
|
|
|
|
// Current depth in the ast
|
|
|
|
depth: usize,
|
2019-08-28 21:47:52 +00:00
|
|
|
def_site: Span,
|
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
|
|
|
impl MutVisitor for EntryPointCleaner {
|
|
|
|
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.
|
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 = match entry::entry_point_type(&item, self.depth) {
|
2015-08-24 15:34:04 +00:00
|
|
|
EntryPointType::MainNamed |
|
|
|
|
EntryPointType::MainAttr |
|
|
|
|
EntryPointType::Start =>
|
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
|
|
|
item.map(|ast::Item {id, ident, attrs, node, vis, span, tokens}| {
|
2019-08-28 21:47:52 +00:00
|
|
|
let allow_ident = Ident::new(sym::allow, self.def_site);
|
|
|
|
let dc_nested = attr::mk_nested_word_item(
|
|
|
|
Ident::from_str_and_span("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);
|
2015-08-24 15:34:04 +00:00
|
|
|
|
|
|
|
ast::Item {
|
2017-08-07 05:54:09 +00:00
|
|
|
id,
|
|
|
|
ident,
|
2015-08-24 18:33:22 +00:00
|
|
|
attrs: attrs.into_iter()
|
|
|
|
.filter(|attr| {
|
2019-05-08 03:21:18 +00:00
|
|
|
!attr.check_name(sym::main) && !attr.check_name(sym::start)
|
2015-08-24 18:33:22 +00:00
|
|
|
})
|
2015-08-24 15:34:04 +00:00
|
|
|
.chain(iter::once(allow_dead_code))
|
|
|
|
.collect(),
|
2017-08-07 05:54:09 +00:00
|
|
|
node,
|
|
|
|
vis,
|
|
|
|
span,
|
|
|
|
tokens,
|
2015-08-24 15:34:04 +00:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
EntryPointType::None |
|
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
|
|
|
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
|
|
|
}
|
2016-06-26 03:32:45 +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 visit_mac(&mut self, _mac: &mut ast::Mac) {
|
|
|
|
// Do nothing.
|
|
|
|
}
|
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
|
2014-07-25 02:44:24 +00:00
|
|
|
fn generate_test_harness(sess: &ParseSess,
|
2018-07-10 19:06:26 +00:00
|
|
|
resolver: &mut dyn Resolver,
|
2016-11-16 10:52:37 +00:00
|
|
|
reexport_test_harness_main: Option<Symbol>,
|
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
|
|
|
krate: &mut ast::Crate,
|
2018-07-21 01:04:02 +00:00
|
|
|
features: &Features,
|
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
|
|
|
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);
|
|
|
|
|
2019-08-28 21:47:52 +00:00
|
|
|
let ext_cx = ExtCtxt::new(sess, econfig, resolver);
|
|
|
|
|
|
|
|
let expn_id = ext_cx.resolver.expansion_for_ast_pass(
|
|
|
|
DUMMY_SP,
|
|
|
|
AstPass::TestHarness,
|
|
|
|
&[sym::main, sym::test, sym::rustc_attrs],
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
let def_site = DUMMY_SP.with_def_site_ctxt(expn_id);
|
|
|
|
|
|
|
|
// Remove the entry points
|
|
|
|
let mut cleaner = EntryPointCleaner { depth: 0, def_site };
|
|
|
|
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,
|
|
|
|
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,
|
2018-07-21 01:04:02 +00:00
|
|
|
test_runner
|
2013-02-04 22:02:01 +00:00
|
|
|
};
|
2011-07-27 12:19:39 +00:00
|
|
|
|
2016-09-02 09:12:47 +00:00
|
|
|
TestHarnessGenerator {
|
2017-08-07 05:54:09 +00:00
|
|
|
cx,
|
2014-07-27 19:02:19 +00:00
|
|
|
tests: Vec::new(),
|
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
|
|
|
}.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
|
|
|
|
///
|
|
|
|
/// #[main]
|
|
|
|
/// pub fn main() {
|
|
|
|
/// extern crate test;
|
|
|
|
/// test::test_main_static(&[
|
|
|
|
/// &test_const1,
|
|
|
|
/// &test_const2,
|
|
|
|
/// &test_const3,
|
|
|
|
/// ]);
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// 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:
|
|
|
|
///
|
|
|
|
/// `reexport_test_harness_main` provides a different name for the `main`
|
|
|
|
/// function and `test_runner` provides a path that replaces
|
|
|
|
/// `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
|
|
|
|
2015-01-22 02:13:08 +00:00
|
|
|
// test::test_main_static(...)
|
2018-07-21 01:04:02 +00:00
|
|
|
let mut test_runner = cx.test_runner.clone().unwrap_or(
|
2019-09-14 20:16:51 +00:00
|
|
|
ecx.path(sp, vec![test_id, ecx.ident_of("test_main_static", 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);
|
2015-01-22 02:13:08 +00:00
|
|
|
let call_test_main = ecx.expr_call(sp, test_main_path_expr,
|
2019-08-25 20:03:24 +00:00
|
|
|
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
|
2018-07-21 01:04:02 +00:00
|
|
|
let test_extern_stmt = ecx.stmt_item(sp, ecx.item(sp,
|
|
|
|
test_id,
|
|
|
|
vec![],
|
2019-06-11 18:47:52 +00:00
|
|
|
ast::ItemKind::ExternCrate(None)
|
2018-07-21 01:04:02 +00:00
|
|
|
));
|
|
|
|
|
2019-08-28 21:47:52 +00:00
|
|
|
// #[main]
|
|
|
|
let main_meta = ecx.meta_word(sp, sym::main);
|
|
|
|
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])
|
|
|
|
};
|
|
|
|
|
2018-03-22 15:55:57 +00:00
|
|
|
let main = ast::ItemKind::Fn(ecx.fn_decl(vec![], ast::FunctionRetTy::Ty(main_ret_ty)),
|
2018-05-17 05:55:18 +00:00
|
|
|
ast::FnHeader::default(),
|
2018-04-26 13:07:26 +00:00
|
|
|
ast::Generics::default(),
|
|
|
|
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,
|
|
|
|
node: main,
|
2019-08-25 20:03:24 +00:00
|
|
|
vis: respan(sp, ast::VisibilityKind::Public),
|
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());
|
|
|
|
let ref ecx = cx.ext_cx;
|
|
|
|
|
2019-08-25 20:03:24 +00:00
|
|
|
|
|
|
|
ecx.expr_vec_slice(sp,
|
2018-07-21 01:04:02 +00:00
|
|
|
cx.test_cases.iter().map(|test| {
|
|
|
|
ecx.expr_addr_of(test.span,
|
2019-08-25 20:03:24 +00:00
|
|
|
ecx.expr_path(ecx.path(test.span, vec![test.ident])))
|
2018-07-21 01:04:02 +00:00
|
|
|
}).collect())
|
2013-08-15 06:06:33 +00:00
|
|
|
}
|
2013-03-14 18:22:51 +00:00
|
|
|
|
2018-07-21 01:04:02 +00:00
|
|
|
fn is_test_case(i: &ast::Item) -> bool {
|
2019-05-08 03:21:18 +00:00
|
|
|
attr::contains_name(&i.attrs, sym::rustc_test_marker)
|
2018-07-21 01:04:02 +00:00
|
|
|
}
|
2014-07-25 00:01:42 +00:00
|
|
|
|
2018-07-21 01:04:02 +00:00
|
|
|
fn get_test_runner(sd: &errors::Handler, krate: &ast::Crate) -> Option<ast::Path> {
|
2019-05-08 03:21:18 +00:00
|
|
|
let test_attr = attr::find_by_name(&krate.attrs, sym::test_runner)?;
|
2019-01-01 23:21:05 +00:00
|
|
|
test_attr.meta_item_list().map(|meta_list| {
|
2018-07-21 01:04:02 +00:00
|
|
|
if meta_list.len() != 1 {
|
2019-03-03 17:56:24 +00:00
|
|
|
sd.span_fatal(test_attr.span,
|
2019-07-23 18:03:20 +00:00
|
|
|
"`#![test_runner(..)]` accepts exactly 1 argument").raise()
|
2018-07-21 01:04:02 +00:00
|
|
|
}
|
2019-02-28 06:17:24 +00:00
|
|
|
match meta_list[0].meta_item() {
|
2019-03-02 16:15:26 +00:00
|
|
|
Some(meta_item) if meta_item.is_word() => meta_item.path.clone(),
|
2019-02-28 06:17:24 +00:00
|
|
|
_ => sd.span_fatal(test_attr.span, "`test_runner` argument must be a path").raise()
|
|
|
|
}
|
2019-01-01 23:21:05 +00:00
|
|
|
})
|
2013-08-15 06:06:33 +00:00
|
|
|
}
|