Skip to content

Commit 43844e3

Browse files
committed
Fix issues in suggesting importing extern crate paths
1 parent 340bb19 commit 43844e3

12 files changed

+52
-46
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,10 +1287,20 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12871287
let mut path_segments = path_segments.clone();
12881288
path_segments.push(ast::PathSegment::from_ident(ident));
12891289

1290+
let alias_import = if let NameBindingKind::Import { import, .. } =
1291+
name_binding.kind
1292+
&& let ImportKind::ExternCrate { source: Some(_), .. } = import.kind
1293+
&& import.parent_scope.expansion == parent_scope.expansion
1294+
{
1295+
true
1296+
} else {
1297+
false
1298+
};
1299+
12901300
let is_extern_crate_that_also_appears_in_prelude =
12911301
name_binding.is_extern_crate() && lookup_ident.span.at_least_rust_2018();
12921302

1293-
if !is_extern_crate_that_also_appears_in_prelude {
1303+
if !is_extern_crate_that_also_appears_in_prelude || alias_import {
12941304
// add the module to the lookup
12951305
if seen_modules.insert(module.def_id()) {
12961306
if via_import { &mut worklist_via_import } else { &mut worklist }
@@ -1378,15 +1388,19 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13781388
crate_path.push(ast::PathSegment::path_root(rustc_span::DUMMY_SP));
13791389
}
13801390
crate_path.push(ast::PathSegment::from_ident(ident));
1381-
1382-
suggestions.extend(self.lookup_import_candidates_from_module(
1391+
let suggest_imports = self.lookup_import_candidates_from_module(
13831392
lookup_ident,
13841393
namespace,
13851394
parent_scope,
13861395
crate_root,
13871396
crate_path,
13881397
&filter_fn,
1389-
));
1398+
);
1399+
for item in suggest_imports {
1400+
if !suggestions.iter().any(|sugg| sugg.did == item.did) {
1401+
suggestions.push(item);
1402+
}
1403+
}
13901404
}
13911405
}
13921406
}

tests/run-make/extern-alias/Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# ignore-cross-compile
2+
include ../tools.mk
3+
4+
all:
5+
$(RUSTC) --crate-type=rlib unwieldy_crate_name.rs -o $(TMPDIR)/libunwieldy_crate_name.rlib
6+
$(RUSTC) --crate-type rlib main.rs --extern unwieldy_crate_name=$(TMPDIR)/libunwieldy_crate_name.rlib \
7+
2>&1 | $(CGREP) "use nice_crate_name::Foo;"
8+
9+
$(RUSTC) --crate-type=rlib --edition=2021 unwieldy_crate_name.rs -o $(TMPDIR)/libunwieldy_crate_name.rlib
10+
$(RUSTC) --crate-type rlib main.rs --edition=2021 --extern unwieldy_crate_name=$(TMPDIR)/libunwieldy_crate_name.rlib \
11+
2>&1 | $(CGREP) "use crate::nice_crate_name::Foo;"

tests/run-make/extern-alias/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
extern crate unwieldy_crate_name as nice_crate_name;
2+
3+
fn use_foo_from_another_crate_without_importing_it_first() {
4+
//use nice_crate_name::Foo;
5+
let _: Foo<i32> = todo!();
6+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct Foo<T>(pub core::ptr::NonNull<T>);

tests/ui/hygiene/extern-prelude-from-opaque-fail-2018.stderr

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ LL | fn f() { my_core::mem::drop(0); }
2424
LL | a!();
2525
| ---- in this macro invocation
2626
|
27-
= help: consider importing one of these items:
27+
= help: consider importing this module:
2828
core::mem
29-
std::mem
3029
= note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info)
3130

3231
error[E0433]: failed to resolve: use of undeclared crate or module `my_core`
@@ -35,12 +34,10 @@ error[E0433]: failed to resolve: use of undeclared crate or module `my_core`
3534
LL | fn f() { my_core::mem::drop(0); }
3635
| ^^^^^^^ use of undeclared crate or module `my_core`
3736
|
38-
help: consider importing one of these items
37+
help: consider importing this module
3938
|
4039
LL + use core::mem;
4140
|
42-
LL + use std::mem;
43-
|
4441
help: if you import `mem`, refer to it directly
4542
|
4643
LL - fn f() { my_core::mem::drop(0); }

tests/ui/imports/issue-56125.stderr

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ help: consider importing one of these items instead
88
|
99
LL | use ::issue_56125::issue_56125;
1010
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
11-
LL | use ::issue_56125::last_segment::issue_56125;
12-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13-
LL | use ::issue_56125::non_last_segment::non_last_segment::issue_56125;
14-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1511
LL | use crate::m3::last_segment::issue_56125;
1612
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17-
and 1 other candidate
13+
LL | use crate::m3::non_last_segment::non_last_segment::issue_56125;
14+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1815

1916
error[E0659]: `issue_56125` is ambiguous
2017
--> $DIR/issue-56125.rs:6:9

tests/ui/mir/issue-106062.stderr

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ error[E0573]: expected type, found variant `Ok`
22
--> $DIR/issue-106062.rs:15:64
33
|
44
LL | async fn connection_handler(handler: impl Sized) -> Result<Ok, std::io::Error> {
5-
| ^^ not a type
6-
|
7-
help: try using the variant's enum
8-
|
9-
LL | async fn connection_handler(handler: impl Sized) -> Result<core::result::Result, std::io::Error> {
10-
| ~~~~~~~~~~~~~~~~~~~~
11-
LL | async fn connection_handler(handler: impl Sized) -> Result<std::result::Result, std::io::Error> {
12-
| ~~~~~~~~~~~~~~~~~~~
5+
| ^^
6+
| |
7+
| not a type
8+
| help: try using the variant's enum: `core::result::Result`
139

1410
error: aborting due to 1 previous error
1511

tests/ui/suggestions/core-std-import-order-issue-83564.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// For some reason, Rust 2018 or higher is required to reproduce the bug.
55

66
fn main() {
7-
//~^ HELP consider importing one of these items
7+
//~^ HELP consider importing this type alias
88
let _x = NonZeroU32::new(5).unwrap();
99
//~^ ERROR failed to resolve: use of undeclared type `NonZeroU32`
1010
}

tests/ui/suggestions/core-std-import-order-issue-83564.stderr

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ error[E0433]: failed to resolve: use of undeclared type `NonZeroU32`
44
LL | let _x = NonZeroU32::new(5).unwrap();
55
| ^^^^^^^^^^ use of undeclared type `NonZeroU32`
66
|
7-
help: consider importing one of these items
7+
help: consider importing this type alias
88
|
99
LL + use core::num::NonZeroU32;
1010
|
11-
LL + use std::num::NonZeroU32;
12-
|
1311

1412
error: aborting due to 1 previous error
1513

tests/ui/suggestions/crate-or-module-typo.stderr

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@ help: there is a crate or module with a similar name
3030
|
3131
LL | bar: std::cell::Cell<bool>
3232
| ~~~
33-
help: consider importing one of these items
33+
help: consider importing this module
3434
|
3535
LL + use core::cell;
3636
|
37-
LL + use std::cell;
38-
|
3937
help: if you import `cell`, refer to it directly
4038
|
4139
LL - bar: st::cell::Cell<bool>

tests/ui/suggestions/suggest-tryinto-edition-change.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,16 @@ fn test() {
1111
let _i: i16 = TryFrom::try_from(0_i32).unwrap();
1212
//~^ ERROR failed to resolve: use of undeclared type
1313
//~| NOTE use of undeclared type
14-
//~| NOTE 'std::convert::TryFrom' is included in the prelude starting in Edition 2021
1514
//~| NOTE 'core::convert::TryFrom' is included in the prelude starting in Edition 2021
1615

1716
let _i: i16 = TryInto::try_into(0_i32).unwrap();
1817
//~^ ERROR failed to resolve: use of undeclared type
1918
//~| NOTE use of undeclared type
20-
//~| NOTE 'std::convert::TryInto' is included in the prelude starting in Edition 2021
2119
//~| NOTE 'core::convert::TryInto' is included in the prelude starting in Edition 2021
2220

2321
let _v: Vec<_> = FromIterator::from_iter(&[1]);
2422
//~^ ERROR failed to resolve: use of undeclared type
2523
//~| NOTE use of undeclared type
26-
//~| NOTE 'std::iter::FromIterator' is included in the prelude starting in Edition 2021
2724
//~| NOTE 'core::iter::FromIterator' is included in the prelude starting in Edition 2021
2825
}
2926

tests/ui/suggestions/suggest-tryinto-edition-change.stderr

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,38 @@ LL | let _i: i16 = TryFrom::try_from(0_i32).unwrap();
55
| ^^^^^^^ use of undeclared type `TryFrom`
66
|
77
= note: 'core::convert::TryFrom' is included in the prelude starting in Edition 2021
8-
= note: 'std::convert::TryFrom' is included in the prelude starting in Edition 2021
9-
help: consider importing one of these items
8+
help: consider importing this trait
109
|
1110
LL + use core::convert::TryFrom;
1211
|
13-
LL + use std::convert::TryFrom;
14-
|
1512

1613
error[E0433]: failed to resolve: use of undeclared type `TryInto`
17-
--> $DIR/suggest-tryinto-edition-change.rs:17:19
14+
--> $DIR/suggest-tryinto-edition-change.rs:16:19
1815
|
1916
LL | let _i: i16 = TryInto::try_into(0_i32).unwrap();
2017
| ^^^^^^^ use of undeclared type `TryInto`
2118
|
2219
= note: 'core::convert::TryInto' is included in the prelude starting in Edition 2021
23-
= note: 'std::convert::TryInto' is included in the prelude starting in Edition 2021
24-
help: consider importing one of these items
20+
help: consider importing this trait
2521
|
2622
LL + use core::convert::TryInto;
2723
|
28-
LL + use std::convert::TryInto;
29-
|
3024

3125
error[E0433]: failed to resolve: use of undeclared type `FromIterator`
32-
--> $DIR/suggest-tryinto-edition-change.rs:23:22
26+
--> $DIR/suggest-tryinto-edition-change.rs:21:22
3327
|
3428
LL | let _v: Vec<_> = FromIterator::from_iter(&[1]);
3529
| ^^^^^^^^^^^^ use of undeclared type `FromIterator`
3630
|
3731
= note: 'core::iter::FromIterator' is included in the prelude starting in Edition 2021
38-
= note: 'std::iter::FromIterator' is included in the prelude starting in Edition 2021
3932
help: a trait with a similar name exists
4033
|
4134
LL | let _v: Vec<_> = IntoIterator::from_iter(&[1]);
4235
| ~~~~~~~~~~~~
43-
help: consider importing one of these items
36+
help: consider importing this trait
4437
|
4538
LL + use core::iter::FromIterator;
4639
|
47-
LL + use std::iter::FromIterator;
48-
|
4940

5041
error[E0599]: no method named `try_into` found for type `i32` in the current scope
5142
--> $DIR/suggest-tryinto-edition-change.rs:6:25

0 commit comments

Comments
 (0)