Skip to content

Commit daedae7

Browse files
authored
Rollup merge of #97264 - TaKO8Ki:suggest-extern-crate-when-failing-to-resolve-use-crate, r=estebank
Suggest `extern crate foo` when failing to resolve `use foo` closes #97095 r? ``@estebank``
2 parents 7d4cf71 + b2480a0 commit daedae7

26 files changed

+94
-8
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -1839,9 +1839,18 @@ impl<'a> Resolver<'a> {
18391839
)),
18401840
)
18411841
} else if self.session.edition() == Edition::Edition2015 {
1842-
(format!("maybe a missing crate `{}`?", ident), None)
1842+
(
1843+
format!("maybe a missing crate `{ident}`?"),
1844+
Some((
1845+
vec![],
1846+
format!(
1847+
"consider adding `extern crate {ident}` to use the `{ident}` crate"
1848+
),
1849+
Applicability::MaybeIncorrect,
1850+
)),
1851+
)
18431852
} else {
1844-
(format!("could not find `{}` in the crate root", ident), None)
1853+
(format!("could not find `{ident}` in the crate root"), None)
18451854
}
18461855
} else if i > 0 {
18471856
let parent = path[i - 1].ident.name;
@@ -1852,7 +1861,7 @@ impl<'a> Resolver<'a> {
18521861
"the list of imported crates".to_owned()
18531862
}
18541863
kw::PathRoot | kw::Crate => "the crate root".to_owned(),
1855-
_ => format!("`{}`", parent),
1864+
_ => format!("`{parent}`"),
18561865
};
18571866

18581867
let mut msg = format!("could not find `{}` in {}", ident, parent);

compiler/rustc_resolve/src/imports.rs

+4
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,10 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
475475
}
476476

477477
if let Some((suggestions, msg, applicability)) = err.suggestion {
478+
if suggestions.is_empty() {
479+
diag.help(&msg);
480+
continue;
481+
}
478482
diag.multipart_suggestion(&msg, suggestions, applicability);
479483
}
480484
}

src/test/rustdoc-ui/intra-doc/unresolved-import-recovery.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0433]: failed to resolve: maybe a missing crate `unresolved_crate`?
33
|
44
LL | use unresolved_crate::module::Name;
55
| ^^^^^^^^^^^^^^^^ maybe a missing crate `unresolved_crate`?
6+
|
7+
= help: consider adding `extern crate unresolved_crate` to use the `unresolved_crate` crate
68

79
error: Compilation failed, aborting rustdoc
810

src/test/rustdoc-ui/issue-61732.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0433]: failed to resolve: maybe a missing crate `r#mod`?
33
|
44
LL | pub(in crate::r#mod) fn main() {}
55
| ^^^^^ maybe a missing crate `r#mod`?
6+
|
7+
= help: consider adding `extern crate r#mod` to use the `r#mod` crate
68

79
error: Compilation failed, aborting rustdoc
810

src/test/ui/attributes/field-attributes-vis-unresolved.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ error[E0433]: failed to resolve: maybe a missing crate `nonexistent`?
33
|
44
LL | pub(in nonexistent) field: u8
55
| ^^^^^^^^^^^ maybe a missing crate `nonexistent`?
6+
|
7+
= help: consider adding `extern crate nonexistent` to use the `nonexistent` crate
68

79
error[E0433]: failed to resolve: maybe a missing crate `nonexistent`?
810
--> $DIR/field-attributes-vis-unresolved.rs:22:12
911
|
1012
LL | pub(in nonexistent) u8
1113
| ^^^^^^^^^^^ maybe a missing crate `nonexistent`?
14+
|
15+
= help: consider adding `extern crate nonexistent` to use the `nonexistent` crate
1216

1317
error: aborting due to 2 previous errors
1418

src/test/ui/error-codes/E0432.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0432]: unresolved import `something`
33
|
44
LL | use something::Foo;
55
| ^^^^^^^^^ maybe a missing crate `something`?
6+
|
7+
= help: consider adding `extern crate something` to use the `something` crate
68

79
error: aborting due to previous error
810

src/test/ui/feature-gates/feature-gate-extern_absolute_paths.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ error[E0432]: unresolved import `core`
33
|
44
LL | use core::default;
55
| ^^^^ maybe a missing crate `core`?
6+
|
7+
= help: consider adding `extern crate core` to use the `core` crate
68

79
error[E0433]: failed to resolve: maybe a missing crate `core`?
810
--> $DIR/feature-gate-extern_absolute_paths.rs:4:19
911
|
1012
LL | let _: u8 = ::core::default::Default();
1113
| ^^^^ maybe a missing crate `core`?
14+
|
15+
= help: consider adding `extern crate core` to use the `core` crate
1216

1317
error: aborting due to 2 previous errors
1418

src/test/ui/imports/import3.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0432]: unresolved import `main`
33
|
44
LL | use main::bar;
55
| ^^^^ maybe a missing crate `main`?
6+
|
7+
= help: consider adding `extern crate main` to use the `main` crate
68

79
error: aborting due to previous error
810

src/test/ui/imports/issue-1697.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0432]: unresolved import `unresolved`
33
|
44
LL | use unresolved::*;
55
| ^^^^^^^^^^ maybe a missing crate `unresolved`?
6+
|
7+
= help: consider adding `extern crate unresolved` to use the `unresolved` crate
68

79
error: aborting due to previous error
810

src/test/ui/imports/issue-33464.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@ error[E0432]: unresolved import `abc`
33
|
44
LL | use abc::one_el;
55
| ^^^ maybe a missing crate `abc`?
6+
|
7+
= help: consider adding `extern crate abc` to use the `abc` crate
68

79
error[E0432]: unresolved import `abc`
810
--> $DIR/issue-33464.rs:5:5
911
|
1012
LL | use abc::{a, bbb, cccccc};
1113
| ^^^ maybe a missing crate `abc`?
14+
|
15+
= help: consider adding `extern crate abc` to use the `abc` crate
1216

1317
error[E0432]: unresolved import `a_very_long_name`
1418
--> $DIR/issue-33464.rs:7:5
1519
|
1620
LL | use a_very_long_name::{el, el2};
1721
| ^^^^^^^^^^^^^^^^ maybe a missing crate `a_very_long_name`?
22+
|
23+
= help: consider adding `extern crate a_very_long_name` to use the `a_very_long_name` crate
1824

1925
error: aborting due to 3 previous errors
2026

src/test/ui/imports/issue-36881.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0432]: unresolved import `issue_36881_aux`
33
|
44
LL | use issue_36881_aux::Foo;
55
| ^^^^^^^^^^^^^^^ maybe a missing crate `issue_36881_aux`?
6+
|
7+
= help: consider adding `extern crate issue_36881_aux` to use the `issue_36881_aux` crate
68

79
error: aborting due to previous error
810

src/test/ui/imports/issue-37887.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0432]: unresolved import `libc`
33
|
44
LL | use libc::*;
55
| ^^^^ maybe a missing crate `libc`?
6+
|
7+
= help: consider adding `extern crate libc` to use the `libc` crate
68

79
error[E0658]: use of unstable library feature 'rustc_private': this crate is being loaded from the sysroot, an unstable location; did you mean to load this crate from crates.io via `Cargo.toml` instead?
810
--> $DIR/issue-37887.rs:2:5

src/test/ui/imports/issue-53269.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0432]: unresolved import `nonexistent_module`
33
|
44
LL | use nonexistent_module::mac;
55
| ^^^^^^^^^^^^^^^^^^ maybe a missing crate `nonexistent_module`?
6+
|
7+
= help: consider adding `extern crate nonexistent_module` to use the `nonexistent_module` crate
68

79
error[E0659]: `mac` is ambiguous
810
--> $DIR/issue-53269.rs:8:5

src/test/ui/imports/issue-55457.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ error[E0432]: unresolved import `non_existent`
1212
|
1313
LL | use non_existent::non_existent;
1414
| ^^^^^^^^^^^^ maybe a missing crate `non_existent`?
15+
|
16+
= help: consider adding `extern crate non_existent` to use the `non_existent` crate
1517

1618
error: cannot determine resolution for the derive macro `NonExistent`
1719
--> $DIR/issue-55457.rs:5:10

src/test/ui/imports/tool-mod-child.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,32 @@ error[E0433]: failed to resolve: maybe a missing crate `clippy`?
33
|
44
LL | use clippy::a::b;
55
| ^^^^^^ maybe a missing crate `clippy`?
6+
|
7+
= help: consider adding `extern crate clippy` to use the `clippy` crate
68

79
error[E0432]: unresolved import `clippy`
810
--> $DIR/tool-mod-child.rs:1:5
911
|
1012
LL | use clippy::a;
1113
| ^^^^^^ maybe a missing crate `clippy`?
14+
|
15+
= help: consider adding `extern crate clippy` to use the `clippy` crate
1216

1317
error[E0433]: failed to resolve: maybe a missing crate `rustdoc`?
1418
--> $DIR/tool-mod-child.rs:5:5
1519
|
1620
LL | use rustdoc::a::b;
1721
| ^^^^^^^ maybe a missing crate `rustdoc`?
22+
|
23+
= help: consider adding `extern crate rustdoc` to use the `rustdoc` crate
1824

1925
error[E0432]: unresolved import `rustdoc`
2026
--> $DIR/tool-mod-child.rs:4:5
2127
|
2228
LL | use rustdoc::a;
2329
| ^^^^^^^ maybe a missing crate `rustdoc`?
30+
|
31+
= help: consider adding `extern crate rustdoc` to use the `rustdoc` crate
2432

2533
error: aborting due to 4 previous errors
2634

src/test/ui/imports/unresolved-imports-used.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,32 @@ error[E0432]: unresolved import `foo`
1515
|
1616
LL | use foo::bar;
1717
| ^^^ maybe a missing crate `foo`?
18+
|
19+
= help: consider adding `extern crate foo` to use the `foo` crate
1820

1921
error[E0432]: unresolved import `baz`
2022
--> $DIR/unresolved-imports-used.rs:12:5
2123
|
2224
LL | use baz::*;
2325
| ^^^ maybe a missing crate `baz`?
26+
|
27+
= help: consider adding `extern crate baz` to use the `baz` crate
2428

2529
error[E0432]: unresolved import `foo2`
2630
--> $DIR/unresolved-imports-used.rs:14:5
2731
|
2832
LL | use foo2::bar2;
2933
| ^^^^ maybe a missing crate `foo2`?
34+
|
35+
= help: consider adding `extern crate foo2` to use the `foo2` crate
3036

3137
error[E0432]: unresolved import `baz2`
3238
--> $DIR/unresolved-imports-used.rs:15:5
3339
|
3440
LL | use baz2::*;
3541
| ^^^^ maybe a missing crate `baz2`?
42+
|
43+
= help: consider adding `extern crate baz2` to use the `baz2` crate
3644

3745
error[E0603]: function `quz` is private
3846
--> $DIR/unresolved-imports-used.rs:9:10

src/test/ui/keyword/extern/keyword-extern-as-identifier-use.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ error[E0432]: unresolved import `r#extern`
1414
|
1515
LL | use extern::foo;
1616
| ^^^^^^ maybe a missing crate `r#extern`?
17+
|
18+
= help: consider adding `extern crate r#extern` to use the `r#extern` crate
1719

1820
error: aborting due to 2 previous errors
1921

src/test/ui/privacy/restricted/test.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0433]: failed to resolve: maybe a missing crate `bad`?
33
|
44
LL | pub(in bad::path) mod m1 {}
55
| ^^^ maybe a missing crate `bad`?
6+
|
7+
= help: consider adding `extern crate bad` to use the `bad` crate
68

79
error[E0742]: visibilities can only be restricted to ancestor modules
810
--> $DIR/test.rs:51:12

src/test/ui/resolve/editions-crate-root-2015.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ error[E0433]: failed to resolve: maybe a missing crate `nonexistant`?
33
|
44
LL | fn global_inner(_: ::nonexistant::Foo) {
55
| ^^^^^^^^^^^ maybe a missing crate `nonexistant`?
6+
|
7+
= help: consider adding `extern crate nonexistant` to use the `nonexistant` crate
68

79
error[E0433]: failed to resolve: maybe a missing crate `nonexistant`?
810
--> $DIR/editions-crate-root-2015.rs:7:30
911
|
1012
LL | fn crate_inner(_: crate::nonexistant::Foo) {
1113
| ^^^^^^^^^^^ maybe a missing crate `nonexistant`?
14+
|
15+
= help: consider adding `extern crate nonexistant` to use the `nonexistant` crate
1216

1317
error[E0412]: cannot find type `nonexistant` in the crate root
1418
--> $DIR/editions-crate-root-2015.rs:11:25

src/test/ui/resolve/extern-prelude-fail.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ error[E0432]: unresolved import `extern_prelude`
33
|
44
LL | use extern_prelude::S;
55
| ^^^^^^^^^^^^^^ maybe a missing crate `extern_prelude`?
6+
|
7+
= help: consider adding `extern crate extern_prelude` to use the `extern_prelude` crate
68

79
error[E0433]: failed to resolve: maybe a missing crate `extern_prelude`?
810
--> $DIR/extern-prelude-fail.rs:8:15
911
|
1012
LL | let s = ::extern_prelude::S;
1113
| ^^^^^^^^^^^^^^ maybe a missing crate `extern_prelude`?
14+
|
15+
= help: consider adding `extern crate extern_prelude` to use the `extern_prelude` crate
1216

1317
error: aborting due to 2 previous errors
1418

src/test/ui/resolve/issue-82865.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0433]: failed to resolve: maybe a missing crate `x`?
33
|
44
LL | use x::y::z;
55
| ^ maybe a missing crate `x`?
6+
|
7+
= help: consider adding `extern crate x` to use the `x` crate
68

79
error[E0599]: no function or associated item named `z` found for struct `Box<_, _>` in the current scope
810
--> $DIR/issue-82865.rs:8:10

src/test/ui/resolve/resolve-bad-visibility.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@ error[E0433]: failed to resolve: maybe a missing crate `nonexistent`?
2121
|
2222
LL | pub(in nonexistent) struct G;
2323
| ^^^^^^^^^^^ maybe a missing crate `nonexistent`?
24+
|
25+
= help: consider adding `extern crate nonexistent` to use the `nonexistent` crate
2426

2527
error[E0433]: failed to resolve: maybe a missing crate `too_soon`?
2628
--> $DIR/resolve-bad-visibility.rs:8:8
2729
|
2830
LL | pub(in too_soon) struct H;
2931
| ^^^^^^^^ maybe a missing crate `too_soon`?
32+
|
33+
= help: consider adding `extern crate too_soon` to use the `too_soon` crate
3034

3135
error: aborting due to 5 previous errors
3236

src/test/ui/simd/portable-intrinsics-arent-exposed.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0433]: failed to resolve: maybe a missing crate `core`?
33
|
44
LL | use core::simd::intrinsics;
55
| ^^^^ maybe a missing crate `core`?
6+
|
7+
= help: consider adding `extern crate core` to use the `core` crate
68

79
error[E0432]: unresolved import `std::simd::intrinsics`
810
--> $DIR/portable-intrinsics-arent-exposed.rs:5:5

src/test/ui/unresolved/unresolved-asterisk-imports.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0432]: unresolved import `not_existing_crate`
33
|
44
LL | use not_existing_crate::*;
55
| ^^^^^^^^^^^^^^^^^^ maybe a missing crate `not_existing_crate`?
6+
|
7+
= help: consider adding `extern crate not_existing_crate` to use the `not_existing_crate` crate
68

79
error: aborting due to previous error
810

src/test/ui/unresolved/unresolved-import.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use foo::bar; //~ ERROR unresolved import `foo` [E0432]
22
//~^ maybe a missing crate `foo`?
3+
//~| HELP consider adding `extern crate foo` to use the `foo` crate
34

45
use bar::Baz as x; //~ ERROR unresolved import `bar::Baz` [E0432]
56
//~| no `Baz` in `bar`

src/test/ui/unresolved/unresolved-import.stderr

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ error[E0432]: unresolved import `foo`
33
|
44
LL | use foo::bar;
55
| ^^^ maybe a missing crate `foo`?
6+
|
7+
= help: consider adding `extern crate foo` to use the `foo` crate
68

79
error[E0432]: unresolved import `bar::Baz`
8-
--> $DIR/unresolved-import.rs:4:5
10+
--> $DIR/unresolved-import.rs:5:5
911
|
1012
LL | use bar::Baz as x;
1113
| ^^^^^---^^^^^
@@ -14,7 +16,7 @@ LL | use bar::Baz as x;
1416
| no `Baz` in `bar`
1517

1618
error[E0432]: unresolved import `food::baz`
17-
--> $DIR/unresolved-import.rs:9:5
19+
--> $DIR/unresolved-import.rs:10:5
1820
|
1921
LL | use food::baz;
2022
| ^^^^^^---
@@ -23,7 +25,7 @@ LL | use food::baz;
2325
| no `baz` in `food`
2426

2527
error[E0432]: unresolved import `food::beens`
26-
--> $DIR/unresolved-import.rs:14:12
28+
--> $DIR/unresolved-import.rs:15:12
2729
|
2830
LL | use food::{beens as Foo};
2931
| -----^^^^^^^
@@ -32,13 +34,13 @@ LL | use food::{beens as Foo};
3234
| help: a similar name exists in the module: `beans`
3335

3436
error[E0432]: unresolved import `MyEnum`
35-
--> $DIR/unresolved-import.rs:38:9
37+
--> $DIR/unresolved-import.rs:39:9
3638
|
3739
LL | use MyEnum::*;
3840
| ^^^^^^ help: a similar path exists: `self::MyEnum`
3941

4042
error[E0432]: unresolved import `Enum`
41-
--> $DIR/unresolved-import.rs:48:9
43+
--> $DIR/unresolved-import.rs:49:9
4244
|
4345
LL | use Enum::*;
4446
| ^^^^ help: a similar path exists: `self::Enum`

0 commit comments

Comments
 (0)