From 638bd4edb0f424d84a4d5dca41bbc003f7a80764 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Sat, 30 Jan 2016 18:23:19 +0300 Subject: [PATCH] resolve: Deduplicate module reexport sets --- src/librustc/middle/def.rs | 5 +++-- src/librustc_resolve/build_reduced_graph.rs | 23 +++++++-------------- src/librustc_resolve/resolve_imports.rs | 2 +- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/src/librustc/middle/def.rs b/src/librustc/middle/def.rs index be967da2713b3..cf7630c663460 100644 --- a/src/librustc/middle/def.rs +++ b/src/librustc/middle/def.rs @@ -14,6 +14,7 @@ use middle::subst::ParamSpace; use util::nodemap::NodeMap; use syntax::ast; use rustc_front::hir; +use std::collections::HashSet; #[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)] pub enum Def { @@ -99,9 +100,9 @@ impl PathResolution { pub type DefMap = NodeMap; // This is the replacement export map. It maps a module to all of the exports // within. -pub type ExportMap = NodeMap>; +pub type ExportMap = NodeMap>; -#[derive(Copy, Clone)] +#[derive(Copy, Clone, PartialEq, Eq, Hash)] pub struct Export { pub name: ast::Name, // The name of the target. pub def_id: DefId, // The definition of the target. diff --git a/src/librustc_resolve/build_reduced_graph.rs b/src/librustc_resolve/build_reduced_graph.rs index a1d866fc48be4..99366becc3556 100644 --- a/src/librustc_resolve/build_reduced_graph.rs +++ b/src/librustc_resolve/build_reduced_graph.rs @@ -97,13 +97,6 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> { intravisit::walk_crate(&mut visitor, krate); } - /// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined. - fn try_define(&self, parent: Module<'b>, name: Name, ns: Namespace, def: T) - where T: ToNameBinding<'b> - { - 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) { @@ -517,7 +510,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> { is_public); let parent_link = ModuleParentLink(new_parent, name); let module = self.new_module(parent_link, Some(def), true, is_public); - self.try_define(new_parent, name, TypeNS, (module, DUMMY_SP)); + self.define(new_parent, name, TypeNS, (module, DUMMY_SP)); } Def::Variant(_, variant_id) => { debug!("(building reduced graph for external crate) building variant {}", @@ -525,8 +518,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> { // Variants are always treated as importable to allow them to be glob used. // All variants are defined in both type and value namespaces as future-proofing. let modifiers = DefModifiers::PUBLIC | DefModifiers::IMPORTABLE; - self.try_define(new_parent, name, TypeNS, (def, DUMMY_SP, modifiers)); - self.try_define(new_parent, name, ValueNS, (def, DUMMY_SP, modifiers)); + self.define(new_parent, name, TypeNS, (def, DUMMY_SP, modifiers)); + self.define(new_parent, name, ValueNS, (def, DUMMY_SP, modifiers)); if self.session.cstore.variant_kind(variant_id) == Some(VariantKind::Struct) { // Not adding fields for variants as they are not accessed with a self receiver self.structs.insert(variant_id, Vec::new()); @@ -539,7 +532,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> { Def::Method(..) => { debug!("(building reduced graph for external crate) building value (fn/static) {}", final_ident); - self.try_define(new_parent, name, ValueNS, (def, DUMMY_SP, modifiers)); + self.define(new_parent, name, ValueNS, (def, DUMMY_SP, modifiers)); } Def::Trait(def_id) => { debug!("(building reduced graph for external crate) building type {}", @@ -566,22 +559,22 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> { let parent_link = ModuleParentLink(new_parent, name); let module = self.new_module(parent_link, Some(def), true, is_public); - self.try_define(new_parent, name, TypeNS, (module, DUMMY_SP)); + self.define(new_parent, name, TypeNS, (module, DUMMY_SP)); } Def::AssociatedTy(..) => { debug!("(building reduced graph for external crate) building type {}", final_ident); - self.try_define(new_parent, name, TypeNS, (def, DUMMY_SP, modifiers)); + self.define(new_parent, name, TypeNS, (def, DUMMY_SP, modifiers)); } Def::Struct(def_id) if self.session.cstore.tuple_struct_definition_if_ctor(def_id).is_none() => { debug!("(building reduced graph for external crate) building type and value for \ {}", final_ident); - self.try_define(new_parent, name, TypeNS, (def, DUMMY_SP, modifiers)); + self.define(new_parent, name, TypeNS, (def, DUMMY_SP, modifiers)); if let Some(ctor_def_id) = self.session.cstore.struct_ctor_def_id(def_id) { let def = Def::Struct(ctor_def_id); - self.try_define(new_parent, name, ValueNS, (def, DUMMY_SP, modifiers)); + self.define(new_parent, name, ValueNS, (def, DUMMY_SP, modifiers)); } // Record the def ID and fields of this struct. diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index 07f6a0f954990..3e8b716142b13 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -819,7 +819,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> { Some(def) => Export { name: name, def_id: def.def_id() }, None => return, }; - self.resolver.export_map.entry(node_id).or_insert(Vec::new()).push(export); + self.resolver.export_map.entry(node_id).or_insert(Default::default()).insert(export); } /// Checks that imported names and items don't have the same name.