Skip to content

Commit ceda838

Browse files
committed
Replace TypeNsDef and ValueNsDef with a more general type NsDef.
Define a newtype `NameBinding` for `Rc<RefCell<Option<NsDef>>>` and refactor `NameBindings` to be a `NameBinding` for each namespace. Replace uses of `NameBindings` with `NameBinding` where only one binding is being used (in `NamespaceResult`, `Target,` etc). Refactor away `resolve_definition_of_name_in_module` and `NameDefinition`.
1 parent b7845f9 commit ceda838

File tree

4 files changed

+236
-478
lines changed

4 files changed

+236
-478
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use {names_to_string, module_to_string};
2525
use ParentLink::{self, ModuleParentLink, BlockParentLink};
2626
use Resolver;
2727
use resolve_imports::Shadowable;
28-
use TypeNsDef;
2928
use {resolve_error, ResolutionError};
3029

3130
use self::DuplicateCheckingMode::*;
@@ -130,7 +129,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
130129
duplicate_checking_mode: DuplicateCheckingMode,
131130
// For printing errors
132131
sp: Span)
133-
-> Rc<NameBindings> {
132+
-> NameBindings {
134133
// If this is the immediate descendant of a module, then we add the
135134
// child name directly. Otherwise, we create or reuse an anonymous
136135
// module and add the child to that.
@@ -141,7 +140,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
141140
let child = parent.children.borrow().get(&name).cloned();
142141
match child {
143142
None => {
144-
let child = Rc::new(NameBindings::new());
143+
let child = NameBindings::new();
145144
parent.children.borrow_mut().insert(name, child.clone());
146145
child
147146
}
@@ -173,27 +172,27 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
173172
Some(TypeNS)
174173
}
175174
ForbidDuplicateTypesAndModules => {
176-
if child.defined_in_namespace(TypeNS) {
175+
if child.type_ns.defined() {
177176
duplicate_type = TypeError;
178177
}
179178
Some(TypeNS)
180179
}
181180
ForbidDuplicateValues => {
182-
if child.defined_in_namespace(ValueNS) {
181+
if child.value_ns.defined() {
183182
duplicate_type = ValueError;
184183
}
185184
Some(ValueNS)
186185
}
187186
ForbidDuplicateTypesAndValues => {
188187
let mut n = None;
189-
match child.def_for_namespace(TypeNS) {
188+
match child.type_ns.def() {
190189
Some(DefMod(_)) | None => {}
191190
Some(_) => {
192191
n = Some(TypeNS);
193192
duplicate_type = TypeError;
194193
}
195194
}
196-
if child.defined_in_namespace(ValueNS) {
195+
if child.value_ns.defined() {
197196
duplicate_type = ValueError;
198197
n = Some(ValueNS);
199198
}
@@ -213,7 +212,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
213212
name)
214213
);
215214
{
216-
let r = child.span_for_namespace(ns);
215+
let r = child[ns].span();
217216
if let Some(sp) = r {
218217
self.session.span_note(sp,
219218
&format!("first definition of {} `{}` here",
@@ -415,7 +414,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
415414
let child = parent.children.borrow().get(&name).cloned();
416415
if let Some(child) = child {
417416
// check if there's struct of the same name already defined
418-
if child.defined_in_namespace(TypeNS) &&
417+
if child.type_ns.defined() &&
419418
child.get_module_if_available().is_none() {
420419
self.session.span_warn(sp,
421420
&format!("duplicate definition of {} `{}`. \
@@ -424,7 +423,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
424423
namespace_error_to_string(TypeError),
425424
name));
426425
{
427-
let r = child.span_for_namespace(TypeNS);
426+
let r = child.type_ns.span();
428427
if let Some(sp) = r {
429428
self.session.span_note(sp,
430429
&format!("first definition of {} `{}` here",
@@ -530,7 +529,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
530529
let child = parent.children.borrow().get(&name).cloned();
531530
if let Some(child) = child {
532531
// check if theres a DefMod
533-
if let Some(DefMod(_)) = child.def_for_namespace(TypeNS) {
532+
if let Some(DefMod(_)) = child.type_ns.def() {
534533
self.session.span_warn(sp,
535534
&format!("duplicate definition of {} `{}`. \
536535
Defining a module and a struct \
@@ -539,7 +538,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
539538
namespace_error_to_string(TypeError),
540539
name));
541540
{
542-
let r = child.span_for_namespace(TypeNS);
541+
let r = child.type_ns.span();
543542
if let Some(sp) = r {
544543
self.session
545544
.span_note(sp,
@@ -752,26 +751,21 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
752751
DefForeignMod(def_id) |
753752
DefStruct(def_id) |
754753
DefTy(def_id, _) => {
755-
let type_def = child_name_bindings.type_def.borrow().clone();
756-
match type_def {
757-
Some(TypeNsDef { module_def: Some(module_def), .. }) => {
758-
debug!("(building reduced graph for external crate) already created \
759-
module");
760-
module_def.def_id.set(Some(def_id));
761-
}
762-
Some(_) | None => {
763-
debug!("(building reduced graph for external crate) building module {} {}",
764-
final_ident,
765-
is_public);
766-
let parent_link = self.get_parent_link(new_parent, name);
767-
768-
child_name_bindings.define_module(parent_link,
769-
Some(def_id),
770-
kind,
771-
true,
772-
is_public,
773-
DUMMY_SP);
774-
}
754+
if let Some(module_def) = child_name_bindings.type_ns.module() {
755+
debug!("(building reduced graph for external crate) already created module");
756+
module_def.def_id.set(Some(def_id));
757+
} else {
758+
debug!("(building reduced graph for external crate) building module {} {}",
759+
final_ident,
760+
is_public);
761+
let parent_link = self.get_parent_link(new_parent, name);
762+
763+
child_name_bindings.define_module(parent_link,
764+
Some(def_id),
765+
kind,
766+
true,
767+
is_public,
768+
DUMMY_SP);
775769
}
776770
}
777771
_ => {}
@@ -807,7 +801,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
807801
final_ident);
808802
// impl methods have already been defined with the correct importability
809803
// modifier
810-
let mut modifiers = match *child_name_bindings.value_def.borrow() {
804+
let mut modifiers = match *child_name_bindings.value_ns.borrow() {
811805
Some(ref def) => (modifiers & !DefModifiers::IMPORTABLE) |
812806
(def.modifiers & DefModifiers::IMPORTABLE),
813807
None => modifiers,
@@ -922,7 +916,7 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
922916

923917
self.handle_external_def(def,
924918
def_visibility,
925-
&*child_name_bindings,
919+
&child_name_bindings,
926920
&name.as_str(),
927921
name,
928922
root);

0 commit comments

Comments
 (0)