]> git.lizzy.rs Git - rust.git/commitdiff
debugging, misc fixes
authorNick Cameron <ncameron@mozilla.com>
Sun, 17 Apr 2016 22:30:55 +0000 (10:30 +1200)
committerNick Cameron <ncameron@mozilla.com>
Tue, 19 Apr 2016 22:14:16 +0000 (10:14 +1200)
17 files changed:
src/librustc/hir/lowering.rs
src/librustc/hir/map/collector.rs
src/librustc/hir/map/def_collector.rs
src/librustc/hir/map/mod.rs
src/librustc_driver/driver.rs
src/librustc_driver/lib.rs
src/librustc_driver/pretty.rs
src/librustc_driver/test.rs
src/librustc_metadata/astencode.rs
src/librustc_metadata/creader.rs
src/librustc_save_analysis/csv_dumper.rs
src/librustc_save_analysis/dump_visitor.rs
src/librustc_save_analysis/lib.rs
src/librustc_save_analysis/span_utils.rs
src/librustdoc/core.rs
src/librustdoc/test.rs
src/test/run-make/execution-engine/test.rs

index 7c47665704118cebb165497bc873349f594825f0..0a01cc91f0e30a05443633004f589f76fde33b63 100644 (file)
@@ -62,6 +62,9 @@
 // in the HIR, especially for multiple identifiers.
 
 use hir;
+use hir::map::Definitions;
+use hir::map::definitions::DefPathData;
+use hir::def_id::DefIndex;
 
 use std::collections::BTreeMap;
 use std::collections::HashMap;
@@ -92,10 +95,20 @@ pub struct LoweringContext<'a> {
     // A copy of cached_id, but is also set to an id while a node is lowered for
     // the first time.
     gensym_key: Cell<u32>,
+    // We must keep the set of definitions up to date as we add nodes that
+    // weren't in the AST.
+    definitions: Option<&'a RefCell<Definitions>>,
+    // As we walk the AST we must keep track of the current 'parent' def id (in
+    // the form of a DefIndex) so that if we create a new node which introduces
+    // a definition, then we can properly create the def id.
+    parent_def: Cell<Option<DefIndex>>,
 }
 
 impl<'a, 'hir> LoweringContext<'a> {
-    pub fn new(id_assigner: &'a NodeIdAssigner, c: Option<&Crate>) -> LoweringContext<'a> {
+    pub fn new(id_assigner: &'a NodeIdAssigner,
+               c: Option<&Crate>,
+               defs: &'a RefCell<Definitions>)
+               -> LoweringContext<'a> {
         let crate_root = c.and_then(|c| {
             if std_inject::no_core(c) {
                 None
@@ -113,6 +126,23 @@ pub fn new(id_assigner: &'a NodeIdAssigner, c: Option<&Crate>) -> LoweringContex
             cached_id: Cell::new(0),
             gensym_cache: RefCell::new(HashMap::new()),
             gensym_key: Cell::new(0),
+            definitions: Some(defs),
+            parent_def: Cell::new(None),
+        }
+    }
+
+    // Only use this when you want a LoweringContext for testing and won't look
+    // up def ids for anything created during lowering.
+    pub fn testing_context(id_assigner: &'a NodeIdAssigner) -> LoweringContext<'a> {
+        LoweringContext {
+            crate_root: None,
+            id_cache: RefCell::new(HashMap::new()),
+            id_assigner: id_assigner,
+            cached_id: Cell::new(0),
+            gensym_cache: RefCell::new(HashMap::new()),
+            gensym_key: Cell::new(0),
+            definitions: None,
+            parent_def: Cell::new(None),
         }
     }
 
@@ -146,6 +176,25 @@ fn str_to_ident(&self, s: &'static str) -> hir::Ident {
     fn diagnostic(&self) -> &Handler {
         self.id_assigner.diagnostic()
     }
+
+    fn with_parent_def<T, F: FnOnce() -> T>(&self, parent_id: NodeId, f: F) -> T {
+        if self.definitions.is_none() {
+            // This should only be used for testing.
+            return f();
+        }
+
+        let old_def = self.parent_def.get();
+        self.parent_def.set(Some(self.get_def(parent_id)));
+        let result = f();
+        self.parent_def.set(old_def);
+
+        result
+    }
+
+    fn get_def(&self, id: NodeId) -> DefIndex {
+        let defs = self.definitions.unwrap().borrow();
+        defs.opt_def_index(id).unwrap()
+    }
 }
 
 // Utility fn for setting and unsetting the cached id.
@@ -733,47 +782,51 @@ pub fn lower_item_kind(lctx: &LoweringContext, i: &ItemKind) -> hir::Item_ {
 }
 
 pub fn lower_trait_item(lctx: &LoweringContext, i: &TraitItem) -> hir::TraitItem {
-    hir::TraitItem {
-        id: i.id,
-        name: i.ident.name,
-        attrs: lower_attrs(lctx, &i.attrs),
-        node: match i.node {
-            TraitItemKind::Const(ref ty, ref default) => {
-                hir::ConstTraitItem(lower_ty(lctx, ty),
-                                    default.as_ref().map(|x| lower_expr(lctx, x)))
-            }
-            TraitItemKind::Method(ref sig, ref body) => {
-                hir::MethodTraitItem(lower_method_sig(lctx, sig),
-                                     body.as_ref().map(|x| lower_block(lctx, x)))
-            }
-            TraitItemKind::Type(ref bounds, ref default) => {
-                hir::TypeTraitItem(lower_bounds(lctx, bounds),
-                                   default.as_ref().map(|x| lower_ty(lctx, x)))
-            }
-        },
-        span: i.span,
-    }
+    lctx.with_parent_def(i.id, || {
+        hir::TraitItem {
+            id: i.id,
+            name: i.ident.name,
+            attrs: lower_attrs(lctx, &i.attrs),
+            node: match i.node {
+                TraitItemKind::Const(ref ty, ref default) => {
+                    hir::ConstTraitItem(lower_ty(lctx, ty),
+                                        default.as_ref().map(|x| lower_expr(lctx, x)))
+                }
+                TraitItemKind::Method(ref sig, ref body) => {
+                    hir::MethodTraitItem(lower_method_sig(lctx, sig),
+                                         body.as_ref().map(|x| lower_block(lctx, x)))
+                }
+                TraitItemKind::Type(ref bounds, ref default) => {
+                    hir::TypeTraitItem(lower_bounds(lctx, bounds),
+                                       default.as_ref().map(|x| lower_ty(lctx, x)))
+                }
+            },
+            span: i.span,
+        }
+    })
 }
 
 pub fn lower_impl_item(lctx: &LoweringContext, i: &ImplItem) -> hir::ImplItem {
-    hir::ImplItem {
-        id: i.id,
-        name: i.ident.name,
-        attrs: lower_attrs(lctx, &i.attrs),
-        vis: lower_visibility(lctx, &i.vis),
-        defaultness: lower_defaultness(lctx, i.defaultness),
-        node: match i.node {
-            ImplItemKind::Const(ref ty, ref expr) => {
-                hir::ImplItemKind::Const(lower_ty(lctx, ty), lower_expr(lctx, expr))
-            }
-            ImplItemKind::Method(ref sig, ref body) => {
-                hir::ImplItemKind::Method(lower_method_sig(lctx, sig), lower_block(lctx, body))
-            }
-            ImplItemKind::Type(ref ty) => hir::ImplItemKind::Type(lower_ty(lctx, ty)),
-            ImplItemKind::Macro(..) => panic!("Shouldn't exist any more"),
-        },
-        span: i.span,
-    }
+    lctx.with_parent_def(i.id, || {
+        hir::ImplItem {
+            id: i.id,
+            name: i.ident.name,
+            attrs: lower_attrs(lctx, &i.attrs),
+            vis: lower_visibility(lctx, &i.vis),
+            defaultness: lower_defaultness(lctx, i.defaultness),
+            node: match i.node {
+                ImplItemKind::Const(ref ty, ref expr) => {
+                    hir::ImplItemKind::Const(lower_ty(lctx, ty), lower_expr(lctx, expr))
+                }
+                ImplItemKind::Method(ref sig, ref body) => {
+                    hir::ImplItemKind::Method(lower_method_sig(lctx, sig), lower_block(lctx, body))
+                }
+                ImplItemKind::Type(ref ty) => hir::ImplItemKind::Type(lower_ty(lctx, ty)),
+                ImplItemKind::Macro(..) => panic!("Shouldn't exist any more"),
+            },
+            span: i.span,
+        }
+    })
 }
 
 pub fn lower_mod(lctx: &LoweringContext, m: &Mod) -> hir::Mod {
@@ -831,7 +884,9 @@ pub fn lower_item_id(_lctx: &LoweringContext, i: &Item) -> hir::ItemId {
 }
 
 pub fn lower_item(lctx: &LoweringContext, i: &Item) -> hir::Item {
-    let node = lower_item_kind(lctx, &i.node);
+    let node = lctx.with_parent_def(i.id, || {
+        lower_item_kind(lctx, &i.node)
+    });
 
     hir::Item {
         id: i.id,
@@ -844,21 +899,23 @@ pub fn lower_item(lctx: &LoweringContext, i: &Item) -> hir::Item {
 }
 
 pub fn lower_foreign_item(lctx: &LoweringContext, i: &ForeignItem) -> hir::ForeignItem {
-    hir::ForeignItem {
-        id: i.id,
-        name: i.ident.name,
-        attrs: lower_attrs(lctx, &i.attrs),
-        node: match i.node {
-            ForeignItemKind::Fn(ref fdec, ref generics) => {
-                hir::ForeignItemFn(lower_fn_decl(lctx, fdec), lower_generics(lctx, generics))
-            }
-            ForeignItemKind::Static(ref t, m) => {
-                hir::ForeignItemStatic(lower_ty(lctx, t), m)
-            }
-        },
-        vis: lower_visibility(lctx, &i.vis),
-        span: i.span,
-    }
+    lctx.with_parent_def(i.id, || {
+        hir::ForeignItem {
+            id: i.id,
+            name: i.ident.name,
+            attrs: lower_attrs(lctx, &i.attrs),
+            node: match i.node {
+                ForeignItemKind::Fn(ref fdec, ref generics) => {
+                    hir::ForeignItemFn(lower_fn_decl(lctx, fdec), lower_generics(lctx, generics))
+                }
+                ForeignItemKind::Static(ref t, m) => {
+                    hir::ForeignItemStatic(lower_ty(lctx, t), m)
+                }
+            },
+            vis: lower_visibility(lctx, &i.vis),
+            span: i.span,
+        }
+    })
 }
 
 pub fn lower_method_sig(lctx: &LoweringContext, sig: &MethodSig) -> hir::MethodSig {
@@ -926,9 +983,11 @@ pub fn lower_pat(lctx: &LoweringContext, p: &Pat) -> P<hir::Pat> {
         node: match p.node {
             PatKind::Wild => hir::PatKind::Wild,
             PatKind::Ident(ref binding_mode, pth1, ref sub) => {
-                hir::PatKind::Ident(lower_binding_mode(lctx, binding_mode),
-                              respan(pth1.span, lower_ident(lctx, pth1.node)),
-                              sub.as_ref().map(|x| lower_pat(lctx, x)))
+                lctx.with_parent_def(p.id, || {
+                    hir::PatKind::Ident(lower_binding_mode(lctx, binding_mode),
+                                  respan(pth1.span, lower_ident(lctx, pth1.node)),
+                                  sub.as_ref().map(|x| lower_pat(lctx, x)))
+                })
             }
             PatKind::Lit(ref e) => hir::PatKind::Lit(lower_expr(lctx, e)),
             PatKind::TupleStruct(ref pth, ref pats) => {
@@ -1202,9 +1261,11 @@ pub fn lower_expr(lctx: &LoweringContext, e: &Expr) -> P<hir::Expr> {
                                hir::MatchSource::Normal)
             }
             ExprKind::Closure(capture_clause, ref decl, ref body) => {
-                hir::ExprClosure(lower_capture_clause(lctx, capture_clause),
-                                 lower_fn_decl(lctx, decl),
-                                 lower_block(lctx, body))
+                lctx.with_parent_def(e.id, || {
+                    hir::ExprClosure(lower_capture_clause(lctx, capture_clause),
+                                     lower_fn_decl(lctx, decl),
+                                     lower_block(lctx, body))
+                })
             }
             ExprKind::Block(ref blk) => hir::ExprBlock(lower_block(lctx, blk)),
             ExprKind::Assign(ref el, ref er) => {
@@ -1602,7 +1663,12 @@ fn make_struct(lctx: &LoweringContext,
                     // `{ let _result = ...; _result }`
                     // underscore prevents an unused_variables lint if the head diverges
                     let result_ident = lctx.str_to_ident("_result");
-                    let let_stmt = stmt_let(lctx, e.span, false, result_ident, match_expr, None);
+                    let let_stmt = stmt_let(lctx,
+                                            e.span,
+                                            false,
+                                            result_ident,
+                                            match_expr,
+                                            None);
                     let result = expr_ident(lctx, e.span, result_ident, None);
                     let block = block_all(lctx, e.span, hir_vec![let_stmt], Some(result));
                     // add the attributes to the outer returned expr node
@@ -1655,7 +1721,8 @@ fn make_struct(lctx: &LoweringContext,
                             let err_ctor = expr_path(lctx, path, None);
                             expr_call(lctx, e.span, err_ctor, hir_vec![from_expr], None)
                         };
-                        let err_pat = pat_err(lctx, e.span, pat_ident(lctx, e.span, err_ident));
+                        let err_pat = pat_err(lctx, e.span,
+                                              pat_ident(lctx, e.span, err_ident));
                         let ret_expr = expr(lctx, e.span,
                                             hir::Expr_::ExprRet(Some(err_expr)), None);
 
@@ -1938,12 +2005,22 @@ fn pat_ident_binding_mode(lctx: &LoweringContext,
                           bm: hir::BindingMode)
                           -> P<hir::Pat> {
     let pat_ident = hir::PatKind::Ident(bm,
-                                  Spanned {
-                                      span: span,
-                                      node: ident,
-                                  },
-                                  None);
-    pat(lctx, span, pat_ident)
+                                        Spanned {
+                                            span: span,
+                                            node: ident,
+                                        },
+                                        None);
+
+    let pat = pat(lctx, span, pat_ident);
+
+    if let Some(defs) = lctx.definitions {
+        let mut defs = defs.borrow_mut();
+        defs.create_def_with_parent(lctx.parent_def.get(),
+                                    pat.id,
+                                    DefPathData::Binding(ident.name));
+    }
+
+    pat
 }
 
 fn pat_wild(lctx: &LoweringContext, span: Span) -> P<hir::Pat> {
@@ -2130,7 +2207,8 @@ fn test_preserves_ids() {
         let ast_in = quote_expr!(&cx, in HEAP { foo() });
         let ast_in = assigner.fold_expr(ast_in);
 
-        let lctx = LoweringContext::new(&assigner, None);
+        let lctx = LoweringContext::testing_context(&assigner);
+
         let hir1 = lower_expr(&lctx, &ast_if_let);
         let hir2 = lower_expr(&lctx, &ast_if_let);
         assert!(hir1 == hir2);
index c9d93319c03a8a42dc8aceeb6fc1f40c4588fcc4..9f55f46541cfde4c007f99fb587ad0173e1cc42e 100644 (file)
@@ -162,7 +162,12 @@ fn visit_impl_item(&mut self, ii: &'ast ImplItem) {
     }
 
     fn visit_pat(&mut self, pat: &'ast Pat) {
-        self.insert(pat.id, NodeLocal(pat));
+        let node = if let PatKind::Ident(..) = pat.node {
+            NodeLocal(pat)
+        } else {
+            NodePat(pat)
+        };
+        self.insert(pat.id, node);
 
         self.with_parent(pat.id, |this| {
             intravisit::walk_pat(this, pat);
index f8d0243548fd13afcb031a57281975812cf484ed..b2456f4ab8714bfdac7c987a0750f74c7f79677c 100644 (file)
@@ -18,6 +18,7 @@
 
 use syntax::ast::*;
 use syntax::visit;
+use syntax::parse::token;
 
 /// Creates def ids for nodes in the HIR.
 pub struct DefCollector<'ast> {
@@ -35,8 +36,9 @@ pub fn root() -> DefCollector<'ast> {
             definitions: Definitions::new(),
             parent_def: None,
         };
-        let result = collector.create_def_with_parent(None, CRATE_NODE_ID, DefPathData::CrateRoot);
-        assert_eq!(result, CRATE_DEF_INDEX);
+        let root = collector.create_def_with_parent(None, CRATE_NODE_ID, DefPathData::CrateRoot);
+        assert_eq!(root, CRATE_DEF_INDEX);
+        collector.parent_def = Some(root);
 
         collector.create_def_with_parent(Some(CRATE_DEF_INDEX), DUMMY_NODE_ID, DefPathData::Misc);
 
@@ -125,12 +127,12 @@ fn visit_item(&mut self, i: &'ast Item) {
                             this.create_def(v.node.data.id(),
                                             DefPathData::EnumVariant(v.node.name.name));
 
-                        for field in v.node.data.fields() {
-                            if let Some(ident) = field.ident {
-                                this.create_def_with_parent(Some(variant_def_index),
-                                                            field.id,
-                                                            DefPathData::Field(ident.name));
-                            }
+                        for (index, field) in v.node.data.fields().iter().enumerate() {
+                            let name = field.ident.map(|ident| ident.name)
+                                .unwrap_or(token::intern(&index.to_string()));
+                            this.create_def_with_parent(Some(variant_def_index),
+                                                        field.id,
+                                                        DefPathData::Field(name));
                         }
                     }
                 }
@@ -141,10 +143,10 @@ fn visit_item(&mut self, i: &'ast Item) {
                                         DefPathData::StructCtor);
                     }
 
-                    for field in struct_def.fields() {
-                        if let Some(ident) = field.ident {
-                            this.create_def(field.id, DefPathData::Field(ident.name));
-                        }
+                    for (index, field) in struct_def.fields().iter().enumerate() {
+                        let name = field.ident.map(|ident| ident.name)
+                            .unwrap_or(token::intern(&index.to_string()));
+                        this.create_def(field.id, DefPathData::Field(name));
                     }
                 }
                 _ => {}
@@ -205,14 +207,10 @@ fn visit_impl_item(&mut self, ii: &'ast ImplItem) {
     }
 
     fn visit_pat(&mut self, pat: &'ast Pat) {
-        let maybe_binding = match pat.node {
-            PatKind::Ident(_, id, _) => Some(id.node),
-            _ => None
-        };
-
         let parent_def = self.parent_def;
-        if let Some(id) = maybe_binding {
-            let def = self.create_def(pat.id, DefPathData::Binding(id.name));
+
+        if let PatKind::Ident(_, id, _) = pat.node {
+            let def = self.create_def(pat.id, DefPathData::Binding(id.node.name));
             self.parent_def = Some(def);
         }
 
@@ -353,14 +351,10 @@ fn visit_impl_item(&mut self, ii: &'ast hir::ImplItem) {
     }
 
     fn visit_pat(&mut self, pat: &'ast hir::Pat) {
-        let maybe_binding = match pat.node {
-            hir::PatKind::Ident(_, id, _) => Some(id.node),
-            _ => None
-        };
-
         let parent_def = self.parent_def;
-        if let Some(id) = maybe_binding {
-            let def = self.create_def(pat.id, DefPathData::Binding(id.name));
+
+        if let hir::PatKind::Ident(_, id, _) = pat.node {
+            let def = self.create_def(pat.id, DefPathData::Binding(id.node.name));
             self.parent_def = Some(def);
         }
 
index 1d0c40646b51a885589cf157d5dc4f32654b582c..92f8c9249c842937e45b55585d16faf9489aed80 100644 (file)
@@ -196,7 +196,7 @@ pub struct Map<'ast> {
     /// plain old integers.
     map: RefCell<Vec<MapEntry<'ast>>>,
 
-    definitions: RefCell<Definitions>,
+    definitions: &'ast RefCell<Definitions>,
 }
 
 impl<'ast> Map<'ast> {
@@ -789,7 +789,9 @@ pub fn collect_definitions<'ast>(krate: &'ast ast::Crate) -> Definitions {
     def_collector.definitions
 }
 
-pub fn map_crate<'ast>(forest: &'ast mut Forest, definitions: Definitions) -> Map<'ast> {
+pub fn map_crate<'ast>(forest: &'ast mut Forest,
+                       definitions: &'ast RefCell<Definitions>)
+                       -> Map<'ast> {
     let mut collector = NodeCollector::root(&forest.krate);
     intravisit::walk_crate(&mut collector, &forest.krate);
     let map = collector.map;
@@ -814,7 +816,7 @@ pub fn map_crate<'ast>(forest: &'ast mut Forest, definitions: Definitions) -> Ma
         forest: forest,
         dep_graph: forest.dep_graph.clone(),
         map: RefCell::new(map),
-        definitions: RefCell::new(definitions),
+        definitions: definitions,
     }
 }
 
index 6724c0ed8ebfd81f1053cbf4b86ac00b9f4e4987..26422e3194a2219ba525f7f980ebb60fb13e5e56 100644 (file)
@@ -43,6 +43,7 @@
 
 use serialize::json;
 
+use std::cell::RefCell;
 use std::collections::HashMap;
 use std::env;
 use std::ffi::{OsString, OsStr};
@@ -123,9 +124,9 @@ macro_rules! controller_entry_point {
         let dep_graph = DepGraph::new(sess.opts.build_dep_graph);
 
         // Collect defintions for def ids.
-        let defs = time(sess.time_passes(),
-                        "collecting defs",
-                        || hir_map::collect_definitions(&expanded_crate));
+        let defs = &RefCell::new(time(sess.time_passes(),
+                                 "collecting defs",
+                                 || hir_map::collect_definitions(&expanded_crate)));
 
         time(sess.time_passes(),
              "external crate/lib resolution",
@@ -133,7 +134,7 @@ macro_rules! controller_entry_point {
                     .read_crates(&dep_graph));
 
         // Lower ast -> hir.
-        let lcx = LoweringContext::new(sess, Some(&expanded_crate));
+        let lcx = LoweringContext::new(sess, Some(&expanded_crate), defs);
         let hir_forest = &mut time(sess.time_passes(),
                                    "lowering ast -> hir",
                                    || hir_map::Forest::new(lower_crate(&lcx, &expanded_crate),
index 769449b96d2b1ed66e424576abf8cf779acaedbc..0e100f48ac38d64488defa10f3e3d7775c0e49ff 100644 (file)
@@ -199,14 +199,9 @@ macro_rules! do_or_return {($expr: expr, $sess: expr) => {
     // It is somewhat unfortunate that this is hardwired in - this is forced by
     // the fact that pretty_print_input requires the session by value.
     let pretty = callbacks.parse_pretty(&sess, &matches);
-    match pretty {
-        Some((ppm, opt_uii)) => {
-            pretty::pretty_print_input(sess, &cstore, cfg, &input, ppm, opt_uii, ofile);
-            return (Ok(()), None);
-        }
-        None => {
-            // continue
-        }
+    if let Some((ppm, opt_uii)) = pretty {
+        pretty::pretty_print_input(sess, &cstore, cfg, &input, ppm, opt_uii, ofile);
+        return (Ok(()), None);
     }
 
     let plugins = sess.opts.debugging_opts.extra_plugins.clone();
index 2e3a477e0484050ec5cf9db731d7a4c48564b6ae..b1143fd3e845463339783d224442c3b191d1c6b0 100644 (file)
@@ -29,6 +29,7 @@
 use rustc_borrowck::graphviz as borrowck_dot;
 use rustc_resolve as resolve;
 use rustc_metadata::cstore::CStore;
+use rustc_metadata::creader::LocalCrateReader;
 
 use rustc_mir::pretty::write_mir_pretty;
 use rustc_mir::graphviz::write_mir_graphviz;
@@ -43,6 +44,7 @@
 
 use graphviz as dot;
 
+use std::cell::RefCell;
 use std::fs::File;
 use std::io::{self, Write};
 use std::iter;
@@ -719,7 +721,7 @@ pub fn pretty_print_input(sess: Session,
     let is_expanded = needs_expansion(&ppm);
     let compute_ast_map = needs_ast_map(&ppm, &opt_uii);
     let krate = if compute_ast_map {
-        match driver::phase_2_configure_and_expand(&sess, &cstore, krate, &id[..], None) {
+        match driver::phase_2_configure_and_expand(&sess, &cstore, krate, &id, None) {
             Err(_) => return,
             Ok(k) => driver::assign_node_ids(&sess, k),
         }
@@ -730,15 +732,18 @@ pub fn pretty_print_input(sess: Session,
     // There is some twisted, god-forsaken tangle of lifetimes here which makes
     // the ordering of stuff super-finicky.
     let mut hir_forest;
-    let lcx = LoweringContext::new(&sess, Some(&krate));
-    let arenas = ty::CtxtArenas::new();
+    let mut _defs = None;
     let dep_graph = DepGraph::new(false);
+    let arenas = ty::CtxtArenas::new();
     let _ignore = dep_graph.in_ignore();
     let ast_map = if compute_ast_map {
-        let defs = hir_map::collect_definitions(&krate);
+        _defs = Some(RefCell::new(hir_map::collect_definitions(&krate)));
+        let defs = _defs.as_ref().unwrap();
+        LocalCrateReader::new(&sess, &cstore, defs, &krate, &id).read_crates(&dep_graph);
+        let lcx = LoweringContext::new(&sess, Some(&krate), defs);
+
         hir_forest = hir_map::Forest::new(lower_crate(&lcx, &krate), dep_graph.clone());
-        let map = hir_map::map_crate(&mut hir_forest, defs);
-        Some(map)
+        Some(hir_map::map_crate(&mut hir_forest, defs))
     } else {
         None
     };
@@ -751,7 +756,7 @@ pub fn pretty_print_input(sess: Session,
                   .unwrap()
                   .as_bytes()
                   .to_vec();
-    let mut rdr = &src[..];
+    let mut rdr = &*src;
 
     let mut out = Vec::new();
 
index fc12d546288b47b0c4a4fe5770723521c955b82d..ce92dd158c969e4b33a80b05125f25fe471f2518 100644 (file)
 use rustc::ty::relate::TypeRelation;
 use rustc::infer::{self, InferOk, InferResult, TypeOrigin};
 use rustc_metadata::cstore::CStore;
+use rustc_metadata::creader::LocalCrateReader;
 use rustc::hir::map as hir_map;
 use rustc::session::{self, config};
+use std::cell::RefCell;
 use std::rc::Rc;
 use syntax::ast;
 use syntax::abi::Abi;
@@ -119,13 +121,15 @@ fn test_env<F>(source_string: &str,
     let krate = driver::phase_2_configure_and_expand(&sess, &cstore, krate, "test", None)
                     .expect("phase 2 aborted");
 
-    let krate = driver::assign_node_ids(&sess, krate);
-    let lcx = LoweringContext::new(&sess, Some(&krate));
     let dep_graph = DepGraph::new(false);
+    let krate = driver::assign_node_ids(&sess, krate);
+    let defs = &RefCell::new(hir_map::collect_definitions(&krate));
+    LocalCrateReader::new(&sess, &cstore, defs, &krate, "test_crate").read_crates(&dep_graph);
+    let lcx = LoweringContext::new(&sess, Some(&krate), defs);
     let _ignore = dep_graph.in_ignore();
-    let mut hir_forest = hir_map::Forest::new(lower_crate(&lcx, &krate), dep_graph.clone());
+    let mut hir_forest = &mut hir_map::Forest::new(lower_crate(&lcx, &krate), dep_graph.clone());
     let arenas = ty::CtxtArenas::new();
-    let ast_map = driver::make_map(&sess, &mut hir_forest);
+    let ast_map = hir_map::map_crate(hir_forest, defs);
 
     // run just enough stuff to build a tcx:
     let lang_items = lang_items::collect_language_items(&sess, &ast_map);
index 49512a5018e7db9c0d2cdaed95f87d7c2fbfc8dd..2d6a043e34aeb88fb280fb58bfdf03039d2f04d0 100644 (file)
@@ -1339,7 +1339,7 @@ fn roundtrip(in_item: hir::Item) {
 fn test_basic() {
     let cx = mk_ctxt();
     let fnia = FakeNodeIdAssigner;
-    let lcx = LoweringContext::new(&fnia, None);
+    let lcx = LoweringContext::testing_context(&fnia);
     roundtrip(lower_item(&lcx, &quote_item!(&cx,
         fn foo() {}
     ).unwrap()));
@@ -1349,7 +1349,7 @@ fn foo() {}
 fn test_smalltalk() {
     let cx = mk_ctxt();
     let fnia = FakeNodeIdAssigner;
-    let lcx = LoweringContext::new(&fnia, None);
+    let lcx = LoweringContext::testing_context(&fnia);
     roundtrip(lower_item(&lcx, &quote_item!(&cx,
         fn foo() -> isize { 3 + 4 } // first smalltalk program ever executed.
     ).unwrap()));
@@ -1359,7 +1359,7 @@ fn foo() -> isize { 3 + 4 } // first smalltalk program ever executed.
 fn test_more() {
     let cx = mk_ctxt();
     let fnia = FakeNodeIdAssigner;
-    let lcx = LoweringContext::new(&fnia, None);
+    let lcx = LoweringContext::testing_context(&fnia);
     roundtrip(lower_item(&lcx, &quote_item!(&cx,
         fn foo(x: usize, y: usize) -> usize {
             let z = x + y;
@@ -1378,7 +1378,7 @@ fn eq_int(a: isize, b: isize) -> bool { a == b }
         }
     ).unwrap();
     let fnia = FakeNodeIdAssigner;
-    let lcx = LoweringContext::new(&fnia, None);
+    let lcx = LoweringContext::testing_context(&fnia);
     let hir_item = lower_item(&lcx, &item);
     let item_in = InlinedItemRef::Item(&hir_item);
     let item_out = simplify_ast(item_in);
index 4ba59a3d7ebe98dedc2def62d0624e70cc76cff9..635ef4ab3583619b4a74475625e4b0451362d380 100644 (file)
@@ -45,7 +45,7 @@ pub struct LocalCrateReader<'a> {
     cstore: &'a CStore,
     creader: CrateReader<'a>,
     krate: &'a ast::Crate,
-    defintions: &'a hir_map::Definitions,
+    defintions: &'a RefCell<hir_map::Definitions>,
 }
 
 pub struct CrateReader<'a> {
@@ -59,6 +59,7 @@ pub struct CrateReader<'a> {
 impl<'a, 'ast> visit::Visitor<'ast> for LocalCrateReader<'a> {
     fn visit_item(&mut self, a: &'ast ast::Item) {
         self.process_item(a);
+        visit::walk_item(self, a);
     }
 }
 
@@ -81,6 +82,7 @@ fn should_link(i: &ast::Item) -> bool {
     !attr::contains_name(&i.attrs, "no_link")
 }
 
+#[derive(Debug)]
 struct CrateInfo {
     ident: String,
     name: String,
@@ -750,7 +752,7 @@ fn validate(me: &CrateReader, krate: ast::CrateNum,
 impl<'a> LocalCrateReader<'a> {
     pub fn new(sess: &'a Session,
                cstore: &'a CStore,
-               defs: &'a hir_map::Definitions,
+               defs: &'a RefCell<hir_map::Definitions>,
                krate: &'a ast::Crate,
                local_crate_name: &str)
                -> LocalCrateReader<'a> {
@@ -807,9 +809,10 @@ fn process_item(&mut self, i: &ast::Item) {
                                                                       i.span,
                                                                       PathKind::Crate,
                                                                       true);
-                        let def_id = self.defintions.opt_local_def_id(i.id).unwrap();
 
-                        let len = self.defintions.def_path(def_id.index).data.len();
+                        let defs = self.defintions.borrow();
+                        let def_id = defs.opt_local_def_id(i.id).unwrap();
+                        let len = defs.def_path(def_id.index).data.len();
 
                         self.creader.update_extern_crate(cnum,
                                                          ExternCrate {
index 0e02830db7ad75498bb4b2e254faf081da93ab4c..45ec9a97a11844308d27cc0a05eeaefcec7dd8d4 100644 (file)
 use super::dump::Dump;
 use super::span_utils::SpanUtils;
 
-pub struct CsvDumper<'a, 'b, W: 'b> {
+pub struct CsvDumper<'tcx, 'b, W: 'b> {
     output: &'b mut W,
     dump_spans: bool,
-    span: SpanUtils<'a>
+    span: SpanUtils<'tcx>
 }
 
 impl<'a, 'b, W: Write> CsvDumper<'a, 'b, W> {
index 3784c95fe2bcbb403d3298e692cfc1bb890a6ad0..bf6ad7039636e97697c919b34de5f10f6bcd27f5 100644 (file)
@@ -42,7 +42,7 @@
 use syntax::print::pprust::{path_to_string, ty_to_string};
 use syntax::ptr::P;
 
-use rustc::hir::lowering::{lower_expr, LoweringContext};
+use rustc::hir::lowering::lower_expr;
 
 use super::{escape, generated_code, SaveContext, PathCollector};
 use super::data::*;
@@ -60,12 +60,12 @@ macro_rules! down_cast_data {
     };
 }
 
-pub struct DumpVisitor<'l, 'tcx: 'l, D: 'l> {
+pub struct DumpVisitor<'l, 'tcx: 'l, 'll, D: 'll> {
     save_ctxt: SaveContext<'l, 'tcx>,
     sess: &'l Session,
     tcx: &'l TyCtxt<'tcx>,
     analysis: &'l ty::CrateAnalysis<'l>,
-    dumper: &'l mut D,
+    dumper: &'ll mut D,
 
     span: SpanUtils<'l>,
 
@@ -77,22 +77,19 @@ pub struct DumpVisitor<'l, 'tcx: 'l, D: 'l> {
     // one macro use per unique callsite span.
     mac_defs: HashSet<Span>,
     mac_uses: HashSet<Span>,
-
 }
 
-impl <'l, 'tcx, D> DumpVisitor<'l, 'tcx, D>
-where D: Dump
-{
+impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> {
     pub fn new(tcx: &'l TyCtxt<'tcx>,
-               lcx: &'l LoweringContext<'l>,
+               save_ctxt: SaveContext<'l, 'tcx>,
                analysis: &'l ty::CrateAnalysis<'l>,
-               dumper: &'l mut D)
-               -> DumpVisitor<'l, 'tcx, D> {
+               dumper: &'ll mut D)
+               -> DumpVisitor<'l, 'tcx, 'll, D> {
         let span_utils = SpanUtils::new(&tcx.sess);
         DumpVisitor {
             sess: &tcx.sess,
             tcx: tcx,
-            save_ctxt: SaveContext::from_span_utils(tcx, lcx, span_utils.clone()),
+            save_ctxt: save_ctxt,
             analysis: analysis,
             dumper: dumper,
             span: span_utils.clone(),
@@ -103,7 +100,7 @@ pub fn new(tcx: &'l TyCtxt<'tcx>,
     }
 
     fn nest<F>(&mut self, scope_id: NodeId, f: F)
-        where F: FnOnce(&mut DumpVisitor<'l, 'tcx, D>)
+        where F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll, D>)
     {
         let parent_scope = self.cur_scope;
         self.cur_scope = scope_id;
@@ -982,7 +979,7 @@ fn process_macro_use(&mut self, span: Span, id: NodeId) {
     }
 }
 
-impl<'l, 'tcx, 'v, D: Dump + 'l> Visitor<'v> for DumpVisitor<'l, 'tcx, D> {
+impl<'v, 'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'v> for DumpVisitor<'l, 'tcx, 'll, D> {
     fn visit_item(&mut self, item: &ast::Item) {
         use syntax::ast::ItemKind::*;
         self.process_macro_use(item.span, item.id);
index 4596398c3152ee24b091b1a4595331f6e4ead173..9148b53322bfe61b125620c0d80f1e3011bdf097 100644 (file)
@@ -73,7 +73,7 @@ pub enum Row {
 pub struct SaveContext<'l, 'tcx: 'l> {
     tcx: &'l TyCtxt<'tcx>,
     lcx: &'l lowering::LoweringContext<'l>,
-    span_utils: SpanUtils<'l>,
+    span_utils: SpanUtils<'tcx>,
 }
 
 macro_rules! option_try(
@@ -90,7 +90,7 @@ pub fn new(tcx: &'l TyCtxt<'tcx>,
 
     pub fn from_span_utils(tcx: &'l TyCtxt<'tcx>,
                            lcx: &'l lowering::LoweringContext<'l>,
-                           span_utils: SpanUtils<'l>)
+                           span_utils: SpanUtils<'tcx>)
                            -> SaveContext<'l, 'tcx> {
         SaveContext {
             tcx: tcx,
@@ -680,7 +680,7 @@ fn visit_pat(&mut self, p: &ast::Pat) {
 pub fn process_crate<'l, 'tcx>(tcx: &'l TyCtxt<'tcx>,
                                lcx: &'l lowering::LoweringContext<'l>,
                                krate: &ast::Crate,
-                               analysis: &ty::CrateAnalysis,
+                               analysis: &'l ty::CrateAnalysis<'l>,
                                cratename: &str,
                                odir: Option<&Path>) {
     let _ignore = tcx.dep_graph.in_ignore();
@@ -726,9 +726,10 @@ pub fn process_crate<'l, 'tcx>(tcx: &'l TyCtxt<'tcx>,
     });
     root_path.pop();
 
-    let utils = SpanUtils::new(&tcx.sess);
+    let utils: SpanUtils<'tcx> = SpanUtils::new(&tcx.sess);
+    let save_ctxt = SaveContext::new(tcx, lcx);
     let mut dumper = CsvDumper::new(&mut output_file, utils);
-    let mut visitor = DumpVisitor::new(tcx, lcx, analysis, &mut dumper);
+    let mut visitor = DumpVisitor::new(tcx, save_ctxt, analysis, &mut dumper);
     // FIXME: we don't write anything!
 
     visitor.dump_crate_info(cratename, krate);
index f410d428177cd6a920706d9d84d3970183bf60ec..c64eeb92737e05ebcdbe2c62db8eeeb3f45d9bde 100644 (file)
@@ -26,6 +26,8 @@
 #[derive(Clone)]
 pub struct SpanUtils<'a> {
     pub sess: &'a Session,
+    // FIXME given that we clone SpanUtils all over the place, this err_count is
+    // probably useless and any logic relying on it is bogus.
     pub err_count: Cell<isize>,
 }
 
index b7c60b8a524c0881780a8bc20253a1c01a856527..6d1e91a687e586074115bd8bb07654073643c28d 100644 (file)
@@ -22,6 +22,7 @@
 use rustc_resolve as resolve;
 use rustc::hir::lowering::{lower_crate, LoweringContext};
 use rustc_metadata::cstore::CStore;
+use rustc_metadata::creader::LocalCrateReader;
 
 use syntax::{ast, codemap, errors};
 use syntax::errors::emitter::ColorConfig;
@@ -151,14 +152,18 @@ pub fn run_core(search_paths: SearchPaths,
                     .expect("phase_2_configure_and_expand aborted in rustdoc!");
 
     let krate = driver::assign_node_ids(&sess, krate);
+    let dep_graph = DepGraph::new(false);
+
+    let defs = &RefCell::new(hir_map::collect_definitions(&krate));
+    LocalCrateReader::new(&sess, &cstore, &defs, &krate, &name).read_crates(&dep_graph);
+    let lcx = LoweringContext::new(&sess, Some(&krate), defs);
+
     // Lower ast -> hir.
-    let lcx = LoweringContext::new(&sess, Some(&krate));
-    let mut hir_forest = hir_map::Forest::new(lower_crate(&lcx, &krate), DepGraph::new(false));
+    let mut hir_forest = hir_map::Forest::new(lower_crate(&lcx, &krate), dep_graph);
     let arenas = ty::CtxtArenas::new();
-    let hir_map = driver::make_map(&sess, &mut hir_forest);
+    let hir_map = hir_map::map_crate(&mut hir_forest, defs);
 
     abort_on_err(driver::phase_3_run_analysis_passes(&sess,
-                                                     &cstore,
                                                      hir_map,
                                                      &arenas,
                                                      &name,
index 5a7050fb42f3045a8115d0ecd616f27247e60da3..487aac1806ea7291e7e1e7122af1f4f604a4a1ed 100644 (file)
@@ -94,15 +94,17 @@ pub fn run(input: &str,
                                                      "rustdoc-test", None)
         .expect("phase_2_configure_and_expand aborted in rustdoc!");
     let krate = driver::assign_node_ids(&sess, krate);
-    let lcx = LoweringContext::new(&sess, Some(&krate));
+    let dep_graph = DepGraph::new(false);
+    let defs = &RefCell::new(hir_map::collect_definitions(&krate));
+
+    let lcx = LoweringContext::new(&sess, Some(&krate), defs);
     let krate = lower_crate(&lcx, &krate);
 
     let opts = scrape_test_config(&krate);
 
-    let dep_graph = DepGraph::new(false);
     let _ignore = dep_graph.in_ignore();
     let mut forest = hir_map::Forest::new(krate, dep_graph.clone());
-    let map = hir_map::map_crate(&mut forest);
+    let map = hir_map::map_crate(&mut forest, defs);
 
     let ctx = core::DocContext {
         map: &map,
index 12cc475f121f6fbe0608ee73c8e8c9e0fae53733..86b9c4c81c66b18922cc9d65a729dee292986ac8 100644 (file)
@@ -20,6 +20,7 @@
 extern crate rustc_resolve;
 #[macro_use] extern crate syntax;
 
+use std::cell::RefCell;
 use std::ffi::{CStr, CString};
 use std::mem::transmute;
 use std::path::PathBuf;
@@ -35,6 +36,7 @@
 use rustc_driver::{driver, abort_on_err};
 use rustc::hir::lowering::{lower_crate, LoweringContext};
 use rustc_resolve::MakeGlobMap;
+use rustc_metadata::creader::LocalCrateReader;
 use rustc_metadata::cstore::CStore;
 use libc::c_void;
 
@@ -237,15 +239,17 @@ fn compile_program(input: &str, sysroot: PathBuf)
         let krate = driver::phase_2_configure_and_expand(&sess, &cstore, krate, &id, None)
             .expect("phase_2 returned `None`");
 
+        let dep_graph = DepGraph::new(sess.opts.build_dep_graph);
         let krate = driver::assign_node_ids(&sess, krate);
-        let lcx = LoweringContext::new(&sess, Some(&krate));
-        let dep_graph = DepGraph::new(sess.opts.build_dep_graph());
+        let defs = RefCell::new(ast_map::collect_definitions(&krate));
+        LocalCrateReader::new(&sess, &cstore, &defs, &krate, &id).read_crates(&dep_graph);
+        let lcx = LoweringContext::new(&sess, Some(&krate), &defs);
         let mut hir_forest = ast_map::Forest::new(lower_crate(&lcx, &krate), dep_graph);
         let arenas = ty::CtxtArenas::new();
-        let ast_map = driver::make_map(&sess, &mut hir_forest);
+        let ast_map = ast_map::map_crate(&mut hir_forest, &defs);
 
         abort_on_err(driver::phase_3_run_analysis_passes(
-            &sess, &cstore, ast_map, &arenas, &id,
+            &sess, ast_map, &arenas, &id,
             MakeGlobMap::No, |tcx, mir_map, analysis, _| {
 
             let trans = driver::phase_4_translate_to_llvm(tcx, mir_map.unwrap(), analysis);