Skip to content

Commit

Permalink
Rollup merge of rust-lang#64265 - petrochenkov:useerr, r=estebank
Browse files Browse the repository at this point in the history
resolve: Mark more erroneous imports as used

Fixes rust-lang#63724
r? @estebank
  • Loading branch information
Centril authored Sep 8, 2019
2 parents 2417208 + 7dc3839 commit 3a32803
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/librustc_resolve/resolve_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,10 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
self.throw_unresolved_import_error(errors.clone(), None);
}

for import in &self.r.indeterminate_imports {
// Consider erroneous imports used to avoid duplicate diagnostics.
self.r.used_imports.insert((import.id, TypeNS));
}
// Report unresolved imports only if no hard error was already reported
// to avoid generating multiple errors on the same import.
if !has_errors {
Expand Down Expand Up @@ -839,6 +843,10 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
true, directive.span, directive.crate_lint());
let no_ambiguity = self.r.ambiguity_errors.len() == prev_ambiguity_errors_len;
directive.vis.set(orig_vis);
if let PathResult::Failed { .. } | PathResult::NonModule(..) = path_res {
// Consider erroneous imports used to avoid duplicate diagnostics.
self.r.used_imports.insert((directive.id, TypeNS));
}
let module = match path_res {
PathResult::Module(module) => {
// Consistency checks, analogous to `finalize_macro_resolutions`.
Expand Down
14 changes: 10 additions & 4 deletions src/test/ui/imports/unresolved-imports-used.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
// There should be *no* unused import errors.
// There should be *one* unused import error.
#![deny(unused_imports)]

mod qux {
fn quz() {}
pub fn quy() {}
}

use qux::quz; //~ ERROR function `quz` is private
use qux::bar; //~ ERROR unresolved import `qux::bar`
use foo::bar; //~ ERROR unresolved import `foo`
use qux::quz; //~ ERROR function `quz` is private
use qux::bar; //~ ERROR unresolved import `qux::bar`
use foo::bar;
use baz::*;
use qux::bar2; //~ ERROR unresolved import `qux::bar2`
use foo2::bar2;
use baz2::*;
use qux::quy; //~ ERROR unused import

fn main() {}
26 changes: 19 additions & 7 deletions src/test/ui/imports/unresolved-imports-used.stderr
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
error[E0432]: unresolved import `qux::bar`
--> $DIR/unresolved-imports-used.rs:9:5
--> $DIR/unresolved-imports-used.rs:10:5
|
LL | use qux::bar;
| ^^^^^^^^ no `bar` in `qux`

error[E0432]: unresolved import `foo`
--> $DIR/unresolved-imports-used.rs:10:5
error[E0432]: unresolved import `qux::bar2`
--> $DIR/unresolved-imports-used.rs:13:5
|
LL | use foo::bar;
| ^^^ maybe a missing crate `foo`?
LL | use qux::bar2;
| ^^^^^^^^^ no `bar2` in `qux`

error[E0603]: function `quz` is private
--> $DIR/unresolved-imports-used.rs:8:10
--> $DIR/unresolved-imports-used.rs:9:10
|
LL | use qux::quz;
| ^^^

error: aborting due to 3 previous errors
error: unused import: `qux::quy`
--> $DIR/unresolved-imports-used.rs:16:5
|
LL | use qux::quy;
| ^^^^^^^^
|
note: lint level defined here
--> $DIR/unresolved-imports-used.rs:2:9
|
LL | #![deny(unused_imports)]
| ^^^^^^^^^^^^^^

error: aborting due to 4 previous errors

Some errors have detailed explanations: E0432, E0603.
For more information about an error, try `rustc --explain E0432`.

0 comments on commit 3a32803

Please sign in to comment.