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};
|
|
|
|
use syntax::ext::base::{ExtCtxt, Resolver};
|
|
|
|
use syntax::ext::expand::ExpansionConfig;
|
|
|
|
use syntax::ext::hygiene::{ExpnId, MacroKind};
|
|
|
|
use syntax::feature_gate::Features;
|
|
|
|
use syntax::mut_visit::{*, ExpectOne};
|
|
|
|
use syntax::parse::ParseSess;
|
|
|
|
use syntax::ptr::P;
|
|
|
|
use syntax::source_map::{ExpnInfo, ExpnKind, dummy_spanned};
|
|
|
|
use syntax::symbol::{kw, sym, Symbol};
|
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
|
|
|
|
|
|
|
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,
|
2018-07-21 01:04:02 +00:00
|
|
|
path: Vec<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> {
|
2015-12-13 22:17:55 +00:00
|
|
|
span_diagnostic: &'a errors::Handler,
|
2016-11-16 08:21:52 +00:00
|
|
|
path: Vec<Ident>,
|
2013-12-25 18:10:33 +00:00
|
|
|
ext_cx: ExtCtxt<'a>,
|
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>,
|
2014-08-31 03:36:16 +00:00
|
|
|
// top-level re-export submodule, filled out after folding is finished
|
2016-11-16 08:21:52 +00:00
|
|
|
toplevel_reexport: Option<Ident>,
|
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,
|
|
|
|
) {
|
2014-08-08 14:01:05 +00:00
|
|
|
// Check for #[reexport_test_harness_main = "some_name"] which
|
2018-03-09 15:58:44 +00:00
|
|
|
// creates a `use __test::main as some_name;`. 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,
|
2018-07-21 01:04:02 +00:00
|
|
|
krate, span_diagnostic, 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>,
|
2016-11-16 08:21:52 +00:00
|
|
|
tests: Vec<Ident>,
|
2014-08-31 03:36:16 +00:00
|
|
|
|
|
|
|
// submodule name, gensym'd identifier for re-exports
|
2016-11-16 08:21:52 +00:00
|
|
|
tested_submods: Vec<(Ident, Ident)>,
|
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
|
|
|
|
let test_main = {
|
|
|
|
let unresolved = mk_main(&mut self.cx);
|
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
|
|
|
self.cx.ext_cx.monotonic_expander().flat_map_item(unresolved).pop().unwrap()
|
2018-07-21 01:04: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
|
|
|
c.module.items.push(test_main);
|
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]> {
|
2014-11-04 22:59:42 +00:00
|
|
|
let ident = i.ident;
|
2019-05-11 14:41:37 +00:00
|
|
|
if ident.name != kw::Invalid {
|
2014-11-04 22:59:42 +00:00
|
|
|
self.cx.path.push(ident);
|
|
|
|
}
|
2016-03-29 09:12:01 +00:00
|
|
|
debug!("current path: {}", path_name_i(&self.cx.path));
|
2013-08-29 19:10:02 +00:00
|
|
|
|
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,
|
2018-05-17 05:55:18 +00:00
|
|
|
path: self.cx.path.clone(),
|
|
|
|
};
|
2018-07-21 01:04:02 +00:00
|
|
|
self.cx.test_cases.push(test);
|
|
|
|
self.tests.push(item.ident);
|
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);
|
|
|
|
let tested_submods = mem::take(&mut self.tested_submods);
|
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);
|
2016-09-23 07:23:01 +00:00
|
|
|
let tests = mem::replace(&mut self.tests, tests);
|
|
|
|
let tested_submods = mem::replace(&mut self.tested_submods, tested_submods);
|
|
|
|
|
|
|
|
if !tests.is_empty() || !tested_submods.is_empty() {
|
|
|
|
let (it, sym) = mk_reexport_mod(&mut self.cx, item.id, tests, tested_submods);
|
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
|
|
|
module.items.push(it);
|
2016-09-23 07:23:01 +00:00
|
|
|
|
|
|
|
if !self.cx.path.is_empty() {
|
|
|
|
self.tested_submods.push((self.cx.path[self.cx.path.len()-1], sym));
|
|
|
|
} else {
|
|
|
|
debug!("pushing nothing, sym: {:?}", sym);
|
|
|
|
self.cx.toplevel_reexport = Some(sym);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
2019-05-11 14:41:37 +00:00
|
|
|
if ident.name != kw::Invalid {
|
2014-11-04 22:59:42 +00:00
|
|
|
self.cx.path.pop();
|
|
|
|
}
|
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,
|
|
|
|
}
|
|
|
|
|
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-05-17 08:37:53 +00:00
|
|
|
let allow_ident = Ident::with_empty_ctxt(sym::allow);
|
2018-03-24 18:17:27 +00:00
|
|
|
let dc_nested = attr::mk_nested_word_item(Ident::from_str("dead_code"));
|
|
|
|
let allow_dead_code_item = attr::mk_list_item(DUMMY_SP, 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
|
|
|
/// Creates an item (specifically a module) that "pub use"s the tests passed in.
|
|
|
|
/// Each tested submodule will contain a similar reexport module that we will export
|
|
|
|
/// under the name of the original module. That is, `submod::__test_reexports` is
|
|
|
|
/// reexported like so `pub use submod::__test_reexports as submod`.
|
2019-02-06 17:33:01 +00:00
|
|
|
fn mk_reexport_mod(cx: &mut TestCtxt<'_>,
|
2016-11-16 08:21:52 +00:00
|
|
|
parent: ast::NodeId,
|
|
|
|
tests: Vec<Ident>,
|
|
|
|
tested_submods: Vec<(Ident, Ident)>)
|
|
|
|
-> (P<ast::Item>, Ident) {
|
2019-05-13 19:46:20 +00:00
|
|
|
let super_ = Ident::with_empty_ctxt(kw::Super);
|
2014-07-27 19:02:19 +00:00
|
|
|
|
2015-01-13 15:30:17 +00:00
|
|
|
let items = tests.into_iter().map(|r| {
|
2018-01-29 05:12:09 +00:00
|
|
|
cx.ext_cx.item_use_simple(DUMMY_SP, dummy_spanned(ast::VisibilityKind::Public),
|
2014-07-27 19:02:19 +00:00
|
|
|
cx.ext_cx.path(DUMMY_SP, vec![super_, r]))
|
2015-01-13 15:30:17 +00:00
|
|
|
}).chain(tested_submods.into_iter().map(|(r, sym)| {
|
2014-08-31 03:36:16 +00:00
|
|
|
let path = cx.ext_cx.path(DUMMY_SP, vec![super_, r, sym]);
|
2018-03-09 15:58:44 +00:00
|
|
|
cx.ext_cx.item_use_simple_(DUMMY_SP, dummy_spanned(ast::VisibilityKind::Public),
|
|
|
|
Some(r), path)
|
2016-09-06 07:52:09 +00:00
|
|
|
})).collect();
|
2014-07-27 19:02:19 +00:00
|
|
|
|
2014-07-21 05:10:11 +00:00
|
|
|
let reexport_mod = ast::Mod {
|
2018-09-10 13:19:27 +00:00
|
|
|
inline: true,
|
2014-07-21 05:10:11 +00:00
|
|
|
inner: DUMMY_SP,
|
2017-08-07 05:54:09 +00:00
|
|
|
items,
|
2014-07-21 05:10:11 +00:00
|
|
|
};
|
2014-08-31 03:36:16 +00:00
|
|
|
|
2019-05-17 00:44:51 +00:00
|
|
|
let name = Ident::from_str("__test_reexports").gensym();
|
2016-09-23 07:23:01 +00:00
|
|
|
let parent = if parent == ast::DUMMY_NODE_ID { ast::CRATE_NODE_ID } else { parent };
|
2019-07-15 22:42:58 +00:00
|
|
|
cx.ext_cx.current_expansion.id = cx.ext_cx.resolver.get_module_scope(parent);
|
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 it = cx.ext_cx.monotonic_expander().flat_map_item(P(ast::Item {
|
2019-05-17 00:44:51 +00:00
|
|
|
ident: name,
|
2014-07-21 05:10:11 +00:00
|
|
|
attrs: Vec::new(),
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2016-02-09 10:36:51 +00:00
|
|
|
node: ast::ItemKind::Mod(reexport_mod),
|
2018-01-29 05:12:09 +00:00
|
|
|
vis: dummy_spanned(ast::VisibilityKind::Public),
|
2014-07-21 05:10:11 +00:00
|
|
|
span: DUMMY_SP,
|
2017-07-11 00:44:46 +00:00
|
|
|
tokens: None,
|
2016-09-06 07:52:09 +00:00
|
|
|
})).pop().unwrap();
|
2014-08-31 03:36:16 +00:00
|
|
|
|
2019-05-17 00:44:51 +00:00
|
|
|
(it, name)
|
2013-08-29 19:10:02 +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-02-08 22:16:39 +00:00
|
|
|
sd: &errors::Handler,
|
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>) {
|
2015-08-24 15:34:04 +00:00
|
|
|
// Remove the entry points
|
|
|
|
let mut cleaner = EntryPointCleaner { depth: 0 };
|
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
|
|
|
cleaner.visit_crate(krate);
|
2015-08-24 15:34:04 +00:00
|
|
|
|
2018-02-08 22:16:39 +00:00
|
|
|
let mut econfig = ExpansionConfig::default("test".to_string());
|
|
|
|
econfig.features = Some(features);
|
|
|
|
|
2017-12-06 18:50:55 +00:00
|
|
|
let cx = TestCtxt {
|
2014-07-25 02:44:24 +00:00
|
|
|
span_diagnostic: sd,
|
2018-02-08 22:16:39 +00:00
|
|
|
ext_cx: ExtCtxt::new(sess, econfig, resolver),
|
2014-07-21 01:05:59 +00:00
|
|
|
path: Vec::new(),
|
2018-07-21 01:04:02 +00:00
|
|
|
test_cases: Vec::new(),
|
2017-08-07 05:54:09 +00:00
|
|
|
reexport_test_harness_main,
|
2014-08-31 03:36:16 +00:00
|
|
|
toplevel_reexport: None,
|
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(),
|
|
|
|
tested_submods: 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-02-06 17:33:01 +00:00
|
|
|
fn mk_main(cx: &mut TestCtxt<'_>) -> P<ast::Item> {
|
2019-07-06 18:02:45 +00:00
|
|
|
// Writing this out by hand:
|
2015-01-22 02:13:08 +00:00
|
|
|
// pub fn main() {
|
|
|
|
// #![main]
|
2019-04-17 09:02:04 +00:00
|
|
|
// test::test_main_static(&[..tests]);
|
2015-01-22 02:13:08 +00:00
|
|
|
// }
|
2019-07-15 22:04:05 +00:00
|
|
|
let sp = DUMMY_SP.fresh_expansion(ExpnId::root(), ExpnInfo::allow_unstable(
|
2019-07-06 18:02:45 +00:00
|
|
|
ExpnKind::Macro(MacroKind::Attr, sym::test_case), DUMMY_SP, cx.ext_cx.parse_sess.edition,
|
|
|
|
[sym::main, sym::test, sym::rustc_attrs][..].into(),
|
|
|
|
));
|
2015-01-22 02:13:08 +00:00
|
|
|
let ecx = &cx.ext_cx;
|
2019-06-11 18:47:52 +00:00
|
|
|
let test_id = Ident::with_empty_ctxt(sym::test);
|
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(
|
|
|
|
ecx.path(sp, vec![
|
|
|
|
test_id, ecx.ident_of("test_main_static")
|
|
|
|
]));
|
|
|
|
|
|
|
|
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,
|
2018-07-21 01:04:02 +00:00
|
|
|
vec![mk_tests_slice(cx)]);
|
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
|
|
|
|
2015-01-22 02:13:08 +00:00
|
|
|
// #![main]
|
2019-05-22 02:42:23 +00:00
|
|
|
let main_meta = ecx.meta_word(sp, sym::main);
|
2019-07-30 18:12:52 +00:00
|
|
|
let main_attr = ecx.attribute(main_meta);
|
2018-07-21 01:04:02 +00:00
|
|
|
|
|
|
|
// extern crate test as test_gensym
|
|
|
|
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
|
|
|
));
|
|
|
|
|
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 {
|
|
|
|
Some(sym) => Ident::new(sym, sp),
|
|
|
|
None => Ident::from_str_and_span("main", sp).gensym(),
|
|
|
|
};
|
2018-07-21 01:04:02 +00:00
|
|
|
|
2017-05-12 18:05:39 +00:00
|
|
|
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,
|
2018-01-29 05:12:09 +00:00
|
|
|
vis: dummy_spanned(ast::VisibilityKind::Public),
|
2017-07-11 00:44:46 +00:00
|
|
|
span: sp,
|
|
|
|
tokens: None,
|
2017-05-12 18:05:39 +00:00
|
|
|
})
|
2013-08-15 06:06:33 +00:00
|
|
|
|
2012-12-28 01:53:04 +00:00
|
|
|
}
|
|
|
|
|
2016-11-16 08:21:52 +00:00
|
|
|
fn path_name_i(idents: &[Ident]) -> String {
|
2018-03-17 15:28:44 +00:00
|
|
|
let mut path_name = "".to_string();
|
|
|
|
let mut idents_iter = idents.iter().peekable();
|
|
|
|
while let Some(ident) = idents_iter.next() {
|
2018-05-26 12:12:38 +00:00
|
|
|
path_name.push_str(&ident.as_str());
|
2018-07-27 11:20:13 +00:00
|
|
|
if idents_iter.peek().is_some() {
|
2018-03-17 15:28:44 +00:00
|
|
|
path_name.push_str("::")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
path_name
|
2016-03-29 09:12:01 +00:00
|
|
|
}
|
|
|
|
|
2018-07-21 01:04:02 +00:00
|
|
|
/// Creates a slice containing every test like so:
|
|
|
|
/// &[path::to::test1, path::to::test2]
|
2019-02-06 17:33:01 +00:00
|
|
|
fn mk_tests_slice(cx: &TestCtxt<'_>) -> 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;
|
|
|
|
|
|
|
|
ecx.expr_vec_slice(DUMMY_SP,
|
|
|
|
cx.test_cases.iter().map(|test| {
|
|
|
|
ecx.expr_addr_of(test.span,
|
|
|
|
ecx.expr_path(ecx.path(test.span, visible_path(cx, &test.path))))
|
|
|
|
}).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
|
|
|
/// Creates a path from the top-level __test module to the test via __test_reexports
|
2019-02-06 17:33:01 +00:00
|
|
|
fn visible_path(cx: &TestCtxt<'_>, path: &[Ident]) -> Vec<Ident>{
|
2018-02-11 14:28:47 +00:00
|
|
|
let mut visible_path = vec![];
|
|
|
|
match cx.toplevel_reexport {
|
|
|
|
Some(id) => visible_path.push(id),
|
2014-08-31 03:36:16 +00:00
|
|
|
None => {
|
2018-07-21 01:04:02 +00:00
|
|
|
cx.span_diagnostic.bug("expected to find top-level re-export name, but found None");
|
2018-02-23 01:09:10 +00:00
|
|
|
}
|
2018-07-21 01:04:02 +00:00
|
|
|
}
|
|
|
|
visible_path.extend_from_slice(path);
|
|
|
|
visible_path
|
|
|
|
}
|
2018-02-08 22:16:39 +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
|
|
|
}
|