Skip to content

Suggest extern crate alloc when using undeclared module alloc #90507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,10 @@ impl<'a> Resolver<'a> {
err.span_label(span, label);

if let Some((suggestions, msg, applicability)) = suggestion {
if suggestions.is_empty() {
err.help(&msg);
return err;
}
err.multipart_suggestion(&msg, suggestions, applicability);
}

Expand Down
28 changes: 19 additions & 9 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2523,19 +2523,29 @@ impl<'a> Resolver<'a> {
} else {
(
format!("use of undeclared crate or module `{}`", ident),
self.find_similarly_named_module_or_crate(
ident.name,
&parent_scope.module,
)
.map(|sugg| {
(
vec![(ident.span, sugg.to_string())],
if ident.name == sym::alloc {
Some((
vec![],
String::from(
"there is a crate or module with a similar name",
"add `extern crate alloc` to use the `alloc` crate",
),
Applicability::MaybeIncorrect,
))
} else {
self.find_similarly_named_module_or_crate(
ident.name,
&parent_scope.module,
)
}),
.map(|sugg| {
(
vec![(ident.span, sugg.to_string())],
String::from(
"there is a crate or module with a similar name",
),
Applicability::MaybeIncorrect,
)
})
},
)
}
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/test/ui/suggestions/undeclared-module-alloc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// edition:2018

use alloc::rc::Rc; //~ ERROR failed to resolve: use of undeclared crate or module `alloc`

fn main() {}
11 changes: 11 additions & 0 deletions src/test/ui/suggestions/undeclared-module-alloc.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0433]: failed to resolve: use of undeclared crate or module `alloc`
--> $DIR/undeclared-module-alloc.rs:3:5
|
LL | use alloc::rc::Rc;
| ^^^^^ use of undeclared crate or module `alloc`
|
= help: add `extern crate alloc` to use the `alloc` crate

error: aborting due to previous error

For more information about this error, try `rustc --explain E0433`.