From fb4710ce21cb26f3caaa9d03ed0078e83018c88d Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Mon, 15 Feb 2016 05:18:55 +0000 Subject: [PATCH] Add a field in Module for the ResolverArenas --- src/librustc_resolve/build_reduced_graph.rs | 9 ++--- src/librustc_resolve/lib.rs | 41 ++++++++++++++------- src/librustc_resolve/resolve_imports.rs | 16 ++++---- 3 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index dff6926d2d34..f82cf98c2d6f 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -98,14 +98,14 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> { fn try_define(&self, parent: Module<'b>, name: Name, ns: Namespace, def: T) where T: ToNameBinding<'b> { - let _ = parent.try_define_child(name, ns, self.new_name_binding(def.to_name_binding())); + let _ = parent.try_define_child(name, ns, def.to_name_binding()); } /// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined; /// otherwise, reports an error. fn define>(&self, parent: Module<'b>, name: Name, ns: Namespace, def: T) { - let binding = self.new_name_binding(def.to_name_binding()); - let old_binding = match parent.try_define_child(name, ns, binding) { + let binding = def.to_name_binding(); + let old_binding = match parent.try_define_child(name, ns, binding.clone()) { Ok(()) => return, Err(old_binding) => old_binding, }; @@ -709,8 +709,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> { let directive = ImportDirective::new(module_path, subclass, span, id, is_public, shadowable); - let directive = self.resolver.arenas.alloc_import_directive(directive); - module_.unresolved_imports.borrow_mut().push(directive); + module_.add_import_directive(directive); self.unresolved_imports += 1; } } diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index 50e6e5c398c1..0e21b769b74c 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -845,13 +845,18 @@ pub struct ModuleS<'a> { // access the children must be preceded with a // `populate_module_if_necessary` call. populated: Cell, + + arenas: &'a ResolverArenas<'a>, } pub type Module<'a> = &'a ModuleS<'a>; impl<'a> ModuleS<'a> { - - fn new(parent_link: ParentLink<'a>, def: Option, external: bool, is_public: bool) -> Self { + fn new(parent_link: ParentLink<'a>, + def: Option, + external: bool, + is_public: bool, + arenas: &'a ResolverArenas<'a>) -> Self { ModuleS { parent_link: parent_link, def: def, @@ -865,6 +870,7 @@ impl<'a> ModuleS<'a> { pub_count: Cell::new(0), pub_glob_count: Cell::new(0), populated: Cell::new(!external), + arenas: arenas } } @@ -881,8 +887,9 @@ impl<'a> ModuleS<'a> { } // Define the name or return the existing binding if there is a collision. - fn try_define_child(&self, name: Name, ns: Namespace, binding: &'a NameBinding<'a>) + fn try_define_child(&self, name: Name, ns: Namespace, binding: NameBinding<'a>) -> Result<(), &'a NameBinding<'a>> { + let binding = self.arenas.alloc_name_binding(binding); let mut children = self.resolutions.borrow_mut(); let resolution = children.entry((name, ns)).or_insert_with(Default::default); @@ -898,6 +905,11 @@ impl<'a> ModuleS<'a> { resolution.try_define(binding) } + fn add_import_directive(&self, import_directive: ImportDirective) { + let import_directive = self.arenas.alloc_import_directive(import_directive); + self.unresolved_imports.borrow_mut().push(import_directive); + } + fn increment_outstanding_references_for(&self, name: Name, ns: Namespace) { let mut children = self.resolutions.borrow_mut(); children.entry((name, ns)).or_insert_with(Default::default).outstanding_references += 1; @@ -995,14 +1007,14 @@ bitflags! { } // Records a possibly-private value, type, or module definition. -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct NameBinding<'a> { modifiers: DefModifiers, kind: NameBindingKind<'a>, span: Option, } -#[derive(Debug)] +#[derive(Clone, Debug)] enum NameBindingKind<'a> { Def(Def), Module(Module<'a>), @@ -1171,6 +1183,12 @@ pub struct ResolverArenas<'a> { } impl<'a> ResolverArenas<'a> { + fn alloc_module(&'a self, module: ModuleS<'a>) -> Module<'a> { + self.modules.alloc(module) + } + fn alloc_name_binding(&'a self, name_binding: NameBinding<'a>) -> &'a NameBinding<'a> { + self.name_bindings.alloc(name_binding) + } fn alloc_import_directive(&'a self, import_directive: ImportDirective) -> &'a ImportDirective { self.import_directives.alloc(import_directive) } @@ -1189,8 +1207,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { arenas: &'a ResolverArenas<'a>) -> Resolver<'a, 'tcx> { let root_def_id = ast_map.local_def_id(CRATE_NODE_ID); - let graph_root = ModuleS::new(NoParentLink, Some(Def::Mod(root_def_id)), false, true); - let graph_root = arenas.modules.alloc(graph_root); + let graph_root = + ModuleS::new(NoParentLink, Some(Def::Mod(root_def_id)), false, true, arenas); + let graph_root = arenas.alloc_module(graph_root); Resolver { session: session, @@ -1250,11 +1269,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { def: Option, external: bool, is_public: bool) -> Module<'a> { - self.arenas.modules.alloc(ModuleS::new(parent_link, def, external, is_public)) - } - - fn new_name_binding(&self, name_binding: NameBinding<'a>) -> &'a NameBinding<'a> { - self.arenas.name_bindings.alloc(name_binding) + self.arenas.alloc_module(ModuleS::new(parent_link, def, external, is_public, self.arenas)) } fn new_extern_crate_module(&self, @@ -1263,7 +1278,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { is_public: bool, local_node_id: NodeId) -> Module<'a> { - let mut module = ModuleS::new(parent_link, Some(def), false, is_public); + let mut module = ModuleS::new(parent_link, Some(def), false, is_public, self.arenas); module.extern_crate_id = Some(local_node_id); self.arenas.modules.alloc(module) } diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index d68ceb81e2a7..15c9528d7ef1 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -236,15 +236,14 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> { // If it's a single failed import then create a "fake" import // resolution for it so that later resolve stages won't complain. if let SingleImport { target, .. } = e.import_directive.subclass { - let dummy_binding = self.resolver.new_name_binding(NameBinding { + let dummy_binding = self.resolver.arenas.alloc_name_binding(NameBinding { modifiers: DefModifiers::PRELUDE, kind: NameBindingKind::Def(Def::Err), span: None, }); - let dummy_binding = - self.resolver.new_name_binding(e.import_directive.import(dummy_binding, None)); + let dummy_binding = e.import_directive.import(dummy_binding, None); - let _ = e.source_module.try_define_child(target, ValueNS, dummy_binding); + let _ = e.source_module.try_define_child(target, ValueNS, dummy_binding.clone()); let _ = e.source_module.try_define_child(target, TypeNS, dummy_binding); } @@ -534,9 +533,8 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> { name: Name, ns: Namespace, binding: NameBinding<'b>) { - let binding = self.resolver.new_name_binding(binding); - if let Err(old_binding) = parent.try_define_child(name, ns, binding) { - self.report_conflict(name, ns, binding, old_binding); + if let Err(old_binding) = parent.try_define_child(name, ns, binding.clone()) { + self.report_conflict(name, ns, &binding, old_binding); } else if binding.is_public() { // Add to the export map if let (Some(parent_def_id), Some(def)) = (parent.def_id(), binding.def()) { let parent_node_id = self.resolver.ast_map.as_local_node_id(parent_def_id).unwrap(); @@ -549,8 +547,8 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> { fn report_conflict(&mut self, name: Name, ns: Namespace, - binding: &'b NameBinding<'b>, - old_binding: &'b NameBinding<'b>) { + binding: &NameBinding, + old_binding: &NameBinding) { // Error on the second of two conflicting imports if old_binding.is_import() && binding.is_import() && old_binding.span.unwrap().lo > binding.span.unwrap().lo {