Skip to content

Commit c9c8509

Browse files
committed
Rollup merge of #32849 - jseyfried:import_resolution_diagnostics, r=eddyb
resolve: import resolution diagnostics This improves the diagnostics for failing import resolutions (fixes #32833). r? @eddyb
2 parents 9989a95 + 44ddaa2 commit c9c8509

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

src/librustc_resolve/resolve_imports.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,7 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
539539
(&Failed(_), &Failed(_)) => {
540540
let resolutions = target_module.resolutions.borrow();
541541
let names = resolutions.iter().filter_map(|(&(ref name, _), resolution)| {
542+
if *name == source { return None; } // Never suggest the same name
542543
match *resolution.borrow() {
543544
NameResolution { binding: Some(_), .. } => Some(name),
544545
NameResolution { single_imports: SingleImports::None, .. } => None,
@@ -549,9 +550,12 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
549550
Some(name) => format!(". Did you mean to use `{}`?", name),
550551
None => "".to_owned(),
551552
};
552-
let msg = format!("There is no `{}` in `{}`{}",
553-
source,
554-
module_to_string(target_module), lev_suggestion);
553+
let module_str = module_to_string(target_module);
554+
let msg = if &module_str == "???" {
555+
format!("There is no `{}` in the crate root{}", source, lev_suggestion)
556+
} else {
557+
format!("There is no `{}` in `{}`{}", source, module_str, lev_suggestion)
558+
};
555559
return Failed(Some((directive.span, msg)));
556560
}
557561
_ => (),

src/test/compile-fail/issue-32833.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use bar::Foo; //~ ERROR There is no `Foo` in `bar` [E0432]
12+
mod bar {
13+
use Foo; //~ ERROR There is no `Foo` in the crate root [E0432]
14+
}
15+
16+
fn main() {}

src/test/compile-fail/use-mod-2.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
mod foo {
1212
use self::{self};
13-
//~^ ERROR unresolved import `self`. There is no `self` in `???`
13+
//~^ ERROR unresolved import `self`. There is no `self` in the crate root
1414

1515
use super::{self};
16-
//~^ ERROR unresolved import `super`. There is no `super` in `???`
16+
//~^ ERROR unresolved import `super`. There is no `super` in the crate root
1717
}
1818

1919
fn main() {}

0 commit comments

Comments
 (0)