Skip to content

Commit 16e7ff1

Browse files
committed
Write and use increment_outstanding_references_for and decrement_outstanding_references_for
1 parent 96b4dc4 commit 16e7ff1

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

src/librustc_resolve/build_reduced_graph.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use DefModifiers;
1717
use resolve_imports::ImportDirective;
1818
use resolve_imports::ImportDirectiveSubclass::{self, SingleImport, GlobImport};
19-
use resolve_imports::NameResolution;
2019
use Module;
2120
use Namespace::{self, TypeNS, ValueNS};
2221
use {NameBinding, NameBindingKind};
@@ -699,15 +698,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
699698
debug!("(building import directive) building import directive: {}::{}",
700699
names_to_string(&module_.imports.borrow().last().unwrap().module_path),
701700
target);
702-
703-
let mut import_resolutions = module_.import_resolutions.borrow_mut();
704-
for &ns in [TypeNS, ValueNS].iter() {
705-
let mut resolution = import_resolutions.entry((target, ns)).or_insert(
706-
NameResolution::default()
707-
);
708-
709-
resolution.outstanding_references += 1;
710-
}
701+
module_.increment_outstanding_references_for(target, ValueNS);
702+
module_.increment_outstanding_references_for(target, TypeNS);
711703
}
712704
GlobImport => {
713705
// Set the glob flag. This tells us that we don't know the

src/librustc_resolve/lib.rs

+20
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,13 @@ impl<T> ResolveResult<T> {
670670
Success(t) => f(t),
671671
}
672672
}
673+
674+
fn success(self) -> Option<T> {
675+
match self {
676+
Success(t) => Some(t),
677+
_ => None,
678+
}
679+
}
673680
}
674681

675682
enum FallbackSuggestion {
@@ -870,6 +877,19 @@ impl<'a> ModuleS<'a> {
870877
}
871878
}
872879

880+
fn increment_outstanding_references_for(&self, name: Name, ns: Namespace) {
881+
let mut resolutions = self.import_resolutions.borrow_mut();
882+
resolutions.entry((name, ns)).or_insert_with(Default::default).outstanding_references += 1;
883+
}
884+
885+
fn decrement_outstanding_references_for(&self, name: Name, ns: Namespace) {
886+
match self.import_resolutions.borrow_mut().get_mut(&(name, ns)).unwrap()
887+
.outstanding_references {
888+
0 => panic!("No more outstanding references!"),
889+
ref mut outstanding_references => { *outstanding_references -= 1; }
890+
}
891+
}
892+
873893
fn for_each_local_child<F: FnMut(Name, Namespace, &'a NameBinding<'a>)>(&self, mut f: F) {
874894
for (&(name, ns), name_binding) in self.children.borrow().iter() {
875895
if !name_binding.is_extern_crate() {

src/librustc_resolve/resolve_imports.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,9 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
511511
}
512512

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

516515
{
516+
let mut import_resolutions = module_.import_resolutions.borrow_mut();
517517
let mut check_and_write_import = |namespace, result| {
518518
let result: &ResolveResult<&NameBinding> = result;
519519

@@ -567,26 +567,22 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
567567
}
568568

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

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

584582
let type_def_and_priv = {
585-
let import_resolution_type = import_resolutions.get_mut(&(target, TypeNS)).unwrap();
586-
assert!(import_resolution_type.outstanding_references >= 1);
587-
import_resolution_type.outstanding_references -= 1;
583+
module_.decrement_outstanding_references_for(target, TypeNS);
588584

589-
import_resolution_type.binding.as_ref().map(|binding| {
585+
type_result.success().map(|binding| {
590586
let def = binding.def().unwrap();
591587
let last_private = if binding.is_public() { lp } else { DependsOn(def.def_id()) };
592588
(def, last_private)

0 commit comments

Comments
 (0)