Skip to content

Commit

Permalink
Rollup merge of rust-lang#121958 - chenyukang:yukang-fix-121915-impor…
Browse files Browse the repository at this point in the history
…t, r=pnkfelix

Fix redundant import errors for preload extern crate

Fixes rust-lang#121915
  • Loading branch information
GuillaumeGomez authored Mar 4, 2024
2 parents 9896348 + 25e56de commit 85f3738
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 3 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_lint/src/context/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiagnostics, diag:
diag.span_label(span, format!("the item `{ident}` is already {introduced} here"));
}
}
BuiltinLintDiagnostics::RedundantImportRemove(span) => {
diag.span_suggestion(span, "remove this import", "", Applicability::MachineApplicable);
}
BuiltinLintDiagnostics::DeprecatedMacro(suggestion, span) => {
stability::deprecation_suggestion(diag, "macro", suggestion, span)
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ pub enum BuiltinLintDiagnostics {
UnknownCrateTypes(Span, String, String),
UnusedImports(String, Vec<(Span, String)>, Option<Span>),
RedundantImport(Vec<(Span, bool)>, Ident),
RedundantImportRemove(Span),
DeprecatedMacro(Option<Symbol>, Span),
MissingAbi(Span, Abi),
UnusedDocComment(Span),
Expand Down
17 changes: 14 additions & 3 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1336,7 +1336,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
let mut is_redundant = true;

let mut redundant_span = PerNS { value_ns: None, type_ns: None, macro_ns: None };

self.per_ns(|this, ns| {
if is_redundant && let Ok(binding) = source_bindings[ns].get() {
if binding.res() == Res::Err {
Expand Down Expand Up @@ -1368,12 +1367,24 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
let mut redundant_spans: Vec<_> = redundant_span.present_items().collect();
redundant_spans.sort();
redundant_spans.dedup();
let is_preload = self.extern_prelude_get(target, false).is_some();
let (msg, diag) = if is_preload {
(
"already exists in the extern prelude",
BuiltinLintDiagnostics::RedundantImportRemove(import.use_span),
)
} else {
(
"imported redundantly",
BuiltinLintDiagnostics::RedundantImport(redundant_spans, source),
)
};
self.lint_buffer.buffer_lint_with_diagnostic(
UNUSED_IMPORTS,
id,
import.span,
format!("the item `{source}` is imported redundantly"),
BuiltinLintDiagnostics::RedundantImport(redundant_spans, source),
format!("the item `{source}` is {msg}"),
diag,
);
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/ui/imports/auxiliary/aux-issue-121915.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub fn item() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: the item `aux_issue_121915` is already exists in the extern prelude
--> $DIR/redundant-import-issue-121915-2015.rs:10:9
|
LL | use aux_issue_121915;
| ^^^^^^^^^^^^^^^^ help: remove this import
|
note: the lint level is defined here
--> $DIR/redundant-import-issue-121915-2015.rs:8:8
|
LL | #[deny(unused_imports)]
| ^^^^^^^^^^^^^^

error: aborting due to 1 previous error

11 changes: 11 additions & 0 deletions tests/ui/imports/redundant-import-issue-121915-2015.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//@ compile-flags: --extern aux_issue_121915 --edition 2015
//@ aux-build: aux-issue-121915.rs

extern crate aux_issue_121915;

#[deny(unused_imports)]
fn main() {
use aux_issue_121915;
//~^ ERROR the item `aux_issue_121915` is already exists in the extern prelude
aux_issue_121915::item();
}
14 changes: 14 additions & 0 deletions tests/ui/imports/redundant-import-issue-121915-2015.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: the item `aux_issue_121915` is already exists in the extern prelude
--> $DIR/redundant-import-issue-121915-2015.rs:8:9
|
LL | use aux_issue_121915;
| ----^^^^^^^^^^^^^^^^- help: remove this import
|
note: the lint level is defined here
--> $DIR/redundant-import-issue-121915-2015.rs:6:8
|
LL | #[deny(unused_imports)]
| ^^^^^^^^^^^^^^

error: aborting due to 1 previous error

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0432]: unresolved import `aux_issue_121915`
--> $DIR/redundant-import-issue-121915.rs:10:9
|
LL | use aux_issue_121915;
| ^^^^^^^^^^^^^^^^ no `aux_issue_121915` in the root

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0432`.
14 changes: 14 additions & 0 deletions tests/ui/imports/redundant-import-issue-121915.edition2018.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: the item `aux_issue_121915` is already exists in the extern prelude
--> $DIR/redundant-import-issue-121915.rs:9:9
|
LL | use aux_issue_121915;
| ----^^^^^^^^^^^^^^^^- help: remove this import
|
note: the lint level is defined here
--> $DIR/redundant-import-issue-121915.rs:7:8
|
LL | #[deny(unused_imports)]
| ^^^^^^^^^^^^^^

error: aborting due to 1 previous error

14 changes: 14 additions & 0 deletions tests/ui/imports/redundant-import-issue-121915.edition2021.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: the item `aux_issue_121915` is already exists in the extern prelude
--> $DIR/redundant-import-issue-121915.rs:9:9
|
LL | use aux_issue_121915;
| ----^^^^^^^^^^^^^^^^- help: remove this import
|
note: the lint level is defined here
--> $DIR/redundant-import-issue-121915.rs:7:8
|
LL | #[deny(unused_imports)]
| ^^^^^^^^^^^^^^

error: aborting due to 1 previous error

12 changes: 12 additions & 0 deletions tests/ui/imports/redundant-import-issue-121915.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@ revisions: edition2018 edition2021
//@ [edition2018] edition:2018
//@ [edition2021] edition:2021
//@ compile-flags: --extern aux_issue_121915
//@ aux-build: aux-issue-121915.rs

#[deny(unused_imports)]
fn main() {
use aux_issue_121915;
//~^ ERROR the item `aux_issue_121915` is already exists in the extern prelude
aux_issue_121915::item();
}

0 comments on commit 85f3738

Please sign in to comment.