From b2480a0251465ddda9a45fc375197c6fbad4f764 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Sun, 22 May 2022 11:48:35 +0900 Subject: suggest `extern crate foo` when failing to resolve `use foo` fix ci error --- compiler/rustc_resolve/src/diagnostics.rs | 15 ++++++++++++--- compiler/rustc_resolve/src/imports.rs | 4 ++++ 2 files changed, 16 insertions(+), 3 deletions(-) (limited to 'compiler/rustc_resolve') diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 95e608da012..75eb03c46cb 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1836,9 +1836,18 @@ impl<'a> Resolver<'a> { )), ) } else if self.session.edition() == Edition::Edition2015 { - (format!("maybe a missing crate `{}`?", ident), None) + ( + format!("maybe a missing crate `{ident}`?"), + Some(( + vec![], + format!( + "consider adding `extern crate {ident}` to use the `{ident}` crate" + ), + Applicability::MaybeIncorrect, + )), + ) } else { - (format!("could not find `{}` in the crate root", ident), None) + (format!("could not find `{ident}` in the crate root"), None) } } else if i > 0 { let parent = path[i - 1].ident.name; @@ -1849,7 +1858,7 @@ impl<'a> Resolver<'a> { "the list of imported crates".to_owned() } kw::PathRoot | kw::Crate => "the crate root".to_owned(), - _ => format!("`{}`", parent), + _ => format!("`{parent}`"), }; let mut msg = format!("could not find `{}` in {}", ident, parent); diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index 3d0e2b9921d..d8695b67d83 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -475,6 +475,10 @@ impl<'a, 'b> ImportResolver<'a, 'b> { } if let Some((suggestions, msg, applicability)) = err.suggestion { + if suggestions.is_empty() { + diag.help(&msg); + continue; + } diag.multipart_suggestion(&msg, suggestions, applicability); } } -- cgit 1.4.1-3-g733a5