diff options
| author | Yuki Okushi <jtitor@2k36.org> | 2021-11-05 10:32:39 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-05 10:32:39 +0900 |
| commit | 987797bfe17ab8b75a9195d047c32264ffef8852 (patch) | |
| tree | e911bf31530687a7d530df8d432267ec2f9df242 | |
| parent | 2cff30b17a5409e15134f8634444069fc6f21cb6 (diff) | |
| parent | 6c1e194534ab91ecdb3aa953089964c364b6b2e2 (diff) | |
| download | rust-987797bfe17ab8b75a9195d047c32264ffef8852.tar.gz rust-987797bfe17ab8b75a9195d047c32264ffef8852.zip | |
Rollup merge of #90507 - TaKO8Ki:suggest-extern-crate-alloc, r=jackh726
Suggest `extern crate alloc` when using undeclared module `alloc` closes #90136
| -rw-r--r-- | compiler/rustc_resolve/src/diagnostics.rs | 4 | ||||
| -rw-r--r-- | compiler/rustc_resolve/src/lib.rs | 28 | ||||
| -rw-r--r-- | src/test/ui/suggestions/undeclared-module-alloc.rs | 5 | ||||
| -rw-r--r-- | src/test/ui/suggestions/undeclared-module-alloc.stderr | 11 |
4 files changed, 39 insertions, 9 deletions
diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 163acebccea..ff0d76e94fd 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -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); } diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 23eb2d1aebb..5f3620b247e 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -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 { diff --git a/src/test/ui/suggestions/undeclared-module-alloc.rs b/src/test/ui/suggestions/undeclared-module-alloc.rs new file mode 100644 index 00000000000..1defa1cef28 --- /dev/null +++ b/src/test/ui/suggestions/undeclared-module-alloc.rs @@ -0,0 +1,5 @@ +// edition:2018 + +use alloc::rc::Rc; //~ ERROR failed to resolve: use of undeclared crate or module `alloc` + +fn main() {} diff --git a/src/test/ui/suggestions/undeclared-module-alloc.stderr b/src/test/ui/suggestions/undeclared-module-alloc.stderr new file mode 100644 index 00000000000..39169dfa9f7 --- /dev/null +++ b/src/test/ui/suggestions/undeclared-module-alloc.stderr @@ -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`. |
