Skip to content

Commit 9a58808

Browse files
committed
Little bit of refactoring in resolve
1 parent 9c1567e commit 9a58808

File tree

1 file changed

+94
-99
lines changed

1 file changed

+94
-99
lines changed

src/librustc_resolve/lib.rs

Lines changed: 94 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,22 @@ impl ImportResolution {
459459

460460
target.unwrap().shadowable
461461
}
462+
463+
fn set_target_and_id(&mut self,
464+
namespace: Namespace,
465+
target: Option<Target>,
466+
id: NodeId) {
467+
match namespace {
468+
TypeNS => {
469+
self.type_target = target;
470+
self.type_id = id;
471+
}
472+
ValueNS => {
473+
self.value_target = target;
474+
self.value_id = id;
475+
}
476+
}
477+
}
462478
}
463479

464480
/// The link from a module up to its nearest parent node.
@@ -2705,64 +2721,45 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
27052721
// We've successfully resolved the import. Write the results in.
27062722
let mut import_resolutions = module_.import_resolutions.borrow_mut();
27072723
let import_resolution = &mut (*import_resolutions)[target];
2724+
{
2725+
let check_and_write_import = |namespace, result: &_, used_public: &mut bool| {
2726+
let namespace_name = match namespace {
2727+
TypeNS => "type",
2728+
ValueNS => "value",
2729+
};
27082730

2709-
match value_result {
2710-
BoundResult(ref target_module, ref name_bindings) => {
2711-
debug!("(resolving single import) found value target: {}",
2712-
{ name_bindings.value_def.borrow().clone().unwrap().def });
2713-
self.check_for_conflicting_import(
2714-
&import_resolution.value_target,
2715-
directive.span,
2716-
target,
2717-
ValueNS);
2718-
2719-
self.check_that_import_is_importable(
2720-
&**name_bindings,
2721-
directive.span,
2722-
target,
2723-
ValueNS);
2724-
2725-
import_resolution.value_target =
2726-
Some(Target::new(target_module.clone(),
2727-
name_bindings.clone(),
2728-
directive.shadowable));
2729-
import_resolution.value_id = directive.id;
2730-
import_resolution.is_public = directive.is_public;
2731-
value_used_public = name_bindings.defined_in_public_namespace(ValueNS);
2732-
}
2733-
UnboundResult => { /* Continue. */ }
2734-
UnknownResult => {
2735-
panic!("value result should be known at this point");
2736-
}
2737-
}
2738-
match type_result {
2739-
BoundResult(ref target_module, ref name_bindings) => {
2740-
debug!("(resolving single import) found type target: {}",
2741-
{ name_bindings.type_def.borrow().clone().unwrap().type_def });
2742-
self.check_for_conflicting_import(
2743-
&import_resolution.type_target,
2744-
directive.span,
2745-
target,
2746-
TypeNS);
2747-
2748-
self.check_that_import_is_importable(
2749-
&**name_bindings,
2750-
directive.span,
2751-
target,
2752-
TypeNS);
2753-
2754-
import_resolution.type_target =
2755-
Some(Target::new(target_module.clone(),
2756-
name_bindings.clone(),
2757-
directive.shadowable));
2758-
import_resolution.type_id = directive.id;
2759-
import_resolution.is_public = directive.is_public;
2760-
type_used_public = name_bindings.defined_in_public_namespace(TypeNS);
2761-
}
2762-
UnboundResult => { /* Continue. */ }
2763-
UnknownResult => {
2764-
panic!("type result should be known at this point");
2765-
}
2731+
match *result {
2732+
BoundResult(ref target_module, ref name_bindings) => {
2733+
debug!("(resolving single import) found {} target: {}",
2734+
namespace_name,
2735+
name_bindings.def_for_namespace(namespace));
2736+
self.check_for_conflicting_import(
2737+
&import_resolution.target_for_namespace(namespace),
2738+
directive.span,
2739+
target,
2740+
namespace);
2741+
2742+
self.check_that_import_is_importable(
2743+
&**name_bindings,
2744+
directive.span,
2745+
target,
2746+
namespace);
2747+
2748+
let target = Some(Target::new(target_module.clone(),
2749+
name_bindings.clone(),
2750+
directive.shadowable));
2751+
import_resolution.set_target_and_id(namespace, target, directive.id);
2752+
import_resolution.is_public = directive.is_public;
2753+
*used_public = name_bindings.defined_in_public_namespace(namespace);
2754+
}
2755+
UnboundResult => { /* Continue. */ }
2756+
UnknownResult => {
2757+
panic!("{} result should be known at this point", namespace_name);
2758+
}
2759+
}
2760+
};
2761+
check_and_write_import(ValueNS, &value_result, &mut value_used_public);
2762+
check_and_write_import(TypeNS, &type_result, &mut type_used_public);
27662763
}
27672764

27682765
self.check_for_conflicts_between_imports_and_items(
@@ -2818,7 +2815,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
28182815

28192816
// Resolves a glob import. Note that this function cannot fail; it either
28202817
// succeeds or bails out (as importing * from an empty module or a module
2821-
// that exports nothing is valid).
2818+
// that exports nothing is valid). containing_module is the module we are
2819+
// actually importing, i.e., `foo` in `use foo::*`.
28222820
fn resolve_glob_import(&mut self,
28232821
module_: &Module,
28242822
containing_module: Rc<Module>,
@@ -2844,12 +2842,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
28442842
assert_eq!(containing_module.glob_count.get(), 0);
28452843

28462844
// Add all resolved imports from the containing module.
2847-
let import_resolutions = containing_module.import_resolutions
2848-
.borrow();
2845+
let import_resolutions = containing_module.import_resolutions.borrow();
28492846
for (ident, target_import_resolution) in import_resolutions.iter() {
28502847
debug!("(resolving glob import) writing module resolution \
28512848
{} into `{}`",
2852-
target_import_resolution.type_target.is_none(),
2849+
token::get_name(*ident),
28532850
self.module_to_string(module_));
28542851

28552852
if !target_import_resolution.is_public {
@@ -2869,17 +2866,15 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
28692866
// Continue.
28702867
}
28712868
Some(ref value_target) => {
2872-
dest_import_resolution.value_target =
2873-
Some(value_target.clone());
2869+
dest_import_resolution.value_target = Some(value_target.clone());
28742870
}
28752871
}
28762872
match target_import_resolution.type_target {
28772873
None => {
28782874
// Continue.
28792875
}
28802876
Some(ref type_target) => {
2881-
dest_import_resolution.type_target =
2882-
Some(type_target.clone());
2877+
dest_import_resolution.type_target = Some(type_target.clone());
28832878
}
28842879
}
28852880
dest_import_resolution.is_public = is_public;
@@ -2901,8 +2896,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
29012896
// Add all children from the containing module.
29022897
self.populate_module_if_necessary(&containing_module);
29032898

2904-
for (&name, name_bindings) in containing_module.children
2905-
.borrow().iter() {
2899+
for (&name, name_bindings) in containing_module.children.borrow().iter() {
29062900
self.merge_import_resolution(module_,
29072901
containing_module.clone(),
29082902
import_directive,
@@ -2912,8 +2906,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
29122906
}
29132907

29142908
// Add external module children from the containing module.
2915-
for (&name, module) in containing_module.external_module_children
2916-
.borrow().iter() {
2909+
for (&name, module) in containing_module.external_module_children.borrow().iter() {
29172910
let name_bindings =
29182911
Rc::new(Resolver::create_name_bindings_from_module(module.clone()));
29192912
self.merge_import_resolution(module_,
@@ -2958,41 +2951,39 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
29582951

29592952
debug!("(resolving glob import) writing resolution `{}` in `{}` \
29602953
to `{}`",
2961-
token::get_name(name).get().to_string(),
2954+
token::get_name(name).get(),
29622955
self.module_to_string(&*containing_module),
29632956
self.module_to_string(module_));
29642957

29652958
// Merge the child item into the import resolution.
2966-
if name_bindings.defined_in_namespace_with(ValueNS, IMPORTABLE | PUBLIC) {
2967-
debug!("(resolving glob import) ... for value target");
2968-
if dest_import_resolution.shadowable(ValueNS) == Shadowable::Never {
2969-
let msg = format!("a value named `{}` has already been imported \
2970-
in this module",
2971-
token::get_name(name).get());
2972-
self.session.span_err(import_directive.span, msg.as_slice());
2973-
} else {
2974-
dest_import_resolution.value_target =
2975-
Some(Target::new(containing_module.clone(),
2976-
name_bindings.clone(),
2977-
import_directive.shadowable));
2978-
dest_import_resolution.value_id = id;
2979-
}
2980-
}
2981-
if name_bindings.defined_in_namespace_with(TypeNS, IMPORTABLE | PUBLIC) {
2982-
debug!("(resolving glob import) ... for type target");
2983-
if dest_import_resolution.shadowable(TypeNS) == Shadowable::Never {
2984-
let msg = format!("a type named `{}` has already been imported \
2985-
in this module",
2986-
token::get_name(name).get());
2987-
self.session.span_err(import_directive.span, msg.as_slice());
2988-
} else {
2989-
dest_import_resolution.type_target =
2990-
Some(Target::new(containing_module,
2991-
name_bindings.clone(),
2992-
import_directive.shadowable));
2993-
dest_import_resolution.type_id = id;
2994-
}
2959+
{
2960+
let merge_child_item = |namespace| {
2961+
if name_bindings.defined_in_namespace_with(namespace, IMPORTABLE | PUBLIC) {
2962+
let namespace_name = match namespace {
2963+
TypeNS => "type",
2964+
ValueNS => "value",
2965+
};
2966+
debug!("(resolving glob import) ... for {} target", namespace_name);
2967+
if dest_import_resolution.shadowable(namespace) == Shadowable::Never {
2968+
let msg = format!("a {} named `{}` has already been imported \
2969+
in this module",
2970+
namespace_name,
2971+
token::get_name(name).get());
2972+
self.session.span_err(import_directive.span, msg.as_slice());
2973+
} else {
2974+
let target = Target::new(containing_module.clone(),
2975+
name_bindings.clone(),
2976+
import_directive.shadowable);
2977+
dest_import_resolution.set_target_and_id(namespace,
2978+
Some(target),
2979+
id);
2980+
}
2981+
}
2982+
};
2983+
merge_child_item(ValueNS);
2984+
merge_child_item(TypeNS);
29952985
}
2986+
29962987
dest_import_resolution.is_public = is_public;
29972988

29982989
self.check_for_conflicts_between_imports_and_items(
@@ -3012,6 +3003,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
30123003
return
30133004
}
30143005

3006+
debug!("check_for_conflicting_import: {}; target exists: {}",
3007+
token::get_name(name).get(),
3008+
target.is_some());
3009+
30153010
match *target {
30163011
Some(ref target) if target.shadowable != Shadowable::Always => {
30173012
let msg = format!("a {} named `{}` has already been imported \

0 commit comments

Comments
 (0)