about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2015-11-14 02:05:05 +0000
committerbors <bors@rust-lang.org>2015-11-14 02:05:05 +0000
commitb6b17c989964291e89c19fbdfc0ada2df3eaf64a (patch)
treef652dc850d55a4d6e74474fdd7e03ffdf643a3d1 /src
parentbdfb13518ed26529f03bf024f1fb928e1df04ba6 (diff)
parent37795baf5f1b2e2b5b9aa8c502a44017d795bf07 (diff)
downloadrust-b6b17c989964291e89c19fbdfc0ada2df3eaf64a.tar.gz
rust-b6b17c989964291e89c19fbdfc0ada2df3eaf64a.zip
Auto merge of #29816 - nikomatsakis:incr-comp-visitor-refactor-hir-folder, r=nrc
Simplify HIR folder so that it only maps 1 item to 1 item, removing a bunch of asserts. This is a small refactoring on the way to my larger branch for moving items out of line in the tree and isolating attempts to access them.

r? @nrc
Diffstat (limited to 'src')
-rw-r--r--src/librustc/front/map/mod.rs6
-rw-r--r--src/librustc/middle/astencode.rs13
-rw-r--r--src/librustc_front/fold.rs213
3 files changed, 95 insertions, 137 deletions
diff --git a/src/librustc/front/map/mod.rs b/src/librustc/front/map/mod.rs
index 94800db184e..11f0fd1bb2c 100644
--- a/src/librustc/front/map/mod.rs
+++ b/src/librustc/front/map/mod.rs
@@ -847,14 +847,14 @@ pub fn map_decoded_item<'ast, F: FoldOps>(map: &Map<'ast>,
                                           -> &'ast InlinedItem {
     let mut fld = IdAndSpanUpdater { fold_ops: fold_ops };
     let ii = match ii {
-        II::Item(i) => II::Item(fld.fold_item(i).expect_one("expected one item")),
+        II::Item(i) => II::Item(fld.fold_item(i)),
         II::TraitItem(d, ti) => {
             II::TraitItem(fld.fold_ops.new_def_id(d),
-                        fld.fold_trait_item(ti).expect_one("expected one trait item"))
+                          fld.fold_trait_item(ti))
         }
         II::ImplItem(d, ii) => {
             II::ImplItem(fld.fold_ops.new_def_id(d),
-                       fld.fold_impl_item(ii).expect_one("expected one impl item"))
+                         fld.fold_impl_item(ii))
         }
         II::Foreign(i) => II::Foreign(fld.fold_foreign_item(i))
     };
diff --git a/src/librustc/middle/astencode.rs b/src/librustc/middle/astencode.rs
index e81445f19ed..a92fe1a0f2b 100644
--- a/src/librustc/middle/astencode.rs
+++ b/src/librustc/middle/astencode.rs
@@ -389,20 +389,13 @@ fn simplify_ast(ii: InlinedItemRef) -> InlinedItem {
     match ii {
         // HACK we're not dropping items.
         InlinedItemRef::Item(i) => {
-            InlinedItem::Item(fold::noop_fold_item(P(i.clone()), &mut fld)
-                            .expect_one("expected one item"))
+            InlinedItem::Item(fold::noop_fold_item(P(i.clone()), &mut fld))
         }
         InlinedItemRef::TraitItem(d, ti) => {
-            InlinedItem::TraitItem(d,
-                fold::noop_fold_trait_item(P(ti.clone()), &mut fld)
-                    .expect_one("noop_fold_trait_item must produce \
-                                 exactly one trait item"))
+            InlinedItem::TraitItem(d, fold::noop_fold_trait_item(P(ti.clone()), &mut fld))
         }
         InlinedItemRef::ImplItem(d, ii) => {
-            InlinedItem::ImplItem(d,
-                fold::noop_fold_impl_item(P(ii.clone()), &mut fld)
-                    .expect_one("noop_fold_impl_item must produce \
-                                 exactly one impl item"))
+            InlinedItem::ImplItem(d, fold::noop_fold_impl_item(P(ii.clone()), &mut fld))
         }
         InlinedItemRef::Foreign(i) => {
             InlinedItem::Foreign(fold::noop_fold_foreign_item(P(i.clone()), &mut fld))
diff --git a/src/librustc_front/fold.rs b/src/librustc_front/fold.rs
index c4bc003e192..955de44b43f 100644
--- a/src/librustc_front/fold.rs
+++ b/src/librustc_front/fold.rs
@@ -20,8 +20,6 @@ use syntax::owned_slice::OwnedSlice;
 use syntax::ptr::P;
 use syntax::parse::token;
 use std::ptr;
-use syntax::util::small_vector::SmallVector;
-
 
 // This could have a better place to live.
 pub trait MoveMap<T> {
@@ -79,14 +77,10 @@ pub trait Folder : Sized {
         noop_fold_foreign_item(ni, self)
     }
 
-    fn fold_item(&mut self, i: P<Item>) -> SmallVector<P<Item>> {
+    fn fold_item(&mut self, i: P<Item>) -> P<Item> {
         noop_fold_item(i, self)
     }
 
-    fn fold_item_simple(&mut self, i: Item) -> Item {
-        noop_fold_item_simple(i, self)
-    }
-
     fn fold_struct_field(&mut self, sf: StructField) -> StructField {
         noop_fold_struct_field(sf, self)
     }
@@ -95,11 +89,11 @@ pub trait Folder : Sized {
         noop_fold_item_underscore(i, self)
     }
 
-    fn fold_trait_item(&mut self, i: P<TraitItem>) -> SmallVector<P<TraitItem>> {
+    fn fold_trait_item(&mut self, i: P<TraitItem>) -> P<TraitItem> {
         noop_fold_trait_item(i, self)
     }
 
-    fn fold_impl_item(&mut self, i: P<ImplItem>) -> SmallVector<P<ImplItem>> {
+    fn fold_impl_item(&mut self, i: P<ImplItem>) -> P<ImplItem> {
         noop_fold_impl_item(i, self)
     }
 
@@ -111,8 +105,8 @@ pub trait Folder : Sized {
         noop_fold_block(b, self)
     }
 
-    fn fold_stmt(&mut self, s: P<Stmt>) -> SmallVector<P<Stmt>> {
-        s.and_then(|s| noop_fold_stmt(s, self))
+    fn fold_stmt(&mut self, s: P<Stmt>) -> P<Stmt> {
+        noop_fold_stmt(s, self)
     }
 
     fn fold_arm(&mut self, a: Arm) -> Arm {
@@ -123,7 +117,7 @@ pub trait Folder : Sized {
         noop_fold_pat(p, self)
     }
 
-    fn fold_decl(&mut self, d: P<Decl>) -> SmallVector<P<Decl>> {
+    fn fold_decl(&mut self, d: P<Decl>) -> P<Decl> {
         noop_fold_decl(d, self)
     }
 
@@ -340,22 +334,17 @@ pub fn noop_fold_arm<T: Folder>(Arm { attrs, pats, guard, body }: Arm, fld: &mut
     }
 }
 
-pub fn noop_fold_decl<T: Folder>(d: P<Decl>, fld: &mut T) -> SmallVector<P<Decl>> {
-    d.and_then(|Spanned { node, span }| {
+pub fn noop_fold_decl<T: Folder>(d: P<Decl>, fld: &mut T) -> P<Decl> {
+    d.map(|Spanned { node, span }| {
         match node {
-            DeclLocal(l) => SmallVector::one(P(Spanned {
+            DeclLocal(l) => Spanned {
                 node: DeclLocal(fld.fold_local(l)),
                 span: fld.new_span(span),
-            })),
-            DeclItem(it) => fld.fold_item(it)
-                               .into_iter()
-                               .map(|i| {
-                                   P(Spanned {
-                                       node: DeclItem(i),
-                                       span: fld.new_span(span),
-                                   })
-                               })
-                               .collect(),
+            },
+            DeclItem(it) => Spanned {
+                node: DeclItem(fld.fold_item(it)),
+                span: fld.new_span(span),
+            },
         }
     })
 }
@@ -771,7 +760,7 @@ pub fn noop_fold_block<T: Folder>(b: P<Block>, folder: &mut T) -> P<Block> {
     b.map(|Block { id, stmts, expr, rules, span }| {
         Block {
             id: folder.new_id(id),
-            stmts: stmts.into_iter().flat_map(|s| folder.fold_stmt(s).into_iter()).collect(),
+            stmts: stmts.into_iter().map(|s| folder.fold_stmt(s)).collect(),
             expr: expr.map(|x| folder.fold_expr(x)),
             rules: rules,
             span: folder.new_span(span),
@@ -819,9 +808,7 @@ pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ {
         }
         ItemImpl(unsafety, polarity, generics, ifce, ty, impl_items) => {
             let new_impl_items = impl_items.into_iter()
-                                           .flat_map(|item| {
-                                               folder.fold_impl_item(item).into_iter()
-                                           })
+                                           .map(|item| folder.fold_impl_item(item))
                                            .collect();
             let ifce = match ifce {
                 None => None,
@@ -839,7 +826,7 @@ pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ {
         ItemTrait(unsafety, generics, bounds, items) => {
             let bounds = folder.fold_bounds(bounds);
             let items = items.into_iter()
-                             .flat_map(|item| folder.fold_trait_item(item).into_iter())
+                             .map(|item| folder.fold_trait_item(item))
                              .collect();
             ItemTrait(unsafety, folder.fold_generics(generics), bounds, items)
         }
@@ -848,8 +835,8 @@ pub fn noop_fold_item_underscore<T: Folder>(i: Item_, folder: &mut T) -> Item_ {
 
 pub fn noop_fold_trait_item<T: Folder>(i: P<TraitItem>,
                                        folder: &mut T)
-                                       -> SmallVector<P<TraitItem>> {
-    SmallVector::one(i.map(|TraitItem { id, name, attrs, node, span }| {
+                                       -> P<TraitItem> {
+    i.map(|TraitItem { id, name, attrs, node, span }| {
         TraitItem {
             id: folder.new_id(id),
             name: folder.fold_name(name),
@@ -869,11 +856,11 @@ pub fn noop_fold_trait_item<T: Folder>(i: P<TraitItem>,
             },
             span: folder.new_span(span),
         }
-    }))
+    })
 }
 
-pub fn noop_fold_impl_item<T: Folder>(i: P<ImplItem>, folder: &mut T) -> SmallVector<P<ImplItem>> {
-    SmallVector::one(i.map(|ImplItem { id, name, attrs, node, vis, span }| {
+pub fn noop_fold_impl_item<T: Folder>(i: P<ImplItem>, folder: &mut T) -> P<ImplItem> {
+    i.map(|ImplItem { id, name, attrs, node, vis, span }| {
         ImplItem {
             id: folder.new_id(id),
             name: folder.fold_name(name),
@@ -890,13 +877,13 @@ pub fn noop_fold_impl_item<T: Folder>(i: P<ImplItem>, folder: &mut T) -> SmallVe
             },
             span: folder.new_span(span),
         }
-    }))
+    })
 }
 
 pub fn noop_fold_mod<T: Folder>(Mod { inner, items }: Mod, folder: &mut T) -> Mod {
     Mod {
         inner: folder.new_span(inner),
-        items: items.into_iter().flat_map(|x| folder.fold_item(x).into_iter()).collect(),
+        items: items.into_iter().map(|x| folder.fold_item(x)).collect(),
     }
 }
 
@@ -905,34 +892,22 @@ pub fn noop_fold_crate<T: Folder>(Crate { module, attrs, config, span, exported_
                                   -> Crate {
     let config = folder.fold_meta_items(config);
 
-    let mut items = folder.fold_item(P(hir::Item {
-                              name: token::special_idents::invalid.name,
-                              attrs: attrs,
-                              id: DUMMY_NODE_ID,
-                              vis: hir::Public,
-                              span: span,
-                              node: hir::ItemMod(module),
-                          }))
-                          .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(|hir::Item { attrs, span, node, .. }| {
-                match node {
-                    hir::ItemMod(m) => (m, attrs, span),
-                    _ => panic!("fold converted a module to not a module"),
-                }
-            })
-        }
-        None => (hir::Mod {
-            inner: span,
-            items: vec![],
-        },
-                 vec![],
-                 span),
-    };
+    let crate_mod = folder.fold_item(P(hir::Item {
+        name: token::special_idents::invalid.name,
+        attrs: attrs,
+        id: DUMMY_NODE_ID,
+        vis: hir::Public,
+        span: span,
+        node: hir::ItemMod(module),
+    }));
+
+    let (module, attrs, span) =
+        crate_mod.and_then(|hir::Item { attrs, span, node, .. }| {
+            match node {
+                hir::ItemMod(m) => (m, attrs, span),
+                _ => panic!("fold converted a module to not a module"),
+            }
+        });
 
     Crate {
         module: module,
@@ -943,34 +918,28 @@ pub fn noop_fold_crate<T: Folder>(Crate { module, attrs, config, span, exported_
     }
 }
 
-// fold one item into possibly many items
-pub fn noop_fold_item<T: Folder>(i: P<Item>, folder: &mut T) -> SmallVector<P<Item>> {
-    SmallVector::one(i.map(|i| folder.fold_item_simple(i)))
-}
-
-// fold one item into exactly one item
-pub fn noop_fold_item_simple<T: Folder>(Item { id, name, attrs, node, vis, span }: Item,
-                                        folder: &mut T)
-                                        -> Item {
-    let id = folder.new_id(id);
-    let node = folder.fold_item_underscore(node);
-    // FIXME: we should update the impl_pretty_name, but it uses pretty printing.
-    // let ident = match node {
-    //     // The node may have changed, recompute the "pretty" impl name.
-    //     ItemImpl(_, _, _, ref maybe_trait, ref ty, _) => {
-    //         impl_pretty_name(maybe_trait, Some(&**ty))
-    //     }
-    //     _ => ident
-    // };
-
-    Item {
-        id: id,
-        name: folder.fold_name(name),
-        attrs: fold_attrs(attrs, folder),
-        node: node,
-        vis: vis,
-        span: folder.new_span(span),
-    }
+pub fn noop_fold_item<T: Folder>(item: P<Item>, folder: &mut T) -> P<Item> {
+    item.map(|Item { id, name, attrs, node, vis, span }| {
+        let id = folder.new_id(id);
+        let node = folder.fold_item_underscore(node);
+        // FIXME: we should update the impl_pretty_name, but it uses pretty printing.
+        // let ident = match node {
+        //     // The node may have changed, recompute the "pretty" impl name.
+        //     ItemImpl(_, _, _, ref maybe_trait, ref ty, _) => {
+        //         impl_pretty_name(maybe_trait, Some(&**ty))
+        //     }
+        //     _ => ident
+        // };
+
+        Item {
+            id: id,
+            name: folder.fold_name(name),
+            attrs: fold_attrs(attrs, folder),
+            node: node,
+            vis: vis,
+            span: folder.new_span(span),
+        }
+    })
 }
 
 pub fn noop_fold_foreign_item<T: Folder>(ni: P<ForeignItem>, folder: &mut T) -> P<ForeignItem> {
@@ -1184,36 +1153,32 @@ pub fn noop_fold_expr<T: Folder>(Expr { id, node, span }: Expr, folder: &mut T)
     }
 }
 
-pub fn noop_fold_stmt<T: Folder>(Spanned { node, span }: Stmt,
-                                 folder: &mut T)
-                                 -> SmallVector<P<Stmt>> {
-    let span = folder.new_span(span);
-    match node {
-        StmtDecl(d, id) => {
-            let id = folder.new_id(id);
-            folder.fold_decl(d)
-                  .into_iter()
-                  .map(|d| {
-                      P(Spanned {
-                          node: StmtDecl(d, id),
-                          span: span,
-                      })
-                  })
-                  .collect()
-        }
-        StmtExpr(e, id) => {
-            let id = folder.new_id(id);
-            SmallVector::one(P(Spanned {
-                node: StmtExpr(folder.fold_expr(e), id),
-                span: span,
-            }))
-        }
-        StmtSemi(e, id) => {
-            let id = folder.new_id(id);
-            SmallVector::one(P(Spanned {
-                node: StmtSemi(folder.fold_expr(e), id),
-                span: span,
-            }))
+pub fn noop_fold_stmt<T: Folder>(stmt: P<Stmt>, folder: &mut T)
+                                 -> P<Stmt> {
+    stmt.map(|Spanned { node, span }| {
+        let span = folder.new_span(span);
+        match node {
+            StmtDecl(d, id) => {
+                let id = folder.new_id(id);
+                Spanned {
+                    node: StmtDecl(folder.fold_decl(d), id),
+                    span: span
+                }
+            }
+            StmtExpr(e, id) => {
+                let id = folder.new_id(id);
+                Spanned {
+                    node: StmtExpr(folder.fold_expr(e), id),
+                    span: span,
+                }
+            }
+            StmtSemi(e, id) => {
+                let id = folder.new_id(id);
+                Spanned {
+                    node: StmtSemi(folder.fold_expr(e), id),
+                    span: span,
+                }
+            }
         }
-    }
+    })
 }