1
Fork 0

ast: Stop using Mod in Crate

Crate root is sufficiently different from `mod` items, at least at syntactic level.

Also remove customization point for "`mod` item or crate root" from AST visitors.
This commit is contained in:
Vadim Petrochenkov 2021-02-14 21:14:12 +03:00
parent d1462d8558
commit eb65f15c78
23 changed files with 114 additions and 179 deletions

View file

@ -91,7 +91,7 @@ pub fn inject(
}
let decls = mk_decls(&mut krate, &mut cx, &macros);
krate.module.items.push(decls);
krate.items.push(decls);
krate
}

View file

@ -44,7 +44,7 @@ pub fn inject(
// .rev() to preserve ordering above in combination with insert(0, ...)
for &name in names.iter().rev() {
let ident = if rust_2018 { Ident::new(name, span) } else { Ident::new(name, call_site) };
krate.module.items.insert(
krate.items.insert(
0,
cx.item(
span,
@ -79,7 +79,7 @@ pub fn inject(
})),
);
krate.module.items.insert(0, use_item);
krate.items.insert(0, use_item);
krate
}

View file

@ -89,7 +89,7 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> {
noop_visit_crate(c, self);
// Create a main function to run our tests
c.module.items.push(mk_main(&mut self.cx));
c.items.push(mk_main(&mut self.cx));
}
fn flat_map_item(&mut self, i: P<ast::Item>) -> SmallVec<[P<ast::Item>; 1]> {
@ -103,9 +103,13 @@ impl<'a> MutVisitor for TestHarnessGenerator<'a> {
// We don't want to recurse into anything other than mods, since
// mods or tests inside of functions will break things
if let ast::ItemKind::Mod(mut module) = item.kind {
if let ast::ItemKind::Mod(..) = item.kind {
let tests = mem::take(&mut self.tests);
noop_visit_mod(&mut module, self);
noop_visit_item_kind(&mut item.kind, self);
let module = match item.kind {
ast::ItemKind::Mod(module) => module,
_ => unreachable!(),
};
let mut tests = mem::replace(&mut self.tests, tests);
if !tests.is_empty() {