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

Fix issues in suggesting importing extern crate paths #121226

Merged
merged 1 commit into from
Feb 28, 2024
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
12 changes: 11 additions & 1 deletion compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub struct Foo<T>(pub core::ptr::NonNull<T>);
14 changes: 14 additions & 0 deletions tests/ui/imports/import-alias-issue-121168.edition2015.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error[E0412]: cannot find type `Foo` in this scope
--> $DIR/import-alias-issue-121168.rs:11:12
|
LL | let _: Foo<i32> = 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`.
16 changes: 16 additions & 0 deletions tests/ui/imports/import-alias-issue-121168.edition2018.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0412]: cannot find type `Foo` in this scope
--> $DIR/import-alias-issue-121168.rs:11:12
|
LL | let _: Foo<i32> = 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`.
16 changes: 16 additions & 0 deletions tests/ui/imports/import-alias-issue-121168.edition2021.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0412]: cannot find type `Foo` in this scope
--> $DIR/import-alias-issue-121168.rs:11:12
|
LL | let _: Foo<i32> = 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`.
14 changes: 14 additions & 0 deletions tests/ui/imports/import-alias-issue-121168.rs
Original file line number Diff line number Diff line change
@@ -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<i32> = todo!(); //~ ERROR cannot find type `Foo` in this scope
}

fn main() {}
Loading