Skip to content

Commit 8270a6b

Browse files
authored
Rollup merge of rust-lang#121226 - chenyukang:yukang-fix-import-alias, r=davidtwco
Fix issues in suggesting importing extern crate paths Fixes rust-lang#121168 r? ```@petrochenkov```
2 parents aaa5889 + 3bcef2d commit 8270a6b

6 files changed

+72
-1
lines changed

compiler/rustc_resolve/src/diagnostics.rs

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

1297+
let alias_import = if let NameBindingKind::Import { import, .. } =
1298+
name_binding.kind
1299+
&& let ImportKind::ExternCrate { source: Some(_), .. } = import.kind
1300+
&& import.parent_scope.expansion == parent_scope.expansion
1301+
{
1302+
true
1303+
} else {
1304+
false
1305+
};
1306+
12971307
let is_extern_crate_that_also_appears_in_prelude =
12981308
name_binding.is_extern_crate() && lookup_ident.span.at_least_rust_2018();
12991309

1300-
if !is_extern_crate_that_also_appears_in_prelude {
1310+
if !is_extern_crate_that_also_appears_in_prelude || alias_import {
13011311
// add the module to the lookup
13021312
if seen_modules.insert(module.def_id()) {
13031313
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/import-alias-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/import-alias-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 import_alias_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/import-alias-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 import_alias_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,14 @@
1+
//@ revisions: edition2015 edition2018 edition2021
2+
//@ [edition2015] edition:2015
3+
//@ [edition2018] edition:2018
4+
//@ [edition2021] edition:2021
5+
//@ compile-flags: --extern import_alias_issue_121168_extern
6+
//@ aux-build: import-alias-issue-121168-extern.rs
7+
8+
extern crate import_alias_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)