Skip to content

Commit

Permalink
Neaten up fold_crate.
Browse files Browse the repository at this point in the history
  • Loading branch information
nnethercote committed Feb 5, 2019
1 parent 8909f70 commit 4730953
Showing 1 changed file with 15 additions and 24 deletions.
39 changes: 15 additions & 24 deletions src/libsyntax/fold.rs
Expand Up @@ -980,38 +980,29 @@ pub fn noop_fold_mod<T: Folder>(Mod {inner, items, inline}: Mod, folder: &mut T)

pub fn noop_fold_crate<T: Folder>(Crate {module, attrs, span}: Crate,
folder: &mut T) -> Crate {
let mut items = folder.fold_item(P(Item {
let item = P(Item {
ident: keywords::Invalid.ident(),
attrs,
id: DUMMY_NODE_ID,
vis: respan(span.shrink_to_lo(), VisibilityKind::Public),
span,
node: ItemKind::Mod(module),
tokens: None,
})).into_iter();

let (module, attrs, span) = match items.next() {
Some(item) => {
assert!(items.next().is_none(),
"a crate cannot expand to more than one item");
item.and_then(|Item { attrs, span, node, .. }| {
match node {
ItemKind::Mod(m) => (m, attrs, span),
_ => panic!("fold converted a module to not a module"),
}
})
});
let items = folder.fold_item(item);

let len = items.len();
if len == 0 {
let module = Mod { inner: span, items: vec![], inline: true };
Crate { module, attrs: vec![], span }
} else if len == 1 {
let Item { attrs, span, node, .. } = items.into_iter().next().unwrap().into_inner();
match node {
ItemKind::Mod(module) => Crate { module, attrs, span },
_ => panic!("fold converted a module to not a module"),
}
None => (Mod {
inner: span,
items: vec![],
inline: true,
}, vec![], span)
};

Crate {
module,
attrs,
span,
} else {
panic!("a crate cannot expand to more than one item");
}
}

Expand Down

0 comments on commit 4730953

Please sign in to comment.