Skip to content

Commit

Permalink
Write and use increment_outstanding_references_for and decrement_outs…
Browse files Browse the repository at this point in the history
…tanding_references_for
  • Loading branch information
jseyfried committed Feb 8, 2016
1 parent 96b4dc4 commit 16e7ff1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
12 changes: 2 additions & 10 deletions src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -16,7 +16,6 @@
use DefModifiers;
use resolve_imports::ImportDirective;
use resolve_imports::ImportDirectiveSubclass::{self, SingleImport, GlobImport};
use resolve_imports::NameResolution;
use Module;
use Namespace::{self, TypeNS, ValueNS};
use {NameBinding, NameBindingKind};
Expand Down Expand Up @@ -699,15 +698,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
debug!("(building import directive) building import directive: {}::{}",
names_to_string(&module_.imports.borrow().last().unwrap().module_path),
target);

let mut import_resolutions = module_.import_resolutions.borrow_mut();
for &ns in [TypeNS, ValueNS].iter() {
let mut resolution = import_resolutions.entry((target, ns)).or_insert(
NameResolution::default()
);

resolution.outstanding_references += 1;
}
module_.increment_outstanding_references_for(target, ValueNS);
module_.increment_outstanding_references_for(target, TypeNS);
}
GlobImport => {
// Set the glob flag. This tells us that we don't know the
Expand Down
20 changes: 20 additions & 0 deletions src/librustc_resolve/lib.rs
Expand Up @@ -670,6 +670,13 @@ impl<T> ResolveResult<T> {
Success(t) => f(t),
}
}

fn success(self) -> Option<T> {
match self {
Success(t) => Some(t),
_ => None,
}
}
}

enum FallbackSuggestion {
Expand Down Expand Up @@ -870,6 +877,19 @@ impl<'a> ModuleS<'a> {
}
}

fn increment_outstanding_references_for(&self, name: Name, ns: Namespace) {
let mut resolutions = self.import_resolutions.borrow_mut();
resolutions.entry((name, ns)).or_insert_with(Default::default).outstanding_references += 1;
}

fn decrement_outstanding_references_for(&self, name: Name, ns: Namespace) {
match self.import_resolutions.borrow_mut().get_mut(&(name, ns)).unwrap()
.outstanding_references {
0 => panic!("No more outstanding references!"),
ref mut outstanding_references => { *outstanding_references -= 1; }
}
}

fn for_each_local_child<F: FnMut(Name, Namespace, &'a NameBinding<'a>)>(&self, mut f: F) {
for (&(name, ns), name_binding) in self.children.borrow().iter() {
if !name_binding.is_extern_crate() {
Expand Down
14 changes: 5 additions & 9 deletions src/librustc_resolve/resolve_imports.rs
Expand Up @@ -511,9 +511,9 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
}

// We've successfully resolved the import. Write the results in.
let mut import_resolutions = module_.import_resolutions.borrow_mut();

{
let mut import_resolutions = module_.import_resolutions.borrow_mut();
let mut check_and_write_import = |namespace, result| {
let result: &ResolveResult<&NameBinding> = result;

Expand Down Expand Up @@ -567,26 +567,22 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
}

let value_def_and_priv = {
let import_resolution_value = import_resolutions.get_mut(&(target, ValueNS)).unwrap();
assert!(import_resolution_value.outstanding_references >= 1);
import_resolution_value.outstanding_references -= 1;
module_.decrement_outstanding_references_for(target, ValueNS);

// Record what this import resolves to for later uses in documentation,
// this may resolve to either a value or a type, but for documentation
// purposes it's good enough to just favor one over the other.
import_resolution_value.binding.as_ref().map(|binding| {
value_result.success().map(|binding| {
let def = binding.def().unwrap();
let last_private = if binding.is_public() { lp } else { DependsOn(def.def_id()) };
(def, last_private)
})
};

let type_def_and_priv = {
let import_resolution_type = import_resolutions.get_mut(&(target, TypeNS)).unwrap();
assert!(import_resolution_type.outstanding_references >= 1);
import_resolution_type.outstanding_references -= 1;
module_.decrement_outstanding_references_for(target, TypeNS);

import_resolution_type.binding.as_ref().map(|binding| {
type_result.success().map(|binding| {
let def = binding.def().unwrap();
let last_private = if binding.is_public() { lp } else { DependsOn(def.def_id()) };
(def, last_private)
Expand Down

0 comments on commit 16e7ff1

Please sign in to comment.