Skip to content

Commit 5f41994

Browse files
committed
Fix issues in suggesting importing extern crate paths
1 parent 43d3470 commit 5f41994

File tree

7 files changed

+73
-2
lines changed

7 files changed

+73
-2
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1290,10 +1290,20 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12901290
let mut path_segments = path_segments.clone();
12911291
path_segments.push(ast::PathSegment::from_ident(ident));
12921292

1293+
let alias_import = if let NameBindingKind::Import { import, .. } =
1294+
name_binding.kind
1295+
&& let ImportKind::ExternCrate { source: Some(_), .. } = import.kind
1296+
&& import.parent_scope.expansion == parent_scope.expansion
1297+
{
1298+
true
1299+
} else {
1300+
false
1301+
};
1302+
12931303
let is_extern_crate_that_also_appears_in_prelude =
12941304
name_binding.is_extern_crate() && lookup_ident.span.at_least_rust_2018();
12951305

1296-
if !is_extern_crate_that_also_appears_in_prelude {
1306+
if !is_extern_crate_that_also_appears_in_prelude || alias_import {
12971307
// add the module to the lookup
12981308
if seen_modules.insert(module.def_id()) {
12991309
if via_import { &mut worklist_via_import } else { &mut worklist }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct Foo<T>(pub core::ptr::NonNull<T>);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0412]: cannot find type `Foo` in this scope
2+
--> $DIR/issue-121168.rs:11:12
3+
|
4+
LL | let _: Foo<i32> = todo!();
5+
| ^^^ not found in this scope
6+
|
7+
help: consider importing this struct
8+
|
9+
LL + use nice_crate_name::Foo;
10+
|
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0412`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0412]: cannot find type `Foo` in this scope
2+
--> $DIR/issue-121168.rs:11:12
3+
|
4+
LL | let _: Foo<i32> = todo!();
5+
| ^^^ not found in this scope
6+
|
7+
help: consider importing one of these items
8+
|
9+
LL + use crate::nice_crate_name::Foo;
10+
|
11+
LL + use issue_121168_extern::Foo;
12+
|
13+
14+
error: aborting due to 1 previous error
15+
16+
For more information about this error, try `rustc --explain E0412`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0412]: cannot find type `Foo` in this scope
2+
--> $DIR/issue-121168.rs:11:12
3+
|
4+
LL | let _: Foo<i32> = todo!();
5+
| ^^^ not found in this scope
6+
|
7+
help: consider importing one of these items
8+
|
9+
LL + use crate::nice_crate_name::Foo;
10+
|
11+
LL + use issue_121168_extern::Foo;
12+
|
13+
14+
error: aborting due to 1 previous error
15+
16+
For more information about this error, try `rustc --explain E0412`.

tests/ui/imports/issue-121168.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ revisions: edition2015 edition2018 edition2021
2+
//@ [edition2015] edition:2015
3+
//@ [edition2018] edition:2018
4+
//@ [edition2021] edition:2021
5+
//@ compile-flags: --extern issue_121168_extern
6+
//@ aux-build: issue-121168-extern.rs
7+
8+
extern crate issue_121168_extern as nice_crate_name;
9+
10+
fn use_foo_from_another_crate_without_importing_it_first() {
11+
let _: Foo<i32> = todo!(); //~ ERROR cannot find type `Foo` in this scope
12+
}
13+
14+
fn main() {}

0 commit comments

Comments
 (0)