Skip to content

Commit fb4710c

Browse files
committed
Add a field in Module for the ResolverArenas
1 parent 6b94bc3 commit fb4710c

File tree

3 files changed

+39
-27
lines changed

3 files changed

+39
-27
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
9898
fn try_define<T>(&self, parent: Module<'b>, name: Name, ns: Namespace, def: T)
9999
where T: ToNameBinding<'b>
100100
{
101-
let _ = parent.try_define_child(name, ns, self.new_name_binding(def.to_name_binding()));
101+
let _ = parent.try_define_child(name, ns, def.to_name_binding());
102102
}
103103

104104
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
105105
/// otherwise, reports an error.
106106
fn define<T: ToNameBinding<'b>>(&self, parent: Module<'b>, name: Name, ns: Namespace, def: T) {
107-
let binding = self.new_name_binding(def.to_name_binding());
108-
let old_binding = match parent.try_define_child(name, ns, binding) {
107+
let binding = def.to_name_binding();
108+
let old_binding = match parent.try_define_child(name, ns, binding.clone()) {
109109
Ok(()) => return,
110110
Err(old_binding) => old_binding,
111111
};
@@ -709,8 +709,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
709709

710710
let directive =
711711
ImportDirective::new(module_path, subclass, span, id, is_public, shadowable);
712-
let directive = self.resolver.arenas.alloc_import_directive(directive);
713-
module_.unresolved_imports.borrow_mut().push(directive);
712+
module_.add_import_directive(directive);
714713
self.unresolved_imports += 1;
715714
}
716715
}

src/librustc_resolve/lib.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -845,13 +845,18 @@ pub struct ModuleS<'a> {
845845
// access the children must be preceded with a
846846
// `populate_module_if_necessary` call.
847847
populated: Cell<bool>,
848+
849+
arenas: &'a ResolverArenas<'a>,
848850
}
849851

850852
pub type Module<'a> = &'a ModuleS<'a>;
851853

852854
impl<'a> ModuleS<'a> {
853-
854-
fn new(parent_link: ParentLink<'a>, def: Option<Def>, external: bool, is_public: bool) -> Self {
855+
fn new(parent_link: ParentLink<'a>,
856+
def: Option<Def>,
857+
external: bool,
858+
is_public: bool,
859+
arenas: &'a ResolverArenas<'a>) -> Self {
855860
ModuleS {
856861
parent_link: parent_link,
857862
def: def,
@@ -865,6 +870,7 @@ impl<'a> ModuleS<'a> {
865870
pub_count: Cell::new(0),
866871
pub_glob_count: Cell::new(0),
867872
populated: Cell::new(!external),
873+
arenas: arenas
868874
}
869875
}
870876

@@ -881,8 +887,9 @@ impl<'a> ModuleS<'a> {
881887
}
882888

883889
// Define the name or return the existing binding if there is a collision.
884-
fn try_define_child(&self, name: Name, ns: Namespace, binding: &'a NameBinding<'a>)
890+
fn try_define_child(&self, name: Name, ns: Namespace, binding: NameBinding<'a>)
885891
-> Result<(), &'a NameBinding<'a>> {
892+
let binding = self.arenas.alloc_name_binding(binding);
886893
let mut children = self.resolutions.borrow_mut();
887894
let resolution = children.entry((name, ns)).or_insert_with(Default::default);
888895

@@ -898,6 +905,11 @@ impl<'a> ModuleS<'a> {
898905
resolution.try_define(binding)
899906
}
900907

908+
fn add_import_directive(&self, import_directive: ImportDirective) {
909+
let import_directive = self.arenas.alloc_import_directive(import_directive);
910+
self.unresolved_imports.borrow_mut().push(import_directive);
911+
}
912+
901913
fn increment_outstanding_references_for(&self, name: Name, ns: Namespace) {
902914
let mut children = self.resolutions.borrow_mut();
903915
children.entry((name, ns)).or_insert_with(Default::default).outstanding_references += 1;
@@ -995,14 +1007,14 @@ bitflags! {
9951007
}
9961008

9971009
// Records a possibly-private value, type, or module definition.
998-
#[derive(Debug)]
1010+
#[derive(Clone, Debug)]
9991011
pub struct NameBinding<'a> {
10001012
modifiers: DefModifiers,
10011013
kind: NameBindingKind<'a>,
10021014
span: Option<Span>,
10031015
}
10041016

1005-
#[derive(Debug)]
1017+
#[derive(Clone, Debug)]
10061018
enum NameBindingKind<'a> {
10071019
Def(Def),
10081020
Module(Module<'a>),
@@ -1171,6 +1183,12 @@ pub struct ResolverArenas<'a> {
11711183
}
11721184

11731185
impl<'a> ResolverArenas<'a> {
1186+
fn alloc_module(&'a self, module: ModuleS<'a>) -> Module<'a> {
1187+
self.modules.alloc(module)
1188+
}
1189+
fn alloc_name_binding(&'a self, name_binding: NameBinding<'a>) -> &'a NameBinding<'a> {
1190+
self.name_bindings.alloc(name_binding)
1191+
}
11741192
fn alloc_import_directive(&'a self, import_directive: ImportDirective) -> &'a ImportDirective {
11751193
self.import_directives.alloc(import_directive)
11761194
}
@@ -1189,8 +1207,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
11891207
arenas: &'a ResolverArenas<'a>)
11901208
-> Resolver<'a, 'tcx> {
11911209
let root_def_id = ast_map.local_def_id(CRATE_NODE_ID);
1192-
let graph_root = ModuleS::new(NoParentLink, Some(Def::Mod(root_def_id)), false, true);
1193-
let graph_root = arenas.modules.alloc(graph_root);
1210+
let graph_root =
1211+
ModuleS::new(NoParentLink, Some(Def::Mod(root_def_id)), false, true, arenas);
1212+
let graph_root = arenas.alloc_module(graph_root);
11941213

11951214
Resolver {
11961215
session: session,
@@ -1250,11 +1269,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12501269
def: Option<Def>,
12511270
external: bool,
12521271
is_public: bool) -> Module<'a> {
1253-
self.arenas.modules.alloc(ModuleS::new(parent_link, def, external, is_public))
1254-
}
1255-
1256-
fn new_name_binding(&self, name_binding: NameBinding<'a>) -> &'a NameBinding<'a> {
1257-
self.arenas.name_bindings.alloc(name_binding)
1272+
self.arenas.alloc_module(ModuleS::new(parent_link, def, external, is_public, self.arenas))
12581273
}
12591274

12601275
fn new_extern_crate_module(&self,
@@ -1263,7 +1278,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12631278
is_public: bool,
12641279
local_node_id: NodeId)
12651280
-> Module<'a> {
1266-
let mut module = ModuleS::new(parent_link, Some(def), false, is_public);
1281+
let mut module = ModuleS::new(parent_link, Some(def), false, is_public, self.arenas);
12671282
module.extern_crate_id = Some(local_node_id);
12681283
self.arenas.modules.alloc(module)
12691284
}

src/librustc_resolve/resolve_imports.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,14 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
236236
// If it's a single failed import then create a "fake" import
237237
// resolution for it so that later resolve stages won't complain.
238238
if let SingleImport { target, .. } = e.import_directive.subclass {
239-
let dummy_binding = self.resolver.new_name_binding(NameBinding {
239+
let dummy_binding = self.resolver.arenas.alloc_name_binding(NameBinding {
240240
modifiers: DefModifiers::PRELUDE,
241241
kind: NameBindingKind::Def(Def::Err),
242242
span: None,
243243
});
244-
let dummy_binding =
245-
self.resolver.new_name_binding(e.import_directive.import(dummy_binding, None));
244+
let dummy_binding = e.import_directive.import(dummy_binding, None);
246245

247-
let _ = e.source_module.try_define_child(target, ValueNS, dummy_binding);
246+
let _ = e.source_module.try_define_child(target, ValueNS, dummy_binding.clone());
248247
let _ = e.source_module.try_define_child(target, TypeNS, dummy_binding);
249248
}
250249

@@ -534,9 +533,8 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
534533
name: Name,
535534
ns: Namespace,
536535
binding: NameBinding<'b>) {
537-
let binding = self.resolver.new_name_binding(binding);
538-
if let Err(old_binding) = parent.try_define_child(name, ns, binding) {
539-
self.report_conflict(name, ns, binding, old_binding);
536+
if let Err(old_binding) = parent.try_define_child(name, ns, binding.clone()) {
537+
self.report_conflict(name, ns, &binding, old_binding);
540538
} else if binding.is_public() { // Add to the export map
541539
if let (Some(parent_def_id), Some(def)) = (parent.def_id(), binding.def()) {
542540
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> {
549547
fn report_conflict(&mut self,
550548
name: Name,
551549
ns: Namespace,
552-
binding: &'b NameBinding<'b>,
553-
old_binding: &'b NameBinding<'b>) {
550+
binding: &NameBinding,
551+
old_binding: &NameBinding) {
554552
// Error on the second of two conflicting imports
555553
if old_binding.is_import() && binding.is_import() &&
556554
old_binding.span.unwrap().lo > binding.span.unwrap().lo {

0 commit comments

Comments
 (0)