From 3bcef2dc1b145ebe32d317e331579fd4c20e0bd8 Mon Sep 17 00:00:00 2001 From: yukang Date: Sat, 17 Feb 2024 17:54:06 +0800 Subject: [PATCH] Fix issues in suggesting importing extern crate paths --- compiler/rustc_resolve/src/diagnostics.rs | 12 +++++++++++- .../import-alias-issue-121168-extern.rs | 1 + .../import-alias-issue-121168.edition2015.stderr | 14 ++++++++++++++ .../import-alias-issue-121168.edition2018.stderr | 16 ++++++++++++++++ .../import-alias-issue-121168.edition2021.stderr | 16 ++++++++++++++++ tests/ui/imports/import-alias-issue-121168.rs | 14 ++++++++++++++ 6 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 tests/ui/imports/auxiliary/import-alias-issue-121168-extern.rs create mode 100644 tests/ui/imports/import-alias-issue-121168.edition2015.stderr create mode 100644 tests/ui/imports/import-alias-issue-121168.edition2018.stderr create mode 100644 tests/ui/imports/import-alias-issue-121168.edition2021.stderr create mode 100644 tests/ui/imports/import-alias-issue-121168.rs diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index d64a3b43aad31..0b545ce6a893f 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1290,10 +1290,20 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { let mut path_segments = path_segments.clone(); path_segments.push(ast::PathSegment::from_ident(ident)); + let alias_import = if let NameBindingKind::Import { import, .. } = + name_binding.kind + && let ImportKind::ExternCrate { source: Some(_), .. } = import.kind + && import.parent_scope.expansion == parent_scope.expansion + { + true + } else { + false + }; + let is_extern_crate_that_also_appears_in_prelude = name_binding.is_extern_crate() && lookup_ident.span.at_least_rust_2018(); - if !is_extern_crate_that_also_appears_in_prelude { + if !is_extern_crate_that_also_appears_in_prelude || alias_import { // add the module to the lookup if seen_modules.insert(module.def_id()) { if via_import { &mut worklist_via_import } else { &mut worklist } diff --git a/tests/ui/imports/auxiliary/import-alias-issue-121168-extern.rs b/tests/ui/imports/auxiliary/import-alias-issue-121168-extern.rs new file mode 100644 index 0000000000000..a665eb8eb96aa --- /dev/null +++ b/tests/ui/imports/auxiliary/import-alias-issue-121168-extern.rs @@ -0,0 +1 @@ +pub struct Foo(pub core::ptr::NonNull); diff --git a/tests/ui/imports/import-alias-issue-121168.edition2015.stderr b/tests/ui/imports/import-alias-issue-121168.edition2015.stderr new file mode 100644 index 0000000000000..47001fc1a5291 --- /dev/null +++ b/tests/ui/imports/import-alias-issue-121168.edition2015.stderr @@ -0,0 +1,14 @@ +error[E0412]: cannot find type `Foo` in this scope + --> $DIR/import-alias-issue-121168.rs:11:12 + | +LL | let _: Foo = todo!(); + | ^^^ not found in this scope + | +help: consider importing this struct + | +LL + use nice_crate_name::Foo; + | + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0412`. diff --git a/tests/ui/imports/import-alias-issue-121168.edition2018.stderr b/tests/ui/imports/import-alias-issue-121168.edition2018.stderr new file mode 100644 index 0000000000000..b61a0e3edd524 --- /dev/null +++ b/tests/ui/imports/import-alias-issue-121168.edition2018.stderr @@ -0,0 +1,16 @@ +error[E0412]: cannot find type `Foo` in this scope + --> $DIR/import-alias-issue-121168.rs:11:12 + | +LL | let _: Foo = todo!(); + | ^^^ not found in this scope + | +help: consider importing one of these items + | +LL + use crate::nice_crate_name::Foo; + | +LL + use import_alias_issue_121168_extern::Foo; + | + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0412`. diff --git a/tests/ui/imports/import-alias-issue-121168.edition2021.stderr b/tests/ui/imports/import-alias-issue-121168.edition2021.stderr new file mode 100644 index 0000000000000..b61a0e3edd524 --- /dev/null +++ b/tests/ui/imports/import-alias-issue-121168.edition2021.stderr @@ -0,0 +1,16 @@ +error[E0412]: cannot find type `Foo` in this scope + --> $DIR/import-alias-issue-121168.rs:11:12 + | +LL | let _: Foo = todo!(); + | ^^^ not found in this scope + | +help: consider importing one of these items + | +LL + use crate::nice_crate_name::Foo; + | +LL + use import_alias_issue_121168_extern::Foo; + | + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0412`. diff --git a/tests/ui/imports/import-alias-issue-121168.rs b/tests/ui/imports/import-alias-issue-121168.rs new file mode 100644 index 0000000000000..826a6765b0399 --- /dev/null +++ b/tests/ui/imports/import-alias-issue-121168.rs @@ -0,0 +1,14 @@ +//@ revisions: edition2015 edition2018 edition2021 +//@ [edition2015] edition:2015 +//@ [edition2018] edition:2018 +//@ [edition2021] edition:2021 +//@ compile-flags: --extern import_alias_issue_121168_extern +//@ aux-build: import-alias-issue-121168-extern.rs + +extern crate import_alias_issue_121168_extern as nice_crate_name; + +fn use_foo_from_another_crate_without_importing_it_first() { + let _: Foo = todo!(); //~ ERROR cannot find type `Foo` in this scope +} + +fn main() {}