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

Lines changed: 12 additions & 3 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

0 commit comments

Comments
 (0)