Skip to content

Commit 336a378

Browse files
authored
Rollup merge of #128151 - estebank:missing-extern-crate, r=petrochenkov
Structured suggestion for `extern crate foo` when `foo` isn't resolved in import When encountering a name in an import that could have come from a crate that wasn't imported, use a structured suggestion to suggest `extern crate foo;` pointing at the right place in the crate. When encountering `_` in an import, do not suggest `extern crate _;`. ``` error[E0432]: unresolved import `spam` --> $DIR/import-from-missing-star-3.rs:2:9 | LL | use spam::*; | ^^^^ maybe a missing crate `spam`? | help: consider importing the `spam` crate | LL + extern crate spam; | ```
2 parents 75dfe1e + b61570a commit 336a378

33 files changed

+192
-64
lines changed

Diff for: compiler/rustc_interface/src/passes.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,13 @@ fn resolver_for_lowering_raw<'tcx>(
544544
let arenas = Resolver::arenas();
545545
let _ = tcx.registered_tools(()); // Uses `crate_for_resolver`.
546546
let (krate, pre_configured_attrs) = tcx.crate_for_resolver(()).steal();
547-
let mut resolver = Resolver::new(tcx, &pre_configured_attrs, krate.spans.inner_span, &arenas);
547+
let mut resolver = Resolver::new(
548+
tcx,
549+
&pre_configured_attrs,
550+
krate.spans.inner_span,
551+
krate.spans.inject_use_span,
552+
&arenas,
553+
);
548554
let krate = configure_and_expand(krate, &pre_configured_attrs, &mut resolver);
549555

550556
// Make sure we don't mutate the cstore from here on.

Diff for: compiler/rustc_resolve/src/diagnostics.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -2026,14 +2026,17 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
20262026
Applicability::MaybeIncorrect,
20272027
)),
20282028
)
2029+
} else if ident.name == kw::Underscore {
2030+
(format!("`_` is not a valid crate or module name"), None)
20292031
} else if self.tcx.sess.is_rust_2015() {
20302032
(
20312033
format!("you might be missing crate `{ident}`"),
20322034
Some((
2033-
vec![],
2034-
format!(
2035-
"consider adding `extern crate {ident}` to use the `{ident}` crate"
2036-
),
2035+
vec![(
2036+
self.current_crate_outer_attr_insert_span,
2037+
format!("extern crate {ident};\n"),
2038+
)],
2039+
format!("consider importing the `{ident}` crate"),
20372040
Applicability::MaybeIncorrect,
20382041
)),
20392042
)

Diff for: compiler/rustc_resolve/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,10 @@ pub struct Resolver<'a, 'tcx> {
11801180
/// Simplified analogue of module `resolutions` but in trait impls, excluding glob delegations.
11811181
/// Needed because glob delegations exclude explicitly defined names.
11821182
impl_binding_keys: FxHashMap<LocalDefId, FxHashSet<BindingKey>>,
1183+
1184+
/// This is the `Span` where an `extern crate foo;` suggestion would be inserted, if `foo`
1185+
/// could be a crate that wasn't imported. For diagnostics use only.
1186+
current_crate_outer_attr_insert_span: Span,
11831187
}
11841188

11851189
/// Nothing really interesting here; it just provides memory for the rest of the crate.
@@ -1342,6 +1346,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13421346
tcx: TyCtxt<'tcx>,
13431347
attrs: &[ast::Attribute],
13441348
crate_span: Span,
1349+
current_crate_outer_attr_insert_span: Span,
13451350
arenas: &'a ResolverArenas<'a>,
13461351
) -> Resolver<'a, 'tcx> {
13471352
let root_def_id = CRATE_DEF_ID.to_def_id();
@@ -1525,6 +1530,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15251530
glob_delegation_invoc_ids: Default::default(),
15261531
impl_unexpanded_invocations: Default::default(),
15271532
impl_binding_keys: Default::default(),
1533+
current_crate_outer_attr_insert_span,
15281534
};
15291535

15301536
let root_parent_scope = ParentScope::module(graph_root, &resolver);

Diff for: tests/rustdoc-ui/ice-unresolved-import-100241.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ error[E0432]: unresolved import `inner`
44
LL | pub use inner::S;
55
| ^^^^^ you might be missing crate `inner`
66
|
7-
= help: consider adding `extern crate inner` to use the `inner` crate
7+
help: consider importing the `inner` crate
8+
|
9+
LL + extern crate inner;
10+
|
811

912
error: aborting due to 1 previous error
1013

Diff for: tests/rustdoc-ui/intra-doc/unresolved-import-recovery.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ error[E0433]: failed to resolve: you might be missing crate `unresolved_crate`
44
LL | use unresolved_crate::module::Name;
55
| ^^^^^^^^^^^^^^^^ you might be missing crate `unresolved_crate`
66
|
7-
= help: consider adding `extern crate unresolved_crate` to use the `unresolved_crate` crate
7+
help: consider importing the `unresolved_crate` crate
8+
|
9+
LL + extern crate unresolved_crate;
10+
|
811

912
error: aborting due to 1 previous error
1013

Diff for: tests/rustdoc-ui/issues/issue-61732.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ error[E0433]: failed to resolve: you might be missing crate `r#mod`
44
LL | pub(in crate::r#mod) fn main() {}
55
| ^^^^^ you might be missing crate `r#mod`
66
|
7-
= help: consider adding `extern crate r#mod` to use the `r#mod` crate
7+
help: consider importing the `r#mod` crate
8+
|
9+
LL + extern crate r#mod;
10+
|
811

912
error: aborting due to 1 previous error
1013

Diff for: tests/ui/attributes/field-attributes-vis-unresolved.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@ error[E0433]: failed to resolve: you might be missing crate `nonexistent`
44
LL | pub(in nonexistent) field: u8
55
| ^^^^^^^^^^^ you might be missing crate `nonexistent`
66
|
7-
= help: consider adding `extern crate nonexistent` to use the `nonexistent` crate
7+
help: consider importing the `nonexistent` crate
8+
|
9+
LL + extern crate nonexistent;
10+
|
811

912
error[E0433]: failed to resolve: you might be missing crate `nonexistent`
1013
--> $DIR/field-attributes-vis-unresolved.rs:22:12
1114
|
1215
LL | pub(in nonexistent) u8
1316
| ^^^^^^^^^^^ you might be missing crate `nonexistent`
1417
|
15-
= help: consider adding `extern crate nonexistent` to use the `nonexistent` crate
18+
help: consider importing the `nonexistent` crate
19+
|
20+
LL + extern crate nonexistent;
21+
|
1622

1723
error: aborting due to 2 previous errors
1824

Diff for: tests/ui/error-codes/E0432.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ error[E0432]: unresolved import `something`
44
LL | use something::Foo;
55
| ^^^^^^^^^ you might be missing crate `something`
66
|
7-
= help: consider adding `extern crate something` to use the `something` crate
7+
help: consider importing the `something` crate
8+
|
9+
LL + extern crate something;
10+
|
811

912
error: aborting due to 1 previous error
1013

Diff for: tests/ui/imports/import-from-missing-star-2.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ error[E0432]: unresolved import `spam`
44
LL | use spam::*;
55
| ^^^^ you might be missing crate `spam`
66
|
7-
= help: consider adding `extern crate spam` to use the `spam` crate
7+
help: consider importing the `spam` crate
8+
|
9+
LL + extern crate spam;
10+
|
811

912
error: aborting due to 1 previous error
1013

Diff for: tests/ui/imports/import-from-missing-star-3.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,21 @@ error[E0432]: unresolved import `spam`
44
LL | use spam::*;
55
| ^^^^ you might be missing crate `spam`
66
|
7-
= help: consider adding `extern crate spam` to use the `spam` crate
7+
help: consider importing the `spam` crate
8+
|
9+
LL + extern crate spam;
10+
|
811

912
error[E0432]: unresolved import `spam`
1013
--> $DIR/import-from-missing-star-3.rs:27:13
1114
|
1215
LL | use spam::*;
1316
| ^^^^ you might be missing crate `spam`
1417
|
15-
= help: consider adding `extern crate spam` to use the `spam` crate
18+
help: consider importing the `spam` crate
19+
|
20+
LL + extern crate spam;
21+
|
1622

1723
error: aborting due to 2 previous errors
1824

Diff for: tests/ui/imports/import-from-missing-star.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ error[E0432]: unresolved import `spam`
44
LL | use spam::*;
55
| ^^^^ you might be missing crate `spam`
66
|
7-
= help: consider adding `extern crate spam` to use the `spam` crate
7+
help: consider importing the `spam` crate
8+
|
9+
LL + extern crate spam;
10+
|
811

912
error: aborting due to 1 previous error
1013

Diff for: tests/ui/imports/import3.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ error[E0432]: unresolved import `main`
44
LL | use main::bar;
55
| ^^^^ you might be missing crate `main`
66
|
7-
= help: consider adding `extern crate main` to use the `main` crate
7+
help: consider importing the `main` crate
8+
|
9+
LL + extern crate main;
10+
|
811

912
error: aborting due to 1 previous error
1013

Diff for: tests/ui/imports/issue-109343.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ error[E0432]: unresolved import `unresolved`
44
LL | pub use unresolved::f;
55
| ^^^^^^^^^^ you might be missing crate `unresolved`
66
|
7-
= help: consider adding `extern crate unresolved` to use the `unresolved` crate
7+
help: consider importing the `unresolved` crate
8+
|
9+
LL + extern crate unresolved;
10+
|
811

912
error: aborting due to 1 previous error
1013

Diff for: tests/ui/imports/issue-1697.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
use unresolved::*;
44
//~^ ERROR unresolved import `unresolved` [E0432]
55
//~| NOTE you might be missing crate `unresolved`
6-
//~| HELP consider adding `extern crate unresolved` to use the `unresolved` crate
6+
//~| HELP consider importing the `unresolved` crate
77

88
fn main() {}

Diff for: tests/ui/imports/issue-1697.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ error[E0432]: unresolved import `unresolved`
44
LL | use unresolved::*;
55
| ^^^^^^^^^^ you might be missing crate `unresolved`
66
|
7-
= help: consider adding `extern crate unresolved` to use the `unresolved` crate
7+
help: consider importing the `unresolved` crate
8+
|
9+
LL + extern crate unresolved;
10+
|
811

912
error: aborting due to 1 previous error
1013

Diff for: tests/ui/imports/issue-33464.stderr

+12-3
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,32 @@ error[E0432]: unresolved import `abc`
44
LL | use abc::one_el;
55
| ^^^ you might be missing crate `abc`
66
|
7-
= help: consider adding `extern crate abc` to use the `abc` crate
7+
help: consider importing the `abc` crate
8+
|
9+
LL + extern crate abc;
10+
|
811

912
error[E0432]: unresolved import `abc`
1013
--> $DIR/issue-33464.rs:5:5
1114
|
1215
LL | use abc::{a, bbb, cccccc};
1316
| ^^^ you might be missing crate `abc`
1417
|
15-
= help: consider adding `extern crate abc` to use the `abc` crate
18+
help: consider importing the `abc` crate
19+
|
20+
LL + extern crate abc;
21+
|
1622

1723
error[E0432]: unresolved import `a_very_long_name`
1824
--> $DIR/issue-33464.rs:7:5
1925
|
2026
LL | use a_very_long_name::{el, el2};
2127
| ^^^^^^^^^^^^^^^^ you might be missing crate `a_very_long_name`
2228
|
23-
= help: consider adding `extern crate a_very_long_name` to use the `a_very_long_name` crate
29+
help: consider importing the `a_very_long_name` crate
30+
|
31+
LL + extern crate a_very_long_name;
32+
|
2433

2534
error: aborting due to 3 previous errors
2635

Diff for: tests/ui/imports/issue-36881.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ error[E0432]: unresolved import `issue_36881_aux`
44
LL | use issue_36881_aux::Foo;
55
| ^^^^^^^^^^^^^^^ you might be missing crate `issue_36881_aux`
66
|
7-
= help: consider adding `extern crate issue_36881_aux` to use the `issue_36881_aux` crate
7+
help: consider importing the `issue_36881_aux` crate
8+
|
9+
LL + extern crate issue_36881_aux;
10+
|
811

912
error: aborting due to 1 previous error
1013

Diff for: tests/ui/imports/issue-37887.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ error[E0432]: unresolved import `test`
44
LL | use test::*;
55
| ^^^^ you might be missing crate `test`
66
|
7-
= help: consider adding `extern crate test` to use the `test` crate
7+
help: consider importing the `test` crate
8+
|
9+
LL + extern crate test;
10+
|
811

912
error[E0658]: use of unstable library feature 'test'
1013
--> $DIR/issue-37887.rs:2:5

Diff for: tests/ui/imports/issue-53269.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ error[E0432]: unresolved import `nonexistent_module`
44
LL | use nonexistent_module::mac;
55
| ^^^^^^^^^^^^^^^^^^ you might be missing crate `nonexistent_module`
66
|
7-
= help: consider adding `extern crate nonexistent_module` to use the `nonexistent_module` crate
7+
help: consider importing the `nonexistent_module` crate
8+
|
9+
LL + extern crate nonexistent_module;
10+
|
811

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

Diff for: tests/ui/imports/issue-55457.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ error[E0432]: unresolved import `non_existent`
1313
LL | use non_existent::non_existent;
1414
| ^^^^^^^^^^^^ you might be missing crate `non_existent`
1515
|
16-
= help: consider adding `extern crate non_existent` to use the `non_existent` crate
16+
help: consider importing the `non_existent` crate
17+
|
18+
LL + extern crate non_existent;
19+
|
1720

1821
error: aborting due to 2 previous errors
1922

Diff for: tests/ui/imports/issue-81413.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ error[E0432]: unresolved import `doesnt_exist`
44
LL | pub use doesnt_exist::*;
55
| ^^^^^^^^^^^^ you might be missing crate `doesnt_exist`
66
|
7-
= help: consider adding `extern crate doesnt_exist` to use the `doesnt_exist` crate
7+
help: consider importing the `doesnt_exist` crate
8+
|
9+
LL + extern crate doesnt_exist;
10+
|
811

912
error: aborting due to 1 previous error
1013

Diff for: tests/ui/imports/tool-mod-child.stderr

+16-4
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,43 @@ error[E0433]: failed to resolve: you might be missing crate `clippy`
44
LL | use clippy::a::b;
55
| ^^^^^^ you might be missing crate `clippy`
66
|
7-
= help: consider adding `extern crate clippy` to use the `clippy` crate
7+
help: consider importing the `clippy` crate
8+
|
9+
LL + extern crate clippy;
10+
|
811

912
error[E0432]: unresolved import `clippy`
1013
--> $DIR/tool-mod-child.rs:1:5
1114
|
1215
LL | use clippy::a;
1316
| ^^^^^^ you might be missing crate `clippy`
1417
|
15-
= help: consider adding `extern crate clippy` to use the `clippy` crate
18+
help: consider importing the `clippy` crate
19+
|
20+
LL + extern crate clippy;
21+
|
1622

1723
error[E0433]: failed to resolve: you might be missing crate `rustdoc`
1824
--> $DIR/tool-mod-child.rs:5:5
1925
|
2026
LL | use rustdoc::a::b;
2127
| ^^^^^^^ you might be missing crate `rustdoc`
2228
|
23-
= help: consider adding `extern crate rustdoc` to use the `rustdoc` crate
29+
help: consider importing the `rustdoc` crate
30+
|
31+
LL + extern crate rustdoc;
32+
|
2433

2534
error[E0432]: unresolved import `rustdoc`
2635
--> $DIR/tool-mod-child.rs:4:5
2736
|
2837
LL | use rustdoc::a;
2938
| ^^^^^^^ you might be missing crate `rustdoc`
3039
|
31-
= help: consider adding `extern crate rustdoc` to use the `rustdoc` crate
40+
help: consider importing the `rustdoc` crate
41+
|
42+
LL + extern crate rustdoc;
43+
|
3244

3345
error: aborting due to 4 previous errors
3446

0 commit comments

Comments
 (0)