syntax: fix fallout of merging ast::ViewItem into ast::Item.

This commit is contained in:
Eduard Burtescu 2015-01-13 17:30:17 +02:00
parent 38ac9e3984
commit 7cece8725b
15 changed files with 201 additions and 417 deletions

View file

@ -107,7 +107,6 @@ pub fn path_to_string<PI: Iterator<Item=PathElem>>(path: PI) -> String {
#[derive(Copy, Show)] #[derive(Copy, Show)]
pub enum Node<'ast> { pub enum Node<'ast> {
NodeItem(&'ast Item), NodeItem(&'ast Item),
NodeViewItem(&'ast ViewItem),
NodeForeignItem(&'ast ForeignItem), NodeForeignItem(&'ast ForeignItem),
NodeTraitItem(&'ast TraitItem), NodeTraitItem(&'ast TraitItem),
NodeImplItem(&'ast ImplItem), NodeImplItem(&'ast ImplItem),
@ -134,7 +133,6 @@ enum MapEntry<'ast> {
/// All the node types, with a parent ID. /// All the node types, with a parent ID.
EntryItem(NodeId, &'ast Item), EntryItem(NodeId, &'ast Item),
EntryViewItem(NodeId, &'ast ViewItem),
EntryForeignItem(NodeId, &'ast ForeignItem), EntryForeignItem(NodeId, &'ast ForeignItem),
EntryTraitItem(NodeId, &'ast TraitItem), EntryTraitItem(NodeId, &'ast TraitItem),
EntryImplItem(NodeId, &'ast ImplItem), EntryImplItem(NodeId, &'ast ImplItem),
@ -169,7 +167,6 @@ impl<'ast> MapEntry<'ast> {
fn from_node(p: NodeId, node: Node<'ast>) -> MapEntry<'ast> { fn from_node(p: NodeId, node: Node<'ast>) -> MapEntry<'ast> {
match node { match node {
NodeItem(n) => EntryItem(p, n), NodeItem(n) => EntryItem(p, n),
NodeViewItem(n) => EntryViewItem(p, n),
NodeForeignItem(n) => EntryForeignItem(p, n), NodeForeignItem(n) => EntryForeignItem(p, n),
NodeTraitItem(n) => EntryTraitItem(p, n), NodeTraitItem(n) => EntryTraitItem(p, n),
NodeImplItem(n) => EntryImplItem(p, n), NodeImplItem(n) => EntryImplItem(p, n),
@ -188,7 +185,6 @@ impl<'ast> MapEntry<'ast> {
fn parent(self) -> Option<NodeId> { fn parent(self) -> Option<NodeId> {
Some(match self { Some(match self {
EntryItem(id, _) => id, EntryItem(id, _) => id,
EntryViewItem(id, _) => id,
EntryForeignItem(id, _) => id, EntryForeignItem(id, _) => id,
EntryTraitItem(id, _) => id, EntryTraitItem(id, _) => id,
EntryImplItem(id, _) => id, EntryImplItem(id, _) => id,
@ -208,7 +204,6 @@ impl<'ast> MapEntry<'ast> {
fn to_node(self) -> Option<Node<'ast>> { fn to_node(self) -> Option<Node<'ast>> {
Some(match self { Some(match self {
EntryItem(_, n) => NodeItem(n), EntryItem(_, n) => NodeItem(n),
EntryViewItem(_, n) => NodeViewItem(n),
EntryForeignItem(_, n) => NodeForeignItem(n), EntryForeignItem(_, n) => NodeForeignItem(n),
EntryTraitItem(_, n) => NodeTraitItem(n), EntryTraitItem(_, n) => NodeTraitItem(n),
EntryImplItem(_, n) => NodeImplItem(n), EntryImplItem(_, n) => NodeImplItem(n),
@ -341,13 +336,6 @@ impl<'ast> Map<'ast> {
} }
} }
pub fn expect_view_item(&self, id: NodeId) -> &'ast ViewItem {
match self.find(id) {
Some(NodeViewItem(view_item)) => view_item,
_ => panic!("expected view item, found {}", self.node_to_string(id))
}
}
pub fn expect_struct(&self, id: NodeId) -> &'ast StructDef { pub fn expect_struct(&self, id: NodeId) -> &'ast StructDef {
match self.find(id) { match self.find(id) {
Some(NodeItem(i)) => { Some(NodeItem(i)) => {
@ -533,7 +521,6 @@ impl<'ast> Map<'ast> {
pub fn opt_span(&self, id: NodeId) -> Option<Span> { pub fn opt_span(&self, id: NodeId) -> Option<Span> {
let sp = match self.find(id) { let sp = match self.find(id) {
Some(NodeItem(item)) => item.span, Some(NodeItem(item)) => item.span,
Some(NodeViewItem(item)) => item.span,
Some(NodeForeignItem(foreign_item)) => foreign_item.span, Some(NodeForeignItem(foreign_item)) => foreign_item.span,
Some(NodeTraitItem(trait_method)) => { Some(NodeTraitItem(trait_method)) => {
match *trait_method { match *trait_method {
@ -826,11 +813,6 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
self.parent = parent; self.parent = parent;
} }
fn visit_view_item(&mut self, item: &'ast ViewItem) {
self.insert(item.id(), NodeViewItem(item));
visit::walk_view_item(self, item);
}
fn visit_pat(&mut self, pat: &'ast Pat) { fn visit_pat(&mut self, pat: &'ast Pat) {
self.insert(pat.id, match pat.node { self.insert(pat.id, match pat.node {
// Note: this is at least *potentially* a pattern... // Note: this is at least *potentially* a pattern...
@ -904,7 +886,6 @@ pub fn map_crate<'ast, F: FoldOps>(forest: &'ast mut Forest, fold_ops: F) -> Map
let krate = mem::replace(&mut forest.krate, Crate { let krate = mem::replace(&mut forest.krate, Crate {
module: Mod { module: Mod {
inner: DUMMY_SP, inner: DUMMY_SP,
view_items: vec![],
items: vec![], items: vec![],
}, },
attrs: vec![], attrs: vec![],
@ -1036,7 +1017,6 @@ impl<'a> NodePrinter for pprust::State<'a> {
fn print_node(&mut self, node: &Node) -> IoResult<()> { fn print_node(&mut self, node: &Node) -> IoResult<()> {
match *node { match *node {
NodeItem(a) => self.print_item(&*a), NodeItem(a) => self.print_item(&*a),
NodeViewItem(a) => self.print_view_item(&*a),
NodeForeignItem(a) => self.print_foreign_item(&*a), NodeForeignItem(a) => self.print_foreign_item(&*a),
NodeTraitItem(a) => self.print_trait_method(&*a), NodeTraitItem(a) => self.print_trait_method(&*a),
NodeImplItem(a) => self.print_impl_item(&*a), NodeImplItem(a) => self.print_impl_item(&*a),
@ -1065,6 +1045,8 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
Some(NodeItem(item)) => { Some(NodeItem(item)) => {
let path_str = map.path_to_str_with_ident(id, item.ident); let path_str = map.path_to_str_with_ident(id, item.ident);
let item_str = match item.node { let item_str = match item.node {
ItemExternCrate(..) => "extern crate",
ItemUse(..) => "use",
ItemStatic(..) => "static", ItemStatic(..) => "static",
ItemConst(..) => "const", ItemConst(..) => "const",
ItemFn(..) => "fn", ItemFn(..) => "fn",
@ -1079,9 +1061,6 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
}; };
format!("{} {}{}", item_str, path_str, id_str) format!("{} {}{}", item_str, path_str, id_str)
} }
Some(NodeViewItem(item)) => {
format!("view item {}{}", pprust::view_item_to_string(&*item), id_str)
}
Some(NodeForeignItem(item)) => { Some(NodeForeignItem(item)) => {
let path_str = map.path_to_str_with_ident(id, item.ident); let path_str = map.path_to_str_with_ident(id, item.ident);
format!("foreign item {}{}", path_str, id_str) format!("foreign item {}{}", path_str, id_str)

View file

@ -410,37 +410,6 @@ impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> {
visit::walk_mod(self, module) visit::walk_mod(self, module)
} }
fn visit_view_item(&mut self, view_item: &ViewItem) {
if !self.pass_through_items {
if self.visited_outermost {
return;
} else {
self.visited_outermost = true;
}
}
match view_item.node {
ViewItemExternCrate(_, _, node_id) => {
self.operation.visit_id(node_id)
}
ViewItemUse(ref view_path) => {
match view_path.node {
ViewPathSimple(_, _, node_id) |
ViewPathGlob(_, node_id) => {
self.operation.visit_id(node_id)
}
ViewPathList(_, ref paths, node_id) => {
self.operation.visit_id(node_id);
for path in paths.iter() {
self.operation.visit_id(path.node.id())
}
}
}
}
}
visit::walk_view_item(self, view_item);
self.visited_outermost = false;
}
fn visit_foreign_item(&mut self, foreign_item: &ForeignItem) { fn visit_foreign_item(&mut self, foreign_item: &ForeignItem) {
self.operation.visit_id(foreign_item.id); self.operation.visit_id(foreign_item.id);
visit::walk_foreign_item(self, foreign_item) visit::walk_foreign_item(self, foreign_item)
@ -456,10 +425,24 @@ impl<'a, 'v, O: IdVisitingOperation> Visitor<'v> for IdVisitor<'a, O> {
} }
self.operation.visit_id(item.id); self.operation.visit_id(item.id);
if let ItemEnum(ref enum_definition, _) = item.node { match item.node {
for variant in enum_definition.variants.iter() { ItemUse(ref view_path) => {
self.operation.visit_id(variant.node.id) match view_path.node {
ViewPathSimple(_, _) |
ViewPathGlob(_) => {}
ViewPathList(_, ref paths) => {
for path in paths.iter() {
self.operation.visit_id(path.node.id())
}
}
}
} }
ItemEnum(ref enum_definition, _) => {
for variant in enum_definition.variants.iter() {
self.operation.visit_id(variant.node.id)
}
}
_ => {}
} }
visit::walk_item(self, item); visit::walk_item(self, item);
@ -662,37 +645,6 @@ pub fn walk_pat<F>(pat: &Pat, mut it: F) -> bool where F: FnMut(&Pat) -> bool {
walk_pat_(pat, &mut it) walk_pat_(pat, &mut it)
} }
pub trait EachViewItem {
fn each_view_item<F>(&self, f: F) -> bool where F: FnMut(&ast::ViewItem) -> bool;
}
struct EachViewItemData<F> where F: FnMut(&ast::ViewItem) -> bool {
callback: F,
}
impl<'v, F> Visitor<'v> for EachViewItemData<F> where F: FnMut(&ast::ViewItem) -> bool {
fn visit_view_item(&mut self, view_item: &ast::ViewItem) {
let _ = (self.callback)(view_item);
}
}
impl EachViewItem for ast::Crate {
fn each_view_item<F>(&self, f: F) -> bool where F: FnMut(&ast::ViewItem) -> bool {
let mut visit = EachViewItemData {
callback: f,
};
visit::walk_crate(&mut visit, self);
true
}
}
pub fn view_path_id(p: &ViewPath) -> NodeId {
match p.node {
ViewPathSimple(_, _, id) | ViewPathGlob(_, id)
| ViewPathList(_, _, id) => id
}
}
/// Returns true if the given struct def is tuple-like; i.e. that its fields /// Returns true if the given struct def is tuple-like; i.e. that its fields
/// are unnamed. /// are unnamed.
pub fn struct_def_is_tuple_like(struct_def: &ast::StructDef) -> bool { pub fn struct_def_is_tuple_like(struct_def: &ast::StructDef) -> bool {

View file

@ -63,28 +63,13 @@ pub fn strip_items<F>(krate: ast::Crate, in_cfg: F) -> ast::Crate where
ctxt.fold_crate(krate) ctxt.fold_crate(krate)
} }
fn filter_view_item<F>(cx: &mut Context<F>,
view_item: ast::ViewItem)
-> Option<ast::ViewItem> where
F: FnMut(&[ast::Attribute]) -> bool
{
if view_item_in_cfg(cx, &view_item) {
Some(view_item)
} else {
None
}
}
fn fold_mod<F>(cx: &mut Context<F>, fn fold_mod<F>(cx: &mut Context<F>,
ast::Mod {inner, ast::Mod {inner, items}: ast::Mod)
view_items, items}: ast::Mod) -> ast::Mod where -> ast::Mod where
F: FnMut(&[ast::Attribute]) -> bool F: FnMut(&[ast::Attribute]) -> bool
{ {
ast::Mod { ast::Mod {
inner: inner, inner: inner,
view_items: view_items.into_iter().filter_map(|a| {
filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
}).collect(),
items: items.into_iter().flat_map(|a| { items: items.into_iter().flat_map(|a| {
cx.fold_item(a).into_iter() cx.fold_item(a).into_iter()
}).collect() }).collect()
@ -104,15 +89,12 @@ fn filter_foreign_item<F>(cx: &mut Context<F>,
} }
fn fold_foreign_mod<F>(cx: &mut Context<F>, fn fold_foreign_mod<F>(cx: &mut Context<F>,
ast::ForeignMod {abi, view_items, items}: ast::ForeignMod) ast::ForeignMod {abi, items}: ast::ForeignMod)
-> ast::ForeignMod where -> ast::ForeignMod where
F: FnMut(&[ast::Attribute]) -> bool F: FnMut(&[ast::Attribute]) -> bool
{ {
ast::ForeignMod { ast::ForeignMod {
abi: abi, abi: abi,
view_items: view_items.into_iter().filter_map(|a| {
filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
}).collect(),
items: items.into_iter() items: items.into_iter()
.filter_map(|a| filter_foreign_item(cx, a)) .filter_map(|a| filter_foreign_item(cx, a))
.collect() .collect()
@ -216,18 +198,14 @@ fn retain_stmt<F>(cx: &mut Context<F>, stmt: &ast::Stmt) -> bool where
fn fold_block<F>(cx: &mut Context<F>, b: P<ast::Block>) -> P<ast::Block> where fn fold_block<F>(cx: &mut Context<F>, b: P<ast::Block>) -> P<ast::Block> where
F: FnMut(&[ast::Attribute]) -> bool F: FnMut(&[ast::Attribute]) -> bool
{ {
b.map(|ast::Block {id, view_items, stmts, expr, rules, span}| { b.map(|ast::Block {id, stmts, expr, rules, span}| {
let resulting_stmts: Vec<P<ast::Stmt>> = let resulting_stmts: Vec<P<ast::Stmt>> =
stmts.into_iter().filter(|a| retain_stmt(cx, &**a)).collect(); stmts.into_iter().filter(|a| retain_stmt(cx, &**a)).collect();
let resulting_stmts = resulting_stmts.into_iter() let resulting_stmts = resulting_stmts.into_iter()
.flat_map(|stmt| cx.fold_stmt(stmt).into_iter()) .flat_map(|stmt| cx.fold_stmt(stmt).into_iter())
.collect(); .collect();
let filtered_view_items = view_items.into_iter().filter_map(|a| {
filter_view_item(cx, a).map(|x| cx.fold_view_item(x))
}).collect();
ast::Block { ast::Block {
id: id, id: id,
view_items: filtered_view_items,
stmts: resulting_stmts, stmts: resulting_stmts,
expr: expr.map(|x| cx.fold_expr(x)), expr: expr.map(|x| cx.fold_expr(x)),
rules: rules, rules: rules,
@ -267,12 +245,6 @@ fn foreign_item_in_cfg<F>(cx: &mut Context<F>, item: &ast::ForeignItem) -> bool
return (cx.in_cfg)(item.attrs.as_slice()); return (cx.in_cfg)(item.attrs.as_slice());
} }
fn view_item_in_cfg<F>(cx: &mut Context<F>, item: &ast::ViewItem) -> bool where
F: FnMut(&[ast::Attribute]) -> bool
{
return (cx.in_cfg)(item.attrs.as_slice());
}
fn trait_method_in_cfg<F>(cx: &mut Context<F>, meth: &ast::TraitItem) -> bool where fn trait_method_in_cfg<F>(cx: &mut Context<F>, meth: &ast::TraitItem) -> bool where
F: FnMut(&[ast::Attribute]) -> bool F: FnMut(&[ast::Attribute]) -> bool
{ {

View file

@ -97,7 +97,6 @@ pub trait AstBuilder {
expr: Option<P<ast::Expr>>) -> P<ast::Block>; expr: Option<P<ast::Expr>>) -> P<ast::Block>;
fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block>; fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block>;
fn block_all(&self, span: Span, fn block_all(&self, span: Span,
view_items: Vec<ast::ViewItem>,
stmts: Vec<P<ast::Stmt>>, stmts: Vec<P<ast::Stmt>>,
expr: Option<P<ast::Expr>>) -> P<ast::Block>; expr: Option<P<ast::Expr>>) -> P<ast::Block>;
@ -242,7 +241,7 @@ pub trait AstBuilder {
fn item_mod(&self, span: Span, inner_span: Span, fn item_mod(&self, span: Span, inner_span: Span,
name: Ident, attrs: Vec<ast::Attribute>, name: Ident, attrs: Vec<ast::Attribute>,
vi: Vec<ast::ViewItem> , items: Vec<P<ast::Item>> ) -> P<ast::Item>; items: Vec<P<ast::Item>>) -> P<ast::Item>;
fn item_static(&self, fn item_static(&self,
span: Span, span: Span,
@ -280,15 +279,15 @@ pub trait AstBuilder {
value: ast::Lit_) value: ast::Lit_)
-> P<ast::MetaItem>; -> P<ast::MetaItem>;
fn view_use(&self, sp: Span, fn item_use(&self, sp: Span,
vis: ast::Visibility, vp: P<ast::ViewPath>) -> ast::ViewItem; vis: ast::Visibility, vp: P<ast::ViewPath>) -> P<ast::Item>;
fn view_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> ast::ViewItem; fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item>;
fn view_use_simple_(&self, sp: Span, vis: ast::Visibility, fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
ident: ast::Ident, path: ast::Path) -> ast::ViewItem; ident: ast::Ident, path: ast::Path) -> P<ast::Item>;
fn view_use_list(&self, sp: Span, vis: ast::Visibility, fn item_use_list(&self, sp: Span, vis: ast::Visibility,
path: Vec<ast::Ident> , imports: &[ast::Ident]) -> ast::ViewItem; path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item>;
fn view_use_glob(&self, sp: Span, fn item_use_glob(&self, sp: Span,
vis: ast::Visibility, path: Vec<ast::Ident> ) -> ast::ViewItem; vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item>;
} }
impl<'a> AstBuilder for ExtCtxt<'a> { impl<'a> AstBuilder for ExtCtxt<'a> {
@ -519,7 +518,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
fn block(&self, span: Span, stmts: Vec<P<ast::Stmt>>, fn block(&self, span: Span, stmts: Vec<P<ast::Stmt>>,
expr: Option<P<Expr>>) -> P<ast::Block> { expr: Option<P<Expr>>) -> P<ast::Block> {
self.block_all(span, Vec::new(), stmts, expr) self.block_all(span, stmts, expr)
} }
fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> P<ast::Stmt> { fn stmt_item(&self, sp: Span, item: P<ast::Item>) -> P<ast::Stmt> {
@ -528,15 +527,13 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
} }
fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> { fn block_expr(&self, expr: P<ast::Expr>) -> P<ast::Block> {
self.block_all(expr.span, Vec::new(), Vec::new(), Some(expr)) self.block_all(expr.span, Vec::new(), Some(expr))
} }
fn block_all(&self, fn block_all(&self,
span: Span, span: Span,
view_items: Vec<ast::ViewItem>,
stmts: Vec<P<ast::Stmt>>, stmts: Vec<P<ast::Stmt>>,
expr: Option<P<ast::Expr>>) -> P<ast::Block> { expr: Option<P<ast::Expr>>) -> P<ast::Block> {
P(ast::Block { P(ast::Block {
view_items: view_items,
stmts: stmts, stmts: stmts,
expr: expr, expr: expr,
id: ast::DUMMY_NODE_ID, id: ast::DUMMY_NODE_ID,
@ -1031,16 +1028,14 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
} }
fn item_mod(&self, span: Span, inner_span: Span, name: Ident, fn item_mod(&self, span: Span, inner_span: Span, name: Ident,
attrs: Vec<ast::Attribute> , attrs: Vec<ast::Attribute>,
vi: Vec<ast::ViewItem> , items: Vec<P<ast::Item>>) -> P<ast::Item> {
items: Vec<P<ast::Item>> ) -> P<ast::Item> {
self.item( self.item(
span, span,
name, name,
attrs, attrs,
ast::ItemMod(ast::Mod { ast::ItemMod(ast::Mod {
inner: inner_span, inner: inner_span,
view_items: vi,
items: items, items: items,
}) })
) )
@ -1101,47 +1096,47 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
P(respan(sp, ast::MetaNameValue(name, respan(sp, value)))) P(respan(sp, ast::MetaNameValue(name, respan(sp, value))))
} }
fn view_use(&self, sp: Span, fn item_use(&self, sp: Span,
vis: ast::Visibility, vp: P<ast::ViewPath>) -> ast::ViewItem { vis: ast::Visibility, vp: P<ast::ViewPath>) -> P<ast::Item> {
ast::ViewItem { P(ast::Item {
node: ast::ViewItemUse(vp), id: ast::DUMMY_NODE_ID,
attrs: Vec::new(), ident: special_idents::invalid,
attrs: vec![],
node: ast::ItemUse(vp),
vis: vis, vis: vis,
span: sp span: sp
} })
} }
fn view_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> ast::ViewItem { fn item_use_simple(&self, sp: Span, vis: ast::Visibility, path: ast::Path) -> P<ast::Item> {
let last = path.segments.last().unwrap().identifier; let last = path.segments.last().unwrap().identifier;
self.view_use_simple_(sp, vis, last, path) self.item_use_simple_(sp, vis, last, path)
} }
fn view_use_simple_(&self, sp: Span, vis: ast::Visibility, fn item_use_simple_(&self, sp: Span, vis: ast::Visibility,
ident: ast::Ident, path: ast::Path) -> ast::ViewItem { ident: ast::Ident, path: ast::Path) -> P<ast::Item> {
self.view_use(sp, vis, self.item_use(sp, vis,
P(respan(sp, P(respan(sp,
ast::ViewPathSimple(ident, ast::ViewPathSimple(ident,
path, path))))
ast::DUMMY_NODE_ID))))
} }
fn view_use_list(&self, sp: Span, vis: ast::Visibility, fn item_use_list(&self, sp: Span, vis: ast::Visibility,
path: Vec<ast::Ident> , imports: &[ast::Ident]) -> ast::ViewItem { path: Vec<ast::Ident>, imports: &[ast::Ident]) -> P<ast::Item> {
let imports = imports.iter().map(|id| { let imports = imports.iter().map(|id| {
respan(sp, ast::PathListIdent { name: *id, id: ast::DUMMY_NODE_ID }) respan(sp, ast::PathListIdent { name: *id, id: ast::DUMMY_NODE_ID })
}).collect(); }).collect();
self.view_use(sp, vis, self.item_use(sp, vis,
P(respan(sp, P(respan(sp,
ast::ViewPathList(self.path(sp, path), ast::ViewPathList(self.path(sp, path),
imports, imports))))
ast::DUMMY_NODE_ID))))
} }
fn view_use_glob(&self, sp: Span, fn item_use_glob(&self, sp: Span,
vis: ast::Visibility, path: Vec<ast::Ident> ) -> ast::ViewItem { vis: ast::Visibility, path: Vec<ast::Ident>) -> P<ast::Item> {
self.view_use(sp, vis, self.item_use(sp, vis,
P(respan(sp, P(respan(sp,
ast::ViewPathGlob(self.path(sp, path), ast::DUMMY_NODE_ID)))) ast::ViewPathGlob(self.path(sp, path)))))
} }
} }

View file

@ -1073,7 +1073,7 @@ impl<'a> MethodDef<'a> {
// <delegated expression referring to __self0_vi, et al.> // <delegated expression referring to __self0_vi, et al.>
// } // }
let arm_expr = cx.expr_block( let arm_expr = cx.expr_block(
cx.block_all(sp, Vec::new(), index_let_stmts, Some(arm_expr))); cx.block_all(sp, index_let_stmts, Some(arm_expr)));
// Builds arm: // Builds arm:
// _ => { let __self0_vi = ...; // _ => { let __self0_vi = ...;

View file

@ -206,7 +206,6 @@ pub fn expand_expr(e: P<ast::Expr>, fld: &mut MacroExpander) -> P<ast::Expr> {
// wrap the if-let expr in a block // wrap the if-let expr in a block
let span = els.span; let span = els.span;
let blk = P(ast::Block { let blk = P(ast::Block {
view_items: vec![],
stmts: vec![], stmts: vec![],
expr: Some(P(els)), expr: Some(P(els)),
id: ast::DUMMY_NODE_ID, id: ast::DUMMY_NODE_ID,
@ -799,8 +798,7 @@ pub fn expand_block(blk: P<Block>, fld: &mut MacroExpander) -> P<Block> {
// expand the elements of a block. // expand the elements of a block.
pub fn expand_block_elts(b: P<Block>, fld: &mut MacroExpander) -> P<Block> { pub fn expand_block_elts(b: P<Block>, fld: &mut MacroExpander) -> P<Block> {
b.map(|Block {id, view_items, stmts, expr, rules, span}| { b.map(|Block {id, stmts, expr, rules, span}| {
let new_view_items = view_items.into_iter().map(|x| fld.fold_view_item(x)).collect();
let new_stmts = stmts.into_iter().flat_map(|x| { let new_stmts = stmts.into_iter().flat_map(|x| {
// perform all pending renames // perform all pending renames
let renamed_stmt = { let renamed_stmt = {
@ -821,7 +819,6 @@ pub fn expand_block_elts(b: P<Block>, fld: &mut MacroExpander) -> P<Block> {
}); });
Block { Block {
id: fld.new_id(id), id: fld.new_id(id),
view_items: new_view_items,
stmts: new_stmts, stmts: new_stmts,
expr: new_expr, expr: new_expr,
rules: rules, rules: rules,

View file

@ -352,18 +352,11 @@ pub mod rt {
impl<'a> ExtParseUtils for ExtCtxt<'a> { impl<'a> ExtParseUtils for ExtCtxt<'a> {
fn parse_item(&self, s: String) -> P<ast::Item> { fn parse_item(&self, s: String) -> P<ast::Item> {
let res = parse::parse_item_from_source_str( parse::parse_item_from_source_str(
"<quote expansion>".to_string(), "<quote expansion>".to_string(),
s, s,
self.cfg(), self.cfg(),
self.parse_sess()); self.parse_sess()).expect("parse error")
match res {
Some(ast) => ast,
None => {
error!("parse error");
panic!()
}
}
} }
fn parse_stmt(&self, s: String) -> P<ast::Stmt> { fn parse_stmt(&self, s: String) -> P<ast::Stmt> {
@ -767,7 +760,6 @@ fn expand_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::TokenTree])
vector.extend(mk_tts(cx, &tts[]).into_iter()); vector.extend(mk_tts(cx, &tts[]).into_iter());
let block = cx.expr_block( let block = cx.expr_block(
cx.block_all(sp, cx.block_all(sp,
Vec::new(),
vector, vector,
Some(cx.expr_ident(sp, id_ext("tt"))))); Some(cx.expr_ident(sp, id_ext("tt")))));
@ -778,18 +770,18 @@ fn expand_wrapper(cx: &ExtCtxt,
sp: Span, sp: Span,
cx_expr: P<ast::Expr>, cx_expr: P<ast::Expr>,
expr: P<ast::Expr>) -> P<ast::Expr> { expr: P<ast::Expr>) -> P<ast::Expr> {
let uses = [
&["syntax", "ext", "quote", "rt"],
].iter().map(|path| {
let path = path.iter().map(|s| s.to_string()).collect();
cx.view_use_glob(sp, ast::Inherited, ids_ext(path))
}).collect();
// Explicitly borrow to avoid moving from the invoker (#16992) // Explicitly borrow to avoid moving from the invoker (#16992)
let cx_expr_borrow = cx.expr_addr_of(sp, cx.expr_deref(sp, cx_expr)); let cx_expr_borrow = cx.expr_addr_of(sp, cx.expr_deref(sp, cx_expr));
let stmt_let_ext_cx = cx.stmt_let(sp, false, id_ext("ext_cx"), cx_expr_borrow); let stmt_let_ext_cx = cx.stmt_let(sp, false, id_ext("ext_cx"), cx_expr_borrow);
cx.expr_block(cx.block_all(sp, uses, vec!(stmt_let_ext_cx), Some(expr))) let stmts = [
&["syntax", "ext", "quote", "rt"],
].iter().map(|path| {
let path = path.iter().map(|s| s.to_string()).collect();
cx.stmt_item(sp, cx.item_use_glob(sp, ast::Inherited, ids_ext(path)))
}).chain(Some(stmt_let_ext_cx).into_iter()).collect();
cx.expr_block(cx.block_all(sp, stmts, Some(expr)))
} }
fn expand_parse_call(cx: &ExtCtxt, fn expand_parse_call(cx: &ExtCtxt,

View file

@ -226,22 +226,6 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {
} }
} }
fn visit_view_item(&mut self, i: &ast::ViewItem) {
match i.node {
ast::ViewItemUse(..) => {}
ast::ViewItemExternCrate(..) => {
for attr in i.attrs.iter() {
if attr.check_name("plugin") {
self.gate_feature("plugin", attr.span,
"compiler plugins are experimental \
and possibly buggy");
}
}
}
}
visit::walk_view_item(self, i)
}
fn visit_item(&mut self, i: &ast::Item) { fn visit_item(&mut self, i: &ast::Item) {
for attr in i.attrs.iter() { for attr in i.attrs.iter() {
if attr.name() == "thread_local" { if attr.name() == "thread_local" {
@ -260,6 +244,14 @@ impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {
} }
} }
match i.node { match i.node {
ast::ItemExternCrate(_) => {
if attr::contains_name(&i.attrs[], "plugin") {
self.gate_feature("plugin", i.span,
"compiler plugins are experimental \
and possibly buggy");
}
}
ast::ItemForeignMod(ref foreign_module) => { ast::ItemForeignMod(ref foreign_module) => {
if attr::contains_name(&i.attrs[], "link_args") { if attr::contains_name(&i.attrs[], "link_args") {
self.gate_feature("link_args", i.span, self.gate_feature("link_args", i.span,

View file

@ -78,10 +78,6 @@ pub trait Folder : Sized {
noop_fold_view_path(view_path, self) noop_fold_view_path(view_path, self)
} }
fn fold_view_item(&mut self, vi: ViewItem) -> ViewItem {
noop_fold_view_item(vi, self)
}
fn fold_foreign_item(&mut self, ni: P<ForeignItem>) -> P<ForeignItem> { fn fold_foreign_item(&mut self, ni: P<ForeignItem>) -> P<ForeignItem> {
noop_fold_foreign_item(ni, self) noop_fold_foreign_item(ni, self)
} }
@ -349,16 +345,13 @@ pub fn noop_fold_meta_items<T: Folder>(meta_items: Vec<P<MetaItem>>, fld: &mut T
pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<ViewPath> { pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<ViewPath> {
view_path.map(|Spanned {node, span}| Spanned { view_path.map(|Spanned {node, span}| Spanned {
node: match node { node: match node {
ViewPathSimple(ident, path, node_id) => { ViewPathSimple(ident, path) => {
let id = fld.new_id(node_id); ViewPathSimple(ident, fld.fold_path(path))
ViewPathSimple(ident, fld.fold_path(path), id)
} }
ViewPathGlob(path, node_id) => { ViewPathGlob(path) => {
let id = fld.new_id(node_id); ViewPathGlob(fld.fold_path(path))
ViewPathGlob(fld.fold_path(path), id)
} }
ViewPathList(path, path_list_idents, node_id) => { ViewPathList(path, path_list_idents) => {
let id = fld.new_id(node_id);
ViewPathList(fld.fold_path(path), ViewPathList(fld.fold_path(path),
path_list_idents.move_map(|path_list_ident| { path_list_idents.move_map(|path_list_ident| {
Spanned { Spanned {
@ -373,8 +366,7 @@ pub fn noop_fold_view_path<T: Folder>(view_path: P<ViewPath>, fld: &mut T) -> P<
}, },
span: fld.new_span(path_list_ident.span) span: fld.new_span(path_list_ident.span)
} }
}), }))
id)
} }
}, },
span: fld.new_span(span) span: fld.new_span(span)
@ -470,11 +462,10 @@ pub fn noop_fold_qpath<T: Folder>(qpath: P<QPath>, fld: &mut T) -> P<QPath> {
}) })
} }
pub fn noop_fold_foreign_mod<T: Folder>(ForeignMod {abi, view_items, items}: ForeignMod, pub fn noop_fold_foreign_mod<T: Folder>(ForeignMod {abi, items}: ForeignMod,
fld: &mut T) -> ForeignMod { fld: &mut T) -> ForeignMod {
ForeignMod { ForeignMod {
abi: abi, abi: abi,
view_items: view_items.move_map(|x| fld.fold_view_item(x)),
items: items.move_map(|x| fld.fold_foreign_item(x)), items: items.move_map(|x| fld.fold_foreign_item(x)),
} }
} }
@ -953,28 +944,9 @@ fn noop_fold_variant_arg<T: Folder>(VariantArg {id, ty}: VariantArg, folder: &mu
} }
} }
pub fn noop_fold_view_item<T: Folder>(ViewItem {node, attrs, vis, span}: ViewItem,
folder: &mut T) -> ViewItem {
ViewItem {
node: match node {
ViewItemExternCrate(ident, string, node_id) => {
ViewItemExternCrate(ident, string,
folder.new_id(node_id))
}
ViewItemUse(view_path) => {
ViewItemUse(folder.fold_view_path(view_path))
}
},
attrs: attrs.move_map(|a| folder.fold_attribute(a)),
vis: vis,
span: folder.new_span(span)
}
}
pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> { pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {
b.map(|Block {id, view_items, stmts, expr, rules, span}| Block { b.map(|Block {id, stmts, expr, rules, span}| Block {
id: folder.new_id(id), id: folder.new_id(id),
view_items: view_items.move_map(|x| folder.fold_view_item(x)),
stmts: stmts.into_iter().flat_map(|s| folder.fold_stmt(s).into_iter()).collect(), stmts: stmts.into_iter().flat_map(|s| folder.fold_stmt(s).into_iter()).collect(),
expr: expr.map(|x| folder.fold_expr(x)), expr: expr.map(|x| folder.fold_expr(x)),
rules: rules, rules: rules,
@ -984,6 +956,10 @@ pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {
pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ { pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ {
match i { match i {
ItemExternCrate(string) => ItemExternCrate(string),
ItemUse(view_path) => {
ItemUse(folder.fold_view_path(view_path))
}
ItemStatic(t, m, e) => { ItemStatic(t, m, e) => {
ItemStatic(folder.fold_ty(t), m, folder.fold_expr(e)) ItemStatic(folder.fold_ty(t), m, folder.fold_expr(e))
} }
@ -1103,10 +1079,9 @@ pub fn noop_fold_type_method<T: Folder>(m: TypeMethod, fld: &mut T) -> TypeMetho
} }
} }
pub fn noop_fold_mod<T: Folder>(Mod {inner, view_items, items}: Mod, folder: &mut T) -> Mod { pub fn noop_fold_mod<T: Folder>(Mod {inner, items}: Mod, folder: &mut T) -> Mod {
Mod { Mod {
inner: folder.new_span(inner), inner: folder.new_span(inner),
view_items: view_items.move_map(|x| folder.fold_view_item(x)),
items: items.into_iter().flat_map(|x| folder.fold_item(x).into_iter()).collect(), items: items.into_iter().flat_map(|x| folder.fold_item(x).into_iter()).collect(),
} }
} }
@ -1137,9 +1112,8 @@ pub fn noop_fold_crate<T: Folder>(Crate {module, attrs, config, mut exported_mac
} }
None => (ast::Mod { None => (ast::Mod {
inner: span, inner: span,
view_items: Vec::new(), items: vec![],
items: Vec::new(), }, vec![], span)
}, Vec::new(), span)
}; };
for def in exported_macros.iter_mut() { for def in exported_macros.iter_mut() {

View file

@ -757,11 +757,10 @@ mod test {
use attr::{first_attr_value_str_by_name, AttrMetaMethods}; use attr::{first_attr_value_str_by_name, AttrMetaMethods};
use parse::parser::Parser; use parse::parser::Parser;
use parse::token::{str_to_ident}; use parse::token::{str_to_ident};
use print::pprust::view_item_to_string; use print::pprust::item_to_string;
use ptr::P; use ptr::P;
use util::parser_testing::{string_to_tts, string_to_parser}; use util::parser_testing::{string_to_tts, string_to_parser};
use util::parser_testing::{string_to_expr, string_to_item}; use util::parser_testing::{string_to_expr, string_to_item, string_to_stmt};
use util::parser_testing::{string_to_stmt, string_to_view_item};
// produce a codemap::span // produce a codemap::span
fn sp(a: u32, b: u32) -> Span { fn sp(a: u32, b: u32) -> Span {
@ -1079,7 +1078,6 @@ mod test {
} }
}, },
P(ast::Block { P(ast::Block {
view_items: Vec::new(),
stmts: vec!(P(Spanned{ stmts: vec!(P(Spanned{
node: ast::StmtSemi(P(ast::Expr{ node: ast::StmtSemi(P(ast::Expr{
id: ast::DUMMY_NODE_ID, id: ast::DUMMY_NODE_ID,
@ -1111,25 +1109,25 @@ mod test {
#[test] fn parse_use() { #[test] fn parse_use() {
let use_s = "use foo::bar::baz;"; let use_s = "use foo::bar::baz;";
let vitem = string_to_view_item(use_s.to_string()); let vitem = string_to_item(use_s.to_string());
let vitem_s = view_item_to_string(&vitem); let vitem_s = item_to_string(&vitem);
assert_eq!(&vitem_s[], use_s); assert_eq!(&vitem_s[], use_s);
let use_s = "use foo::bar as baz;"; let use_s = "use foo::bar as baz;";
let vitem = string_to_view_item(use_s.to_string()); let vitem = string_to_item(use_s.to_string());
let vitem_s = view_item_to_string(&vitem); let vitem_s = item_to_string(&vitem);
assert_eq!(&vitem_s[], use_s); assert_eq!(&vitem_s[], use_s);
} }
#[test] fn parse_extern_crate() { #[test] fn parse_extern_crate() {
let ex_s = "extern crate foo;"; let ex_s = "extern crate foo;";
let vitem = string_to_view_item(ex_s.to_string()); let vitem = string_to_item(ex_s.to_string());
let vitem_s = view_item_to_string(&vitem); let vitem_s = item_to_string(&vitem);
assert_eq!(&vitem_s[], ex_s); assert_eq!(&vitem_s[], ex_s);
let ex_s = "extern crate \"foo\" as bar;"; let ex_s = "extern crate \"foo\" as bar;";
let vitem = string_to_view_item(ex_s.to_string()); let vitem = string_to_item(ex_s.to_string());
let vitem_s = view_item_to_string(&vitem); let vitem_s = item_to_string(&vitem);
assert_eq!(&vitem_s[], ex_s); assert_eq!(&vitem_s[], ex_s);
} }

View file

@ -337,10 +337,6 @@ pub fn item_to_string(i: &ast::Item) -> String {
$to_string(|s| s.print_item(i)) $to_string(|s| s.print_item(i))
} }
pub fn view_item_to_string(i: &ast::ViewItem) -> String {
$to_string(|s| s.print_view_item(i))
}
pub fn generics_to_string(generics: &ast::Generics) -> String { pub fn generics_to_string(generics: &ast::Generics) -> String {
$to_string(|s| s.print_generics(generics)) $to_string(|s| s.print_generics(generics))
} }
@ -638,9 +634,6 @@ impl<'a> State<'a> {
pub fn print_mod(&mut self, _mod: &ast::Mod, pub fn print_mod(&mut self, _mod: &ast::Mod,
attrs: &[ast::Attribute]) -> IoResult<()> { attrs: &[ast::Attribute]) -> IoResult<()> {
try!(self.print_inner_attributes(attrs)); try!(self.print_inner_attributes(attrs));
for vitem in _mod.view_items.iter() {
try!(self.print_view_item(vitem));
}
for item in _mod.items.iter() { for item in _mod.items.iter() {
try!(self.print_item(&**item)); try!(self.print_item(&**item));
} }
@ -650,9 +643,6 @@ impl<'a> State<'a> {
pub fn print_foreign_mod(&mut self, nmod: &ast::ForeignMod, pub fn print_foreign_mod(&mut self, nmod: &ast::ForeignMod,
attrs: &[ast::Attribute]) -> IoResult<()> { attrs: &[ast::Attribute]) -> IoResult<()> {
try!(self.print_inner_attributes(attrs)); try!(self.print_inner_attributes(attrs));
for vitem in nmod.view_items.iter() {
try!(self.print_view_item(vitem));
}
for item in nmod.items.iter() { for item in nmod.items.iter() {
try!(self.print_foreign_item(&**item)); try!(self.print_foreign_item(&**item));
} }
@ -809,6 +799,28 @@ impl<'a> State<'a> {
try!(self.print_outer_attributes(&item.attrs[])); try!(self.print_outer_attributes(&item.attrs[]));
try!(self.ann.pre(self, NodeItem(item))); try!(self.ann.pre(self, NodeItem(item)));
match item.node { match item.node {
ast::ItemExternCrate(ref optional_path) => {
try!(self.head(&visibility_qualified(item.vis,
"extern crate")[]));
for &(ref p, style) in optional_path.iter() {
try!(self.print_string(p.get(), style));
try!(space(&mut self.s));
try!(word(&mut self.s, "as"));
try!(space(&mut self.s));
}
try!(self.print_ident(item.ident));
try!(word(&mut self.s, ";"));
try!(self.end()); // end inner head-block
try!(self.end()); // end outer head-block
}
ast::ItemUse(ref vp) => {
try!(self.head(&visibility_qualified(item.vis,
"use")[]));
try!(self.print_view_path(&**vp));
try!(word(&mut self.s, ";"));
try!(self.end()); // end inner head-block
try!(self.end()); // end outer head-block
}
ast::ItemStatic(ref ty, m, ref expr) => { ast::ItemStatic(ref ty, m, ref expr) => {
try!(self.head(&visibility_qualified(item.vis, try!(self.head(&visibility_qualified(item.vis,
"static")[])); "static")[]));
@ -1380,9 +1392,6 @@ impl<'a> State<'a> {
try!(self.print_inner_attributes(attrs)); try!(self.print_inner_attributes(attrs));
for vi in blk.view_items.iter() {
try!(self.print_view_item(vi));
}
for st in blk.stmts.iter() { for st in blk.stmts.iter() {
try!(self.print_stmt(&**st)); try!(self.print_stmt(&**st));
} }
@ -2577,7 +2586,7 @@ impl<'a> State<'a> {
pub fn print_view_path(&mut self, vp: &ast::ViewPath) -> IoResult<()> { pub fn print_view_path(&mut self, vp: &ast::ViewPath) -> IoResult<()> {
match vp.node { match vp.node {
ast::ViewPathSimple(ident, ref path, _) => { ast::ViewPathSimple(ident, ref path) => {
try!(self.print_path(path, false)); try!(self.print_path(path, false));
// FIXME(#6993) can't compare identifiers directly here // FIXME(#6993) can't compare identifiers directly here
@ -2591,12 +2600,12 @@ impl<'a> State<'a> {
Ok(()) Ok(())
} }
ast::ViewPathGlob(ref path, _) => { ast::ViewPathGlob(ref path) => {
try!(self.print_path(path, false)); try!(self.print_path(path, false));
word(&mut self.s, "::*") word(&mut self.s, "::*")
} }
ast::ViewPathList(ref path, ref idents, _) => { ast::ViewPathList(ref path, ref idents) => {
if path.segments.is_empty() { if path.segments.is_empty() {
try!(word(&mut self.s, "{")); try!(word(&mut self.s, "{"));
} else { } else {
@ -2618,33 +2627,6 @@ impl<'a> State<'a> {
} }
} }
pub fn print_view_item(&mut self, item: &ast::ViewItem) -> IoResult<()> {
try!(self.hardbreak_if_not_bol());
try!(self.maybe_print_comment(item.span.lo));
try!(self.print_outer_attributes(&item.attrs[]));
try!(self.print_visibility(item.vis));
match item.node {
ast::ViewItemExternCrate(id, ref optional_path, _) => {
try!(self.head("extern crate"));
for &(ref p, style) in optional_path.iter() {
try!(self.print_string(p.get(), style));
try!(space(&mut self.s));
try!(word(&mut self.s, "as"));
try!(space(&mut self.s));
}
try!(self.print_ident(id));
}
ast::ViewItemUse(ref vp) => {
try!(self.head("use"));
try!(self.print_view_path(&**vp));
}
}
try!(word(&mut self.s, ";"));
try!(self.end()); // end inner head-block
self.end() // end outer head-block
}
pub fn print_mutability(&mut self, pub fn print_mutability(&mut self,
mutbl: ast::Mutability) -> IoResult<()> { mutbl: ast::Mutability) -> IoResult<()> {
match mutbl { match mutbl {

View file

@ -20,8 +20,6 @@ use parse::token;
use ptr::P; use ptr::P;
use util::small_vector::SmallVector; use util::small_vector::SmallVector;
use std::mem;
pub fn maybe_inject_crates_ref(krate: ast::Crate, alt_std_name: Option<String>) pub fn maybe_inject_crates_ref(krate: ast::Crate, alt_std_name: Option<String>)
-> ast::Crate { -> ast::Crate {
if use_std(&krate) { if use_std(&krate) {
@ -60,20 +58,16 @@ impl<'a> fold::Folder for StandardLibraryInjector<'a> {
None => token::intern_and_get_ident("std"), None => token::intern_and_get_ident("std"),
}; };
let mut vis = vec!(ast::ViewItem { krate.module.items.insert(0, P(ast::Item {
node: ast::ViewItemExternCrate(token::str_to_ident("std"), id: ast::DUMMY_NODE_ID,
Some((actual_crate_name, ast::CookedStr)), ident: token::str_to_ident("std"),
ast::DUMMY_NODE_ID),
attrs: vec!( attrs: vec!(
attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item( attr::mk_attr_outer(attr::mk_attr_id(), attr::mk_word_item(
InternedString::new("macro_use")))), InternedString::new("macro_use")))),
node: ast::ItemExternCrate(Some((actual_crate_name, ast::CookedStr))),
vis: ast::Inherited, vis: ast::Inherited,
span: DUMMY_SP span: DUMMY_SP
}); }));
// `extern crate` must be precede `use` items
mem::swap(&mut vis, &mut krate.module.view_items);
krate.module.view_items.extend(vis.into_iter());
// don't add #![no_std] here, that will block the prelude injection later. // don't add #![no_std] here, that will block the prelude injection later.
// Add it during the prelude injection instead. // Add it during the prelude injection instead.
@ -123,7 +117,7 @@ impl<'a> fold::Folder for PreludeInjector<'a> {
} }
} }
fn fold_mod(&mut self, ast::Mod {inner, view_items, items}: ast::Mod) -> ast::Mod { fn fold_mod(&mut self, mut mod_: ast::Mod) -> ast::Mod {
let prelude_path = ast::Path { let prelude_path = ast::Path {
span: DUMMY_SP, span: DUMMY_SP,
global: false, global: false,
@ -143,18 +137,11 @@ impl<'a> fold::Folder for PreludeInjector<'a> {
], ],
}; };
let (crates, uses): (Vec<_>, _) = view_items.iter().cloned().partition(|x| { let vp = P(codemap::dummy_spanned(ast::ViewPathGlob(prelude_path)));
match x.node { mod_.items.insert(0, P(ast::Item {
ast::ViewItemExternCrate(..) => true, id: ast::DUMMY_NODE_ID,
_ => false, ident: special_idents::invalid,
} node: ast::ItemUse(vp),
});
// add prelude after any `extern crate` but before any `use`
let mut view_items = crates;
let vp = P(codemap::dummy_spanned(ast::ViewPathGlob(prelude_path, ast::DUMMY_NODE_ID)));
view_items.push(ast::ViewItem {
node: ast::ViewItemUse(vp),
attrs: vec![ast::Attribute { attrs: vec![ast::Attribute {
span: DUMMY_SP, span: DUMMY_SP,
node: ast::Attribute_ { node: ast::Attribute_ {
@ -170,14 +157,9 @@ impl<'a> fold::Folder for PreludeInjector<'a> {
}], }],
vis: ast::Inherited, vis: ast::Inherited,
span: DUMMY_SP, span: DUMMY_SP,
}); }));
view_items.extend(uses.into_iter());
fold::noop_fold_mod(ast::Mod { fold::noop_fold_mod(mod_, self)
inner: inner,
view_items: view_items,
items: items
}, self)
} }
} }

View file

@ -105,11 +105,11 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
// Add a special __test module to the crate that will contain code // Add a special __test module to the crate that will contain code
// generated for the test harness // generated for the test harness
let (mod_, reexport) = mk_test_module(&mut self.cx); let (mod_, reexport) = mk_test_module(&mut self.cx);
folded.module.items.push(mod_);
match reexport { match reexport {
Some(re) => folded.module.view_items.push(re), Some(re) => folded.module.items.push(re),
None => {} None => {}
} }
folded.module.items.push(mod_);
folded folded
} }
@ -205,22 +205,19 @@ impl<'a> fold::Folder for TestHarnessGenerator<'a> {
fn mk_reexport_mod(cx: &mut TestCtxt, tests: Vec<ast::Ident>, fn mk_reexport_mod(cx: &mut TestCtxt, tests: Vec<ast::Ident>,
tested_submods: Vec<(ast::Ident, ast::Ident)>) -> (P<ast::Item>, ast::Ident) { tested_submods: Vec<(ast::Ident, ast::Ident)>) -> (P<ast::Item>, ast::Ident) {
let mut view_items = Vec::new();
let super_ = token::str_to_ident("super"); let super_ = token::str_to_ident("super");
view_items.extend(tests.into_iter().map(|r| { let items = tests.into_iter().map(|r| {
cx.ext_cx.view_use_simple(DUMMY_SP, ast::Public, cx.ext_cx.item_use_simple(DUMMY_SP, ast::Public,
cx.ext_cx.path(DUMMY_SP, vec![super_, r])) cx.ext_cx.path(DUMMY_SP, vec![super_, r]))
})); }).chain(tested_submods.into_iter().map(|(r, sym)| {
view_items.extend(tested_submods.into_iter().map(|(r, sym)| {
let path = cx.ext_cx.path(DUMMY_SP, vec![super_, r, sym]); let path = cx.ext_cx.path(DUMMY_SP, vec![super_, r, sym]);
cx.ext_cx.view_use_simple_(DUMMY_SP, ast::Public, r, path) cx.ext_cx.item_use_simple_(DUMMY_SP, ast::Public, r, path)
})); }));
let reexport_mod = ast::Mod { let reexport_mod = ast::Mod {
inner: DUMMY_SP, inner: DUMMY_SP,
view_items: view_items, items: items.collect(),
items: Vec::new(),
}; };
let sym = token::gensym_ident("__test_reexports"); let sym = token::gensym_ident("__test_reexports");
@ -388,29 +385,29 @@ mod __test {
*/ */
fn mk_std(cx: &TestCtxt) -> ast::ViewItem { fn mk_std(cx: &TestCtxt) -> P<ast::Item> {
let id_test = token::str_to_ident("test"); let id_test = token::str_to_ident("test");
let (vi, vis) = if cx.is_test_crate { let (vi, vis, ident) = if cx.is_test_crate {
(ast::ViewItemUse( (ast::ItemUse(
P(nospan(ast::ViewPathSimple(id_test, P(nospan(ast::ViewPathSimple(id_test,
path_node(vec!(id_test)), path_node(vec!(id_test)))))),
ast::DUMMY_NODE_ID)))), ast::Public, token::special_idents::invalid)
ast::Public)
} else { } else {
(ast::ViewItemExternCrate(id_test, None, ast::DUMMY_NODE_ID), (ast::ItemExternCrate(None), ast::Inherited, id_test)
ast::Inherited)
}; };
ast::ViewItem { P(ast::Item {
id: ast::DUMMY_NODE_ID,
ident: ident,
node: vi, node: vi,
attrs: Vec::new(), attrs: vec![],
vis: vis, vis: vis,
span: DUMMY_SP span: DUMMY_SP
} })
} }
fn mk_test_module(cx: &mut TestCtxt) -> (P<ast::Item>, Option<ast::ViewItem>) { fn mk_test_module(cx: &mut TestCtxt) -> (P<ast::Item>, Option<P<ast::Item>>) {
// Link to test crate // Link to test crate
let view_items = vec!(mk_std(cx)); let import = mk_std(cx);
// A constant vector of test descriptors. // A constant vector of test descriptors.
let tests = mk_tests(cx); let tests = mk_tests(cx);
@ -427,8 +424,7 @@ fn mk_test_module(cx: &mut TestCtxt) -> (P<ast::Item>, Option<ast::ViewItem>) {
let testmod = ast::Mod { let testmod = ast::Mod {
inner: DUMMY_SP, inner: DUMMY_SP,
view_items: view_items, items: vec![import, mainfn, tests],
items: vec!(mainfn, tests),
}; };
let item_ = ast::ItemMod(testmod); let item_ = ast::ItemMod(testmod);
@ -439,34 +435,35 @@ fn mk_test_module(cx: &mut TestCtxt) -> (P<ast::Item>, Option<ast::ViewItem>) {
vec![unstable]))); vec![unstable])));
attr::mk_attr_inner(attr::mk_attr_id(), allow) attr::mk_attr_inner(attr::mk_attr_id(), allow)
}; };
let item = ast::Item { let item = P(ast::Item {
ident: mod_ident,
id: ast::DUMMY_NODE_ID, id: ast::DUMMY_NODE_ID,
ident: mod_ident,
attrs: vec![allow_unstable],
node: item_, node: item_,
vis: ast::Public, vis: ast::Public,
span: DUMMY_SP, span: DUMMY_SP,
attrs: vec![allow_unstable], });
};
let reexport = cx.reexport_test_harness_main.as_ref().map(|s| { let reexport = cx.reexport_test_harness_main.as_ref().map(|s| {
// building `use <ident> = __test::main` // building `use <ident> = __test::main`
let reexport_ident = token::str_to_ident(s.get()); let reexport_ident = token::str_to_ident(s.get());
let use_path = let use_path =
nospan(ast::ViewPathSimple(reexport_ident, nospan(ast::ViewPathSimple(reexport_ident,
path_node(vec![mod_ident, token::str_to_ident("main")]), path_node(vec![mod_ident, token::str_to_ident("main")])));
ast::DUMMY_NODE_ID));
ast::ViewItem { P(ast::Item {
node: ast::ViewItemUse(P(use_path)), id: ast::DUMMY_NODE_ID,
ident: token::special_idents::invalid,
attrs: vec![], attrs: vec![],
node: ast::ItemUse(P(use_path)),
vis: ast::Inherited, vis: ast::Inherited,
span: DUMMY_SP span: DUMMY_SP
} })
}); });
debug!("Synthetic test module:\n{}\n", pprust::item_to_string(&item)); debug!("Synthetic test module:\n{}\n", pprust::item_to_string(&*item));
(P(item), reexport) (item, reexport)
} }
fn nospan<T>(t: T) -> codemap::Spanned<T> { fn nospan<T>(t: T) -> codemap::Spanned<T> {

View file

@ -69,13 +69,6 @@ pub fn string_to_stmt(source_str : String) -> P<ast::Stmt> {
}) })
} }
/// Parse a string, return a view item
pub fn string_to_view_item (source_str : String) -> ast::ViewItem {
with_error_checking_parse(source_str, |p| {
p.parse_view_item(Vec::new())
})
}
/// Parse a string, return a pat. Uses "irrefutable"... which doesn't /// Parse a string, return a pat. Uses "irrefutable"... which doesn't
/// (currently) affect parsing. /// (currently) affect parsing.
pub fn string_to_pat(source_str: String) -> P<ast::Pat> { pub fn string_to_pat(source_str: String) -> P<ast::Pat> {

View file

@ -62,7 +62,6 @@ pub trait Visitor<'v> : Sized {
self.visit_name(span, ident.name); self.visit_name(span, ident.name);
} }
fn visit_mod(&mut self, m: &'v Mod, _s: Span, _n: NodeId) { walk_mod(self, m) } fn visit_mod(&mut self, m: &'v Mod, _s: Span, _n: NodeId) { walk_mod(self, m) }
fn visit_view_item(&mut self, i: &'v ViewItem) { walk_view_item(self, i) }
fn visit_foreign_item(&mut self, i: &'v ForeignItem) { walk_foreign_item(self, i) } fn visit_foreign_item(&mut self, i: &'v ForeignItem) { walk_foreign_item(self, i) }
fn visit_item(&mut self, i: &'v Item) { walk_item(self, i) } fn visit_item(&mut self, i: &'v Item) { walk_item(self, i) }
fn visit_local(&mut self, l: &'v Local) { walk_local(self, l) } fn visit_local(&mut self, l: &'v Local) { walk_local(self, l) }
@ -166,51 +165,11 @@ pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate) {
} }
pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod) { pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod) {
for view_item in module.view_items.iter() {
visitor.visit_view_item(view_item)
}
for item in module.items.iter() { for item in module.items.iter() {
visitor.visit_item(&**item) visitor.visit_item(&**item)
} }
} }
pub fn walk_view_item<'v, V: Visitor<'v>>(visitor: &mut V, vi: &'v ViewItem) {
match vi.node {
ViewItemExternCrate(name, _, _) => {
visitor.visit_ident(vi.span, name)
}
ViewItemUse(ref vp) => {
match vp.node {
ViewPathSimple(ident, ref path, id) => {
visitor.visit_ident(vp.span, ident);
visitor.visit_path(path, id);
}
ViewPathGlob(ref path, id) => {
visitor.visit_path(path, id);
}
ViewPathList(ref prefix, ref list, _) => {
for id in list.iter() {
match id.node {
PathListIdent { name, .. } => {
visitor.visit_ident(id.span, name);
}
PathListMod { .. } => ()
}
}
// Note that the `prefix` here is not a complete
// path, so we don't use `visit_path`.
walk_path(visitor, prefix);
}
}
}
}
for attr in vi.attrs.iter() {
visitor.visit_attribute(attr);
}
}
pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) { pub fn walk_local<'v, V: Visitor<'v>>(visitor: &mut V, local: &'v Local) {
visitor.visit_pat(&*local.pat); visitor.visit_pat(&*local.pat);
walk_ty_opt(visitor, &local.ty); walk_ty_opt(visitor, &local.ty);
@ -269,6 +228,32 @@ pub fn walk_trait_ref<'v,V>(visitor: &mut V,
pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) { pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
visitor.visit_ident(item.span, item.ident); visitor.visit_ident(item.span, item.ident);
match item.node { match item.node {
ItemExternCrate(..) => {}
ItemUse(ref vp) => {
match vp.node {
ViewPathSimple(ident, ref path) => {
visitor.visit_ident(vp.span, ident);
visitor.visit_path(path, item.id);
}
ViewPathGlob(ref path) => {
visitor.visit_path(path, item.id);
}
ViewPathList(ref prefix, ref list) => {
for id in list.iter() {
match id.node {
PathListIdent { name, .. } => {
visitor.visit_ident(id.span, name);
}
PathListMod { .. } => ()
}
}
// Note that the `prefix` here is not a complete
// path, so we don't use `visit_path`.
walk_path(visitor, prefix);
}
}
}
ItemStatic(ref typ, _, ref expr) | ItemStatic(ref typ, _, ref expr) |
ItemConst(ref typ, ref expr) => { ItemConst(ref typ, ref expr) => {
visitor.visit_ty(&**typ); visitor.visit_ty(&**typ);
@ -285,9 +270,6 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
visitor.visit_mod(module, item.span, item.id) visitor.visit_mod(module, item.span, item.id)
} }
ItemForeignMod(ref foreign_module) => { ItemForeignMod(ref foreign_module) => {
for view_item in foreign_module.view_items.iter() {
visitor.visit_view_item(view_item)
}
for foreign_item in foreign_module.items.iter() { for foreign_item in foreign_module.items.iter() {
visitor.visit_foreign_item(&**foreign_item) visitor.visit_foreign_item(&**foreign_item)
} }
@ -732,9 +714,6 @@ pub fn walk_struct_field<'v, V: Visitor<'v>>(visitor: &mut V,
} }
pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) { pub fn walk_block<'v, V: Visitor<'v>>(visitor: &mut V, block: &'v Block) {
for view_item in block.view_items.iter() {
visitor.visit_view_item(view_item)
}
for statement in block.stmts.iter() { for statement in block.stmts.iter() {
visitor.visit_stmt(&**statement) visitor.visit_stmt(&**statement)
} }