Skip to content

Commit 85a1209

Browse files
committed
Improve import failure detection
1 parent bfb832e commit 85a1209

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

src/librustc_resolve/resolve_imports.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl<'a> NameResolution<'a> {
168168

169169
// Returns Some(the resolution of the name), or None if the resolution depends
170170
// on whether more globs can define the name.
171-
fn try_result(&self, allow_private_imports: bool)
171+
fn try_result(&self, ns: Namespace, allow_private_imports: bool)
172172
-> Option<ResolveResult<&'a NameBinding<'a>>> {
173173
match self.binding {
174174
Some(binding) if !binding.defined_with(DefModifiers::GLOB_IMPORTED) =>
@@ -189,7 +189,18 @@ impl<'a> NameResolution<'a> {
189189
return None;
190190
}
191191

192-
return Indeterminate;
192+
let target_module = match directive.target_module.get() {
193+
Some(target_module) => target_module,
194+
None => return Some(Indeterminate),
195+
};
196+
let name = match directive.subclass {
197+
SingleImport { source, target, .. } if source == target => target,
198+
_ => return Some(Indeterminate),
199+
};
200+
match target_module.resolve_name(name, ns, false) {
201+
Failed(_) => {}
202+
_ => return Some(Indeterminate),
203+
}
193204
}
194205
}
195206

@@ -224,7 +235,7 @@ impl<'a> ::ModuleS<'a> {
224235
};
225236

226237
let resolution = resolutions.get(&(name, ns)).cloned().unwrap_or_default();
227-
if let Some(result) = resolution.try_result(allow_private_imports) {
238+
if let Some(result) = resolution.try_result(ns, allow_private_imports) {
228239
// If the resolution doesn't depend on glob definability, check privacy and return.
229240
return result.and_then(|binding| {
230241
let allowed = allow_private_imports || !binding.is_import() || binding.is_public();
@@ -483,27 +494,12 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
483494
};
484495

485496
// We need to resolve both namespaces for this to succeed.
486-
let module_ = self.resolver.current_module;
487-
let (value_result, type_result) = {
488-
let mut resolve_in_ns = |ns, determined: bool| {
489-
// Temporarily count the directive as determined so that the resolution fails
490-
// (as opposed to being indeterminate) when it can only be defined by the directive.
491-
if !determined {
492-
module_.resolutions.borrow_mut().get_mut(&(target, ns)).unwrap()
493-
.single_imports.directive_failed();
494-
}
495-
let result =
496-
self.resolver.resolve_name_in_module(target_module, source, ns, false, true);
497-
if !determined {
498-
module_.resolutions.borrow_mut().get_mut(&(target, ns)).unwrap()
499-
.single_imports.add_directive(directive);
500-
}
501-
result
502-
};
503-
(resolve_in_ns(ValueNS, value_determined.get()),
504-
resolve_in_ns(TypeNS, type_determined.get()))
505-
};
497+
let value_result =
498+
self.resolver.resolve_name_in_module(target_module, source, ValueNS, false, true);
499+
let type_result =
500+
self.resolver.resolve_name_in_module(target_module, source, TypeNS, false, true);
506501

502+
let module_ = self.resolver.current_module;
507503
for &(ns, result, determined) in &[(ValueNS, &value_result, value_determined),
508504
(TypeNS, &type_result, type_determined)] {
509505
if determined.get() { continue }

0 commit comments

Comments
 (0)