Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolve: Mark more erroneous imports as used #64265

Merged
merged 1 commit into from
Sep 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`.