Skip to content

Commit 61f1f4d

Browse files
committed
Add a field pub_outstanding_references to NameResolution.
Add an argument `allow_private_imports` to some methods.
1 parent 45f0ce7 commit 61f1f4d

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,8 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
681681

682682
match subclass {
683683
SingleImport { target, .. } => {
684-
module_.increment_outstanding_references_for(target, ValueNS);
685-
module_.increment_outstanding_references_for(target, TypeNS);
684+
module_.increment_outstanding_references_for(target, ValueNS, is_public);
685+
module_.increment_outstanding_references_for(target, TypeNS, is_public);
686686
}
687687
GlobImport => {
688688
// Set the glob flag. This tells us that we don't know the

src/librustc_resolve/resolve_imports.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ impl ImportDirective {
125125
/// Records information about the resolution of a name in a module.
126126
pub struct NameResolution<'a> {
127127
/// The number of unresolved single imports that could define the name.
128-
outstanding_references: usize,
128+
outstanding_references: u32,
129+
pub_outstanding_references: u32,
129130
/// The least shadowable known binding for this name, or None if there are no known bindings.
130131
pub binding: Option<&'a NameBinding<'a>>,
131132
duplicate_globs: Vec<&'a NameBinding<'a>>,
@@ -151,7 +152,7 @@ impl<'a> NameResolution<'a> {
151152
}
152153

153154
// Returns the resolution of the name assuming no more globs will define it.
154-
fn result(&self) -> ResolveResult<&'a NameBinding<'a>> {
155+
fn result(&self, allow_private_imports: bool) -> ResolveResult<&'a NameBinding<'a>> {
155156
match self.binding {
156157
Some(binding) if !binding.defined_with(DefModifiers::GLOB_IMPORTED) => Success(binding),
157158
_ if self.outstanding_references > 0 => Indeterminate,
@@ -162,8 +163,9 @@ impl<'a> NameResolution<'a> {
162163

163164
// Returns Some(the resolution of the name), or None if the resolution depends
164165
// on whether more globs can define the name.
165-
fn try_result(&self) -> Option<ResolveResult<&'a NameBinding<'a>>> {
166-
match self.result() {
166+
fn try_result(&self, allow_private_imports: bool)
167+
-> Option<ResolveResult<&'a NameBinding<'a>>> {
168+
match self.result(allow_private_imports) {
167169
Success(binding) if binding.defined_with(DefModifiers::PRELUDE) => None,
168170
Failed(_) => None,
169171
result @ _ => Some(result),
@@ -200,7 +202,7 @@ impl<'a> ::ModuleS<'a> {
200202
};
201203

202204
let resolution = resolutions.get(&(name, ns)).cloned().unwrap_or_default();
203-
if let Some(result) = resolution.try_result() {
205+
if let Some(result) = resolution.try_result(allow_private_imports) {
204206
// If the resolution doesn't depend on glob definability, check privacy and return.
205207
return result.and_then(|binding| {
206208
let allowed = allow_private_imports || !binding.is_import() || binding.is_public();
@@ -234,7 +236,7 @@ impl<'a> ::ModuleS<'a> {
234236
}
235237
}
236238

237-
resolution.result()
239+
resolution.result(true)
238240
}
239241

240242
// Define the name or return the existing binding if there is a collision.
@@ -246,15 +248,26 @@ impl<'a> ::ModuleS<'a> {
246248
})
247249
}
248250

249-
pub fn increment_outstanding_references_for(&self, name: Name, ns: Namespace) {
251+
pub fn increment_outstanding_references_for(&self, name: Name, ns: Namespace, is_public: bool) {
250252
let mut resolutions = self.resolutions.borrow_mut();
251-
resolutions.entry((name, ns)).or_insert_with(Default::default).outstanding_references += 1;
253+
let resolution = resolutions.entry((name, ns)).or_insert_with(Default::default);
254+
resolution.outstanding_references += 1;
255+
if is_public {
256+
resolution.pub_outstanding_references += 1;
257+
}
252258
}
253259

254-
fn decrement_outstanding_references_for(&self, name: Name, ns: Namespace) {
255-
self.update_resolution(name, ns, |resolution| match resolution.outstanding_references {
256-
0 => panic!("No more outstanding references!"),
257-
ref mut outstanding_references => *outstanding_references -= 1,
260+
fn decrement_outstanding_references_for(&self, name: Name, ns: Namespace, is_public: bool) {
261+
let decrement_references = |count: &mut _| {
262+
assert!(*count > 0);
263+
*count -= 1;
264+
};
265+
266+
self.update_resolution(name, ns, |resolution| {
267+
decrement_references(&mut resolution.outstanding_references);
268+
if is_public {
269+
decrement_references(&mut resolution.pub_outstanding_references);
270+
}
258271
})
259272
}
260273

@@ -265,11 +278,11 @@ impl<'a> ::ModuleS<'a> {
265278
{
266279
let mut resolutions = self.resolutions.borrow_mut();
267280
let resolution = resolutions.entry((name, ns)).or_insert_with(Default::default);
268-
let was_success = resolution.try_result().and_then(ResolveResult::success).is_some();
281+
let was_success = resolution.try_result(false).and_then(ResolveResult::success).is_some();
269282

270283
let t = update(resolution);
271284
if !was_success {
272-
if let Some(Success(binding)) = resolution.try_result() {
285+
if let Some(Success(binding)) = resolution.try_result(false) {
273286
self.define_in_glob_importers(name, ns, binding);
274287
}
275288
}
@@ -460,10 +473,14 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
460473
let mut resolve_in_ns = |ns, determined: bool| {
461474
// Temporarily count the directive as determined so that the resolution fails
462475
// (as opposed to being indeterminate) when it can only be defined by the directive.
463-
if !determined { module_.decrement_outstanding_references_for(target, ns) }
476+
if !determined {
477+
module_.decrement_outstanding_references_for(target, ns, directive.is_public)
478+
}
464479
let result =
465480
self.resolver.resolve_name_in_module(target_module, source, ns, false, true);
466-
if !determined { module_.increment_outstanding_references_for(target, ns) }
481+
if !determined {
482+
module_.increment_outstanding_references_for(target, ns, directive.is_public)
483+
}
467484
result
468485
};
469486
(resolve_in_ns(ValueNS, value_determined.get()),
@@ -494,7 +511,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
494511
self.report_conflict(target, ns, &directive.import(binding, None), old_binding);
495512
}
496513
}
497-
module_.decrement_outstanding_references_for(target, ns);
514+
module_.decrement_outstanding_references_for(target, ns, directive.is_public);
498515
}
499516

500517
match (&value_result, &type_result) {
@@ -601,7 +618,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
601618
}
602619

603620
for (&(name, ns), resolution) in target_module.resolutions.borrow().iter() {
604-
if let Some(Success(binding)) = resolution.try_result() {
621+
if let Some(Success(binding)) = resolution.try_result(false) {
605622
if binding.defined_with(DefModifiers::IMPORTABLE | DefModifiers::PUBLIC) {
606623
let _ = module_.try_define_child(name, ns, directive.import(binding, None));
607624
}

0 commit comments

Comments
 (0)