Skip to content

Commit 987797b

Browse files
authored
Rollup merge of #90507 - TaKO8Ki:suggest-extern-crate-alloc, r=jackh726
Suggest `extern crate alloc` when using undeclared module `alloc` closes #90136
2 parents 2cff30b + 6c1e194 commit 987797b

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+4
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,10 @@ impl<'a> Resolver<'a> {
420420
err.span_label(span, label);
421421

422422
if let Some((suggestions, msg, applicability)) = suggestion {
423+
if suggestions.is_empty() {
424+
err.help(&msg);
425+
return err;
426+
}
423427
err.multipart_suggestion(&msg, suggestions, applicability);
424428
}
425429

compiler/rustc_resolve/src/lib.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -2523,19 +2523,29 @@ impl<'a> Resolver<'a> {
25232523
} else {
25242524
(
25252525
format!("use of undeclared crate or module `{}`", ident),
2526-
self.find_similarly_named_module_or_crate(
2527-
ident.name,
2528-
&parent_scope.module,
2529-
)
2530-
.map(|sugg| {
2531-
(
2532-
vec![(ident.span, sugg.to_string())],
2526+
if ident.name == sym::alloc {
2527+
Some((
2528+
vec![],
25332529
String::from(
2534-
"there is a crate or module with a similar name",
2530+
"add `extern crate alloc` to use the `alloc` crate",
25352531
),
25362532
Applicability::MaybeIncorrect,
2533+
))
2534+
} else {
2535+
self.find_similarly_named_module_or_crate(
2536+
ident.name,
2537+
&parent_scope.module,
25372538
)
2538-
}),
2539+
.map(|sugg| {
2540+
(
2541+
vec![(ident.span, sugg.to_string())],
2542+
String::from(
2543+
"there is a crate or module with a similar name",
2544+
),
2545+
Applicability::MaybeIncorrect,
2546+
)
2547+
})
2548+
},
25392549
)
25402550
}
25412551
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// edition:2018
2+
3+
use alloc::rc::Rc; //~ ERROR failed to resolve: use of undeclared crate or module `alloc`
4+
5+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0433]: failed to resolve: use of undeclared crate or module `alloc`
2+
--> $DIR/undeclared-module-alloc.rs:3:5
3+
|
4+
LL | use alloc::rc::Rc;
5+
| ^^^^^ use of undeclared crate or module `alloc`
6+
|
7+
= help: add `extern crate alloc` to use the `alloc` crate
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0433`.

0 commit comments

Comments
 (0)