From 6985c4b0f8b7b2b0b78f8fb033f2da2e17c81e4f Mon Sep 17 00:00:00 2001 From: yukang Date: Mon, 4 Mar 2024 10:24:23 +0800 Subject: [PATCH] Fix redundant import errors for preload extern crate --- compiler/rustc_lint/src/context/diagnostics.rs | 3 +++ compiler/rustc_lint_defs/src/lib.rs | 1 + compiler/rustc_resolve/src/imports.rs | 17 ++++++++++++++--- tests/ui/imports/auxiliary/aux-issue-121915.rs | 1 + ...-import-issue-121915-2015.edition2015.stderr | 14 ++++++++++++++ .../redundant-import-issue-121915-2015.rs | 11 +++++++++++ .../redundant-import-issue-121915-2015.stderr | 14 ++++++++++++++ ...ndant-import-issue-121915.edition2015.stderr | 9 +++++++++ ...ndant-import-issue-121915.edition2018.stderr | 14 ++++++++++++++ ...ndant-import-issue-121915.edition2021.stderr | 14 ++++++++++++++ .../ui/imports/redundant-import-issue-121915.rs | 12 ++++++++++++ 11 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 tests/ui/imports/auxiliary/aux-issue-121915.rs create mode 100644 tests/ui/imports/redundant-import-issue-121915-2015.edition2015.stderr create mode 100644 tests/ui/imports/redundant-import-issue-121915-2015.rs create mode 100644 tests/ui/imports/redundant-import-issue-121915-2015.stderr create mode 100644 tests/ui/imports/redundant-import-issue-121915.edition2015.stderr create mode 100644 tests/ui/imports/redundant-import-issue-121915.edition2018.stderr create mode 100644 tests/ui/imports/redundant-import-issue-121915.edition2021.stderr create mode 100644 tests/ui/imports/redundant-import-issue-121915.rs diff --git a/compiler/rustc_lint/src/context/diagnostics.rs b/compiler/rustc_lint/src/context/diagnostics.rs index 728996f07417c..2692344337973 100644 --- a/compiler/rustc_lint/src/context/diagnostics.rs +++ b/compiler/rustc_lint/src/context/diagnostics.rs @@ -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) } diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index 198008d4d0db6..06f312f6030aa 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -578,6 +578,7 @@ pub enum BuiltinLintDiagnostics { UnknownCrateTypes(Span, String, String), UnusedImports(String, Vec<(Span, String)>, Option), RedundantImport(Vec<(Span, bool)>, Ident), + RedundantImportRemove(Span), DeprecatedMacro(Option, Span), MissingAbi(Span, Abi), UnusedDocComment(Span), diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index 9bfca0f179880..49af5a57ccde7 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -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 { @@ -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); + let (msg, diag) = if is_preload.is_some() { + ( + "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, ); } } diff --git a/tests/ui/imports/auxiliary/aux-issue-121915.rs b/tests/ui/imports/auxiliary/aux-issue-121915.rs new file mode 100644 index 0000000000000..7f9f5bda79ffd --- /dev/null +++ b/tests/ui/imports/auxiliary/aux-issue-121915.rs @@ -0,0 +1 @@ +pub fn item() {} diff --git a/tests/ui/imports/redundant-import-issue-121915-2015.edition2015.stderr b/tests/ui/imports/redundant-import-issue-121915-2015.edition2015.stderr new file mode 100644 index 0000000000000..af3e4d54ec234 --- /dev/null +++ b/tests/ui/imports/redundant-import-issue-121915-2015.edition2015.stderr @@ -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 + diff --git a/tests/ui/imports/redundant-import-issue-121915-2015.rs b/tests/ui/imports/redundant-import-issue-121915-2015.rs new file mode 100644 index 0000000000000..79b5110e3eb01 --- /dev/null +++ b/tests/ui/imports/redundant-import-issue-121915-2015.rs @@ -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(); +} diff --git a/tests/ui/imports/redundant-import-issue-121915-2015.stderr b/tests/ui/imports/redundant-import-issue-121915-2015.stderr new file mode 100644 index 0000000000000..05bdccd1d49cf --- /dev/null +++ b/tests/ui/imports/redundant-import-issue-121915-2015.stderr @@ -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 + diff --git a/tests/ui/imports/redundant-import-issue-121915.edition2015.stderr b/tests/ui/imports/redundant-import-issue-121915.edition2015.stderr new file mode 100644 index 0000000000000..17e094b6b5028 --- /dev/null +++ b/tests/ui/imports/redundant-import-issue-121915.edition2015.stderr @@ -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`. diff --git a/tests/ui/imports/redundant-import-issue-121915.edition2018.stderr b/tests/ui/imports/redundant-import-issue-121915.edition2018.stderr new file mode 100644 index 0000000000000..a9aacc5360463 --- /dev/null +++ b/tests/ui/imports/redundant-import-issue-121915.edition2018.stderr @@ -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 + diff --git a/tests/ui/imports/redundant-import-issue-121915.edition2021.stderr b/tests/ui/imports/redundant-import-issue-121915.edition2021.stderr new file mode 100644 index 0000000000000..a9aacc5360463 --- /dev/null +++ b/tests/ui/imports/redundant-import-issue-121915.edition2021.stderr @@ -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 + diff --git a/tests/ui/imports/redundant-import-issue-121915.rs b/tests/ui/imports/redundant-import-issue-121915.rs new file mode 100644 index 0000000000000..6eb8c2cbd92a2 --- /dev/null +++ b/tests/ui/imports/redundant-import-issue-121915.rs @@ -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(); +}