1
Fork 0

Write and use increment_outstanding_references_for and decrement_outstanding_references_for

This commit is contained in:
Jeffrey Seyfried 2016-02-07 22:40:23 +00:00
parent 96b4dc4b87
commit 16e7ff1bff
3 changed files with 27 additions and 19 deletions

View file

@ -16,7 +16,6 @@
use DefModifiers; use DefModifiers;
use resolve_imports::ImportDirective; use resolve_imports::ImportDirective;
use resolve_imports::ImportDirectiveSubclass::{self, SingleImport, GlobImport}; use resolve_imports::ImportDirectiveSubclass::{self, SingleImport, GlobImport};
use resolve_imports::NameResolution;
use Module; use Module;
use Namespace::{self, TypeNS, ValueNS}; use Namespace::{self, TypeNS, ValueNS};
use {NameBinding, NameBindingKind}; use {NameBinding, NameBindingKind};
@ -699,15 +698,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
debug!("(building import directive) building import directive: {}::{}", debug!("(building import directive) building import directive: {}::{}",
names_to_string(&module_.imports.borrow().last().unwrap().module_path), names_to_string(&module_.imports.borrow().last().unwrap().module_path),
target); target);
module_.increment_outstanding_references_for(target, ValueNS);
let mut import_resolutions = module_.import_resolutions.borrow_mut(); module_.increment_outstanding_references_for(target, TypeNS);
for &ns in [TypeNS, ValueNS].iter() {
let mut resolution = import_resolutions.entry((target, ns)).or_insert(
NameResolution::default()
);
resolution.outstanding_references += 1;
}
} }
GlobImport => { GlobImport => {
// Set the glob flag. This tells us that we don't know the // Set the glob flag. This tells us that we don't know the

View file

@ -670,6 +670,13 @@ impl<T> ResolveResult<T> {
Success(t) => f(t), Success(t) => f(t),
} }
} }
fn success(self) -> Option<T> {
match self {
Success(t) => Some(t),
_ => None,
}
}
} }
enum FallbackSuggestion { enum FallbackSuggestion {
@ -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) { 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() { for (&(name, ns), name_binding) in self.children.borrow().iter() {
if !name_binding.is_extern_crate() { if !name_binding.is_extern_crate() {

View file

@ -511,9 +511,9 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
} }
// We've successfully resolved the import. Write the results in. // 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 mut check_and_write_import = |namespace, result| {
let result: &ResolveResult<&NameBinding> = result; let result: &ResolveResult<&NameBinding> = result;
@ -567,14 +567,12 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
} }
let value_def_and_priv = { let value_def_and_priv = {
let import_resolution_value = import_resolutions.get_mut(&(target, ValueNS)).unwrap(); module_.decrement_outstanding_references_for(target, ValueNS);
assert!(import_resolution_value.outstanding_references >= 1);
import_resolution_value.outstanding_references -= 1;
// Record what this import resolves to for later uses in documentation, // Record what this import resolves to for later uses in documentation,
// this may resolve to either a value or a type, but for 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. // 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 def = binding.def().unwrap();
let last_private = if binding.is_public() { lp } else { DependsOn(def.def_id()) }; let last_private = if binding.is_public() { lp } else { DependsOn(def.def_id()) };
(def, last_private) (def, last_private)
@ -582,11 +580,9 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
}; };
let type_def_and_priv = { let type_def_and_priv = {
let import_resolution_type = import_resolutions.get_mut(&(target, TypeNS)).unwrap(); module_.decrement_outstanding_references_for(target, TypeNS);
assert!(import_resolution_type.outstanding_references >= 1);
import_resolution_type.outstanding_references -= 1;
import_resolution_type.binding.as_ref().map(|binding| { type_result.success().map(|binding| {
let def = binding.def().unwrap(); let def = binding.def().unwrap();
let last_private = if binding.is_public() { lp } else { DependsOn(def.def_id()) }; let last_private = if binding.is_public() { lp } else { DependsOn(def.def_id()) };
(def, last_private) (def, last_private)