]> git.lizzy.rs Git - rust.git/commitdiff
Refactor `no_implicit_prelude: Cell<bool>` -> `no_implicit_prelude: bool`.
authorJeffrey Seyfried <jeffrey.seyfried@gmail.com>
Mon, 19 Sep 2016 05:25:17 +0000 (05:25 +0000)
committerJeffrey Seyfried <jeffrey.seyfried@gmail.com>
Thu, 22 Sep 2016 07:45:11 +0000 (07:45 +0000)
src/librustc_driver/driver.rs
src/librustc_resolve/build_reduced_graph.rs
src/librustc_resolve/lib.rs

index 55892801247b5459a5420c43fadd2155c7b4510b..18ce35237afcb8d3dcbf4c5fa04e6a1c2256174e 100644 (file)
@@ -643,7 +643,8 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
         macro_import::MacroLoader::new(sess, &cstore, crate_name, krate.config.clone());
 
     let resolver_arenas = Resolver::arenas();
-    let mut resolver = Resolver::new(sess, make_glob_map, &mut macro_loader, &resolver_arenas);
+    let mut resolver =
+        Resolver::new(sess, &krate, make_glob_map, &mut macro_loader, &resolver_arenas);
     syntax_ext::register_builtins(&mut resolver, sess.features.borrow().quote);
 
     krate = time(time_passes, "expansion", || {
index c46e83fc400cab69f376691eb00e79277df4d460..3075e394869a5a35322555563ea5b7171314c12b 100644 (file)
@@ -14,7 +14,7 @@
 //! any imports resolved.
 
 use resolve_imports::ImportDirectiveSubclass::{self, GlobImport};
-use {Module, ModuleKind};
+use {Module, ModuleS, ModuleKind};
 use Namespace::{self, TypeNS, ValueNS};
 use {NameBinding, NameBindingKind, ToNameBinding};
 use Resolver;
@@ -55,8 +55,6 @@ fn to_name_binding(self) -> NameBinding<'a> {
 impl<'b> Resolver<'b> {
     /// Constructs the reduced graph for the entire crate.
     pub fn build_reduced_graph(&mut self, krate: &Crate) {
-        let no_implicit_prelude = attr::contains_name(&krate.attrs, "no_implicit_prelude");
-        self.graph_root.no_implicit_prelude.set(no_implicit_prelude);
         visit::walk_crate(&mut BuildReducedGraphVisitor { resolver: self }, krate);
     }
 
@@ -195,8 +193,11 @@ fn build_reduced_graph_for_item(&mut self, item: &Item) {
                         krate: crate_id,
                         index: CRATE_DEF_INDEX,
                     };
-                    let def = Def::Mod(def_id);
-                    let module = self.new_extern_crate_module(parent, name, def, item.id);
+                    let module = self.arenas.alloc_module(ModuleS {
+                        extern_crate_id: Some(item.id),
+                        populated: Cell::new(false),
+                        ..ModuleS::new(Some(parent), ModuleKind::Def(Def::Mod(def_id), name))
+                    });
                     self.define(parent, name, TypeNS, (module, sp, vis));
 
                     self.populate_module_if_necessary(module);
@@ -205,10 +206,12 @@ fn build_reduced_graph_for_item(&mut self, item: &Item) {
 
             ItemKind::Mod(..) => {
                 let def = Def::Mod(self.definitions.local_def_id(item.id));
-                let module = self.new_module(parent, ModuleKind::Def(def, name), Some(item.id));
-                module.no_implicit_prelude.set({
-                    parent.no_implicit_prelude.get() ||
+                let module = self.arenas.alloc_module(ModuleS {
+                    no_implicit_prelude: parent.no_implicit_prelude || {
                         attr::contains_name(&item.attrs, "no_implicit_prelude")
+                    },
+                    normal_ancestor_id: Some(item.id),
+                    ..ModuleS::new(Some(parent), ModuleKind::Def(def, name))
                 });
                 self.define(parent, name, TypeNS, (module, sp, vis));
                 self.module_map.insert(item.id, module);
@@ -241,8 +244,8 @@ fn build_reduced_graph_for_item(&mut self, item: &Item) {
             }
 
             ItemKind::Enum(ref enum_definition, _) => {
-                let kind = ModuleKind::Def(Def::Enum(self.definitions.local_def_id(item.id)), name);
-                let module = self.new_module(parent, kind, parent.normal_ancestor_id);
+                let def = Def::Enum(self.definitions.local_def_id(item.id));
+                let module = self.new_module(parent, ModuleKind::Def(def, name), true);
                 self.define(parent, name, TypeNS, (module, sp, vis));
 
                 for variant in &(*enum_definition).variants {
@@ -293,8 +296,8 @@ fn build_reduced_graph_for_item(&mut self, item: &Item) {
                 let def_id = self.definitions.local_def_id(item.id);
 
                 // Add all the items within to a new module.
-                let kind = ModuleKind::Def(Def::Trait(def_id), name);
-                let module = self.new_module(parent, kind, parent.normal_ancestor_id);
+                let module =
+                    self.new_module(parent, ModuleKind::Def(Def::Trait(def_id), name), true);
                 self.define(parent, name, TypeNS, (module, sp, vis));
                 self.current_module = module;
             }
@@ -348,8 +351,7 @@ fn build_reduced_graph_for_block(&mut self, block: &Block) {
                     {}",
                    block_id);
 
-            let new_module =
-                self.new_module(parent, ModuleKind::Block(block_id), parent.normal_ancestor_id);
+            let new_module = self.new_module(parent, ModuleKind::Block(block_id), true);
             self.module_map.insert(block_id, new_module);
             self.current_module = new_module; // Descend into the block.
         }
@@ -377,7 +379,7 @@ fn build_reduced_graph_for_external_crate_def(&mut self, parent: Module<'b>,
             Def::Mod(_) | Def::Enum(..) => {
                 debug!("(building reduced graph for external crate) building module {} {:?}",
                        name, vis);
-                let module = self.new_module(parent, ModuleKind::Def(def, name), None);
+                let module = self.new_module(parent, ModuleKind::Def(def, name), false);
                 let _ = self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
             }
             Def::Variant(variant_id) => {
@@ -420,7 +422,7 @@ fn build_reduced_graph_for_external_crate_def(&mut self, parent: Module<'b>,
                     self.trait_item_map.insert((trait_item_name, def_id), false);
                 }
 
-                let module = self.new_module(parent, ModuleKind::Def(def, name), None);
+                let module = self.new_module(parent, ModuleKind::Def(def, name), false);
                 let _ = self.try_define(parent, name, TypeNS, (module, DUMMY_SP, vis));
             }
             Def::TyAlias(..) | Def::AssociatedTy(..) => {
index 0a8514ffc73fbf202c093ca9c67eb3d86e8027d5..7a5f255aa9f33bb0b87cd4e174799f5056aed141 100644 (file)
@@ -60,6 +60,7 @@
 use syntax::util::lev_distance::find_best_match_for_name;
 
 use syntax::visit::{self, FnKind, Visitor};
+use syntax::attr;
 use syntax::ast::{Arm, BindingMode, Block, Crate, Expr, ExprKind};
 use syntax::ast::{FnDecl, ForeignItem, ForeignItemKind, Generics};
 use syntax::ast::{Item, ItemKind, ImplItem, ImplItemKind};
@@ -771,7 +772,7 @@ pub struct ModuleS<'a> {
 
     resolutions: RefCell<FnvHashMap<(Name, Namespace), &'a RefCell<NameResolution<'a>>>>,
 
-    no_implicit_prelude: Cell<bool>,
+    no_implicit_prelude: bool,
 
     glob_importers: RefCell<Vec<&'a ImportDirective<'a>>>,
     globs: RefCell<Vec<&'a ImportDirective<'a>>>,
@@ -788,19 +789,18 @@ pub struct ModuleS<'a> {
 pub type Module<'a> = &'a ModuleS<'a>;
 
 impl<'a> ModuleS<'a> {
-    fn new(parent: Option<Module<'a>>, kind: ModuleKind, normal_ancestor_id: Option<NodeId>)
-           -> Self {
+    fn new(parent: Option<Module<'a>>, kind: ModuleKind) -> Self {
         ModuleS {
             parent: parent,
             kind: kind,
-            normal_ancestor_id: normal_ancestor_id,
+            normal_ancestor_id: None,
             extern_crate_id: None,
             resolutions: RefCell::new(FnvHashMap()),
-            no_implicit_prelude: Cell::new(false),
+            no_implicit_prelude: false,
             glob_importers: RefCell::new(Vec::new()),
             globs: RefCell::new((Vec::new())),
             traits: RefCell::new(None),
-            populated: Cell::new(normal_ancestor_id.is_some()),
+            populated: Cell::new(true),
         }
     }
 
@@ -1170,14 +1170,17 @@ fn name(&self) -> Name {
 
 impl<'a> Resolver<'a> {
     pub fn new(session: &'a Session,
+               krate: &Crate,
                make_glob_map: MakeGlobMap,
                macro_loader: &'a mut MacroLoader,
                arenas: &'a ResolverArenas<'a>)
                -> Resolver<'a> {
-        let graph_root_kind =
-            ModuleKind::Def(Def::Mod(DefId::local(CRATE_DEF_INDEX)), keywords::Invalid.name());
-        let graph_root =
-            arenas.alloc_module(ModuleS::new(None, graph_root_kind, Some(CRATE_NODE_ID)));
+        let root_def = Def::Mod(DefId::local(CRATE_DEF_INDEX));
+        let graph_root = arenas.alloc_module(ModuleS {
+            normal_ancestor_id: Some(CRATE_NODE_ID),
+            no_implicit_prelude: attr::contains_name(&krate.attrs, "no_implicit_prelude"),
+            ..ModuleS::new(None, ModuleKind::Def(root_def, keywords::Invalid.name()))
+        });
         let mut module_map = NodeMap();
         module_map.insert(CRATE_NODE_ID, graph_root);
 
@@ -1259,17 +1262,12 @@ pub fn resolve_crate(&mut self, krate: &Crate) {
         self.report_errors();
     }
 
-    fn new_module(&self, parent: Module<'a>, kind: ModuleKind, normal_ancestor_id: Option<NodeId>)
-                  -> Module<'a> {
-        self.arenas.alloc_module(ModuleS::new(Some(parent), kind, normal_ancestor_id))
-    }
-
-    fn new_extern_crate_module(&self, parent: Module<'a>, name: Name, def: Def, node_id: NodeId)
-                               -> Module<'a> {
-        let mut module = ModuleS::new(Some(parent), ModuleKind::Def(def, name), Some(node_id));
-        module.extern_crate_id = Some(node_id);
-        module.populated.set(false);
-        self.arenas.modules.alloc(module)
+    fn new_module(&self, parent: Module<'a>, kind: ModuleKind, local: bool) -> Module<'a> {
+        self.arenas.alloc_module(ModuleS {
+            normal_ancestor_id: if local { self.current_module.normal_ancestor_id } else { None },
+            populated: Cell::new(local),
+            ..ModuleS::new(Some(parent), kind)
+        })
     }
 
     fn get_ribs<'b>(&'b mut self, ns: Namespace) -> &'b mut Vec<Rib<'a>> {
@@ -1509,7 +1507,7 @@ fn resolve_ident_in_lexical_scope(&mut self,
                 }
 
                 if let ModuleKind::Block(..) = module.kind { // We can see through blocks
-                } else if !module.no_implicit_prelude.get() {
+                } else if !module.no_implicit_prelude {
                     return self.prelude.and_then(|prelude| {
                         self.resolve_name_in_module(prelude, name, ns, false, None).success()
                     }).map(LexicalScopeBinding::Item)
@@ -3156,7 +3154,7 @@ fn add_trait_info(found_traits: &mut Vec<TraitCandidate>,
             if let ModuleKind::Block(..) = search_module.kind {
                 search_module = search_module.parent.unwrap();
             } else {
-                if !search_module.no_implicit_prelude.get() {
+                if !search_module.no_implicit_prelude {
                     self.prelude.map(|prelude| search_in_module(self, prelude));
                 }
                 break;