From e7e1a39fa0ff1da2f408af3fac253839cbd74848 Mon Sep 17 00:00:00 2001 From: yukang Date: Thu, 22 Jun 2023 11:18:48 +0800 Subject: [PATCH 1/2] suggest importing for partial mod path in name resolving --- compiler/rustc_resolve/src/late.rs | 6 +++- .../rustc_resolve/src/late/diagnostics.rs | 30 ++++++++++++++++- .../feature-gate-extern_absolute_paths.stderr | 9 ++++++ .../extern-prelude-from-opaque-fail.stderr | 12 +++++++ .../builtin-prelude-no-accidents.stderr | 32 ++++++++++++++----- ...-param-decl-on-type-instead-of-impl.stderr | 20 +++++++++--- .../ui/resolve/export-fully-qualified.stderr | 5 +++ .../suggestions/crate-or-module-typo.stderr | 26 ++++++++++++--- 8 files changed, 120 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 9f4573ea02594..9e05d1e14a4dd 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -3556,9 +3556,13 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { _ => return Some(parent_err), }; - let (mut err, candidates) = + let (mut err, mut candidates) = this.smart_resolve_report_errors(prefix_path, path_span, PathSource::Type, None); + if candidates.is_empty() { + candidates = this.smart_resolve_partial_mod_path_errors(prefix_path, path); + } + // There are two different error messages user might receive at // this point: // - E0412 cannot find type `{}` in this scope diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 7284b33f09d8e..b859e8d4c2eda 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -4,6 +4,7 @@ use crate::late::{LifetimeBinderKind, LifetimeRes, LifetimeRibKind, LifetimeUseS use crate::{errors, path_names_to_string}; use crate::{Module, ModuleKind, ModuleOrUniformRoot}; use crate::{PathResult, PathSource, Segment}; +use rustc_hir::def::Namespace::{self, *}; use rustc_ast::visit::{FnCtxt, FnKind, LifetimeCtxt}; use rustc_ast::{ @@ -17,7 +18,6 @@ use rustc_errors::{ MultiSpan, }; use rustc_hir as hir; -use rustc_hir::def::Namespace::{self, *}; use rustc_hir::def::{self, CtorKind, CtorOf, DefKind}; use rustc_hir::def_id::{DefId, CRATE_DEF_ID}; use rustc_hir::PrimTy; @@ -221,10 +221,14 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { let suggestion = if self.current_trait_ref.is_none() && let Some((fn_kind, _)) = self.diagnostic_metadata.current_function && let Some(FnCtxt::Assoc(_)) = fn_kind.ctxt() + && let FnKind::Fn(_, _, sig, ..) = fn_kind && let Some(items) = self.diagnostic_metadata.current_impl_items && let Some(item) = items.iter().find(|i| { if let AssocItemKind::Fn(..) | AssocItemKind::Const(..) = &i.kind && i.ident.name == item_str.name + // don't suggest if the item is in Fn signature arguments + // issue #112590 + && !sig.span.contains(item_span) { debug!(?item_str.name); return true @@ -318,6 +322,30 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { } } + /// Try to suggest for a module path that cannot be resolved. + /// Such as `fmt::Debug` where `fmt` is not resolved without importing, + /// here we search with `lookup_import_candidates` for a module named `fmt` + /// with `TypeNS` as namespace. + /// + /// We need a separate function here because we won't suggest for a path with single segment + /// and we won't change `SourcePath` api `is_expected` to match `Type` with `DefKind::Mod` + pub(crate) fn smart_resolve_partial_mod_path_errors( + &mut self, + prefix_path: &[Segment], + path: &[Segment], + ) -> Vec { + if path.len() <= 1 { + return Vec::new(); + } + let ident = prefix_path.last().unwrap().ident; + self.r.lookup_import_candidates( + ident, + Namespace::TypeNS, + &self.parent_scope, + &|res: Res| matches!(res, Res::Def(DefKind::Mod, _)), + ) + } + /// Handles error reporting for `smart_resolve_path_fragment` function. /// Creates base error and amends it with one short label and possibly some longer helps/notes. pub(crate) fn smart_resolve_report_errors( diff --git a/tests/ui/feature-gates/feature-gate-extern_absolute_paths.stderr b/tests/ui/feature-gates/feature-gate-extern_absolute_paths.stderr index 3bae23a4aaa78..7de67da9b5d14 100644 --- a/tests/ui/feature-gates/feature-gate-extern_absolute_paths.stderr +++ b/tests/ui/feature-gates/feature-gate-extern_absolute_paths.stderr @@ -13,6 +13,15 @@ LL | let _: u8 = ::core::default::Default(); | ^^^^ maybe a missing crate `core`? | = help: consider adding `extern crate core` to use the `core` crate +help: consider importing this module + | +LL + use std::default; + | +help: if you import `default`, refer to it directly + | +LL - let _: u8 = ::core::default::Default(); +LL + let _: u8 = default::Default(); + | error: aborting due to 2 previous errors diff --git a/tests/ui/hygiene/extern-prelude-from-opaque-fail.stderr b/tests/ui/hygiene/extern-prelude-from-opaque-fail.stderr index f1f4caee36197..1f7df7a2e79cd 100644 --- a/tests/ui/hygiene/extern-prelude-from-opaque-fail.stderr +++ b/tests/ui/hygiene/extern-prelude-from-opaque-fail.stderr @@ -24,6 +24,8 @@ LL | fn f() { my_core::mem::drop(0); } LL | a!(); | ---- in this macro invocation | + = help: consider importing this module: + my_core::mem = note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0433]: failed to resolve: use of undeclared crate or module `my_core` @@ -31,6 +33,16 @@ error[E0433]: failed to resolve: use of undeclared crate or module `my_core` | LL | fn f() { my_core::mem::drop(0); } | ^^^^^^^ use of undeclared crate or module `my_core` + | +help: consider importing this module + | +LL + use my_core::mem; + | +help: if you import `mem`, refer to it directly + | +LL - fn f() { my_core::mem::drop(0); } +LL + fn f() { mem::drop(0); } + | error: aborting due to 4 previous errors diff --git a/tests/ui/macros/builtin-prelude-no-accidents.stderr b/tests/ui/macros/builtin-prelude-no-accidents.stderr index 8cd9a63b80893..b726e18624199 100644 --- a/tests/ui/macros/builtin-prelude-no-accidents.stderr +++ b/tests/ui/macros/builtin-prelude-no-accidents.stderr @@ -3,21 +3,37 @@ error[E0433]: failed to resolve: use of undeclared crate or module `env` | LL | env::current_dir; | ^^^ use of undeclared crate or module `env` - -error[E0433]: failed to resolve: use of undeclared crate or module `vec` - --> $DIR/builtin-prelude-no-accidents.rs:7:14 | -LL | type B = vec::Vec; - | ^^^ - | | - | use of undeclared crate or module `vec` - | help: a struct with a similar name exists (notice the capitalization): `Vec` +help: consider importing this module + | +LL + use std::env; + | error[E0433]: failed to resolve: use of undeclared crate or module `panic` --> $DIR/builtin-prelude-no-accidents.rs:6:14 | LL | type A = panic::PanicInfo; | ^^^^^ use of undeclared crate or module `panic` + | +help: consider importing this module + | +LL + use std::panic; + | + +error[E0433]: failed to resolve: use of undeclared crate or module `vec` + --> $DIR/builtin-prelude-no-accidents.rs:7:14 + | +LL | type B = vec::Vec; + | ^^^ use of undeclared crate or module `vec` + | +help: a struct with a similar name exists + | +LL | type B = Vec::Vec; + | ~~~ +help: consider importing this module + | +LL + use std::vec; + | error: aborting due to 3 previous errors diff --git a/tests/ui/parser/const-param-decl-on-type-instead-of-impl.stderr b/tests/ui/parser/const-param-decl-on-type-instead-of-impl.stderr index 96885d11ee07f..ce95633d1145c 100644 --- a/tests/ui/parser/const-param-decl-on-type-instead-of-impl.stderr +++ b/tests/ui/parser/const-param-decl-on-type-instead-of-impl.stderr @@ -21,17 +21,27 @@ error: unexpected `const` parameter declaration LL | path::path::Struct::() | ^^^^^^^^^^^^^^ expected a `const` expression, not a parameter declaration +error[E0412]: cannot find type `T` in this scope + --> $DIR/const-param-decl-on-type-instead-of-impl.rs:8:15 + | +LL | fn banana(a: >::BAR) {} + | ^ not found in this scope + error[E0433]: failed to resolve: use of undeclared crate or module `path` --> $DIR/const-param-decl-on-type-instead-of-impl.rs:12:5 | LL | path::path::Struct::() | ^^^^ use of undeclared crate or module `path` - -error[E0412]: cannot find type `T` in this scope - --> $DIR/const-param-decl-on-type-instead-of-impl.rs:8:15 | -LL | fn banana(a: >::BAR) {} - | ^ not found in this scope +help: consider importing this module + | +LL + use std::path; + | +help: if you import `path`, refer to it directly + | +LL - path::path::Struct::() +LL + path::Struct::() + | error[E0308]: mismatched types --> $DIR/const-param-decl-on-type-instead-of-impl.rs:5:17 diff --git a/tests/ui/resolve/export-fully-qualified.stderr b/tests/ui/resolve/export-fully-qualified.stderr index 7ee352e1232a7..04d62477062fa 100644 --- a/tests/ui/resolve/export-fully-qualified.stderr +++ b/tests/ui/resolve/export-fully-qualified.stderr @@ -3,6 +3,11 @@ error[E0433]: failed to resolve: use of undeclared crate or module `foo` | LL | pub fn bar() { foo::baz(); } | ^^^ use of undeclared crate or module `foo` + | +help: consider importing this module + | +LL + use foo; + | error: aborting due to previous error diff --git a/tests/ui/suggestions/crate-or-module-typo.stderr b/tests/ui/suggestions/crate-or-module-typo.stderr index 98b88b4fb9202..8b988beb5c2c2 100644 --- a/tests/ui/suggestions/crate-or-module-typo.stderr +++ b/tests/ui/suggestions/crate-or-module-typo.stderr @@ -20,6 +20,17 @@ help: there is a crate or module with a similar name LL | use bar::bar; | ~~~ +error[E0433]: failed to resolve: use of undeclared crate or module `bar` + --> $DIR/crate-or-module-typo.rs:6:20 + | +LL | pub fn bar() { bar::baz(); } + | ^^^ use of undeclared crate or module `bar` + | +help: consider importing this module + | +LL + use crate::bar; + | + error[E0433]: failed to resolve: use of undeclared crate or module `st` --> $DIR/crate-or-module-typo.rs:14:10 | @@ -30,12 +41,17 @@ help: there is a crate or module with a similar name | LL | bar: std::cell::Cell | ~~~ - -error[E0433]: failed to resolve: use of undeclared crate or module `bar` - --> $DIR/crate-or-module-typo.rs:6:20 +help: consider importing one of these items + | +LL + use core::cell; + | +LL + use std::cell; + | +help: if you import `cell`, refer to it directly + | +LL - bar: st::cell::Cell +LL + bar: cell::Cell | -LL | pub fn bar() { bar::baz(); } - | ^^^ use of undeclared crate or module `bar` error: aborting due to 4 previous errors From b26701ea7964d444f32a43710a763eb0989d9d7e Mon Sep 17 00:00:00 2001 From: yukang Date: Thu, 22 Jun 2023 11:19:05 +0800 Subject: [PATCH 2/2] add testcase for 112590 --- compiler/rustc_resolve/src/late.rs | 15 +++--- .../rustc_resolve/src/late/diagnostics.rs | 46 +++++++++++++++---- .../feature-gate-extern_absolute_paths.stderr | 9 ---- .../extern-prelude-from-opaque-fail.stderr | 12 ----- ...-param-decl-on-type-instead-of-impl.stderr | 20 ++------ .../ui/resolve/export-fully-qualified-2018.rs | 13 ++++++ .../export-fully-qualified-2018.stderr | 14 ++++++ tests/ui/resolve/export-fully-qualified.rs | 2 + .../ui/resolve/export-fully-qualified.stderr | 2 +- .../suggestions/crate-or-module-typo.stderr | 25 +++------- .../issue-112590-suggest-import.rs | 10 ++++ .../issue-112590-suggest-import.stderr | 36 +++++++++++++++ 12 files changed, 132 insertions(+), 72 deletions(-) create mode 100644 tests/ui/resolve/export-fully-qualified-2018.rs create mode 100644 tests/ui/resolve/export-fully-qualified-2018.stderr create mode 100644 tests/ui/suggestions/issue-112590-suggest-import.rs create mode 100644 tests/ui/suggestions/issue-112590-suggest-import.stderr diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 9e05d1e14a4dd..386be405a92a8 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -3499,7 +3499,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { let report_errors = |this: &mut Self, res: Option| { if this.should_report_errs() { let (err, candidates) = - this.smart_resolve_report_errors(path, path_span, source, res); + this.smart_resolve_report_errors(path, path, path_span, source, res); let def_id = this.parent_scope.module.nearest_parent_mod(); let instead = res.is_some(); @@ -3556,12 +3556,13 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { _ => return Some(parent_err), }; - let (mut err, mut candidates) = - this.smart_resolve_report_errors(prefix_path, path_span, PathSource::Type, None); - - if candidates.is_empty() { - candidates = this.smart_resolve_partial_mod_path_errors(prefix_path, path); - } + let (mut err, candidates) = this.smart_resolve_report_errors( + prefix_path, + path, + path_span, + PathSource::Type, + None, + ); // There are two different error messages user might receive at // this point: diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index b859e8d4c2eda..a551f681d5c6d 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -334,16 +334,36 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { prefix_path: &[Segment], path: &[Segment], ) -> Vec { - if path.len() <= 1 { - return Vec::new(); + let next_seg = if path.len() >= prefix_path.len() + 1 && prefix_path.len() == 1 { + path.get(prefix_path.len()) + } else { + None + }; + if let Some(segment) = prefix_path.last() && + let Some(next_seg) = next_seg { + let candidates = self.r.lookup_import_candidates( + segment.ident, + Namespace::TypeNS, + &self.parent_scope, + &|res: Res| matches!(res, Res::Def(DefKind::Mod, _)), + ); + // double check next seg is valid + candidates + .into_iter() + .filter(|candidate| { + if let Some(def_id) = candidate.did && + let Some(module) = self.r.get_module(def_id) { + self.r.resolutions(module).borrow().iter().any(|(key, _r)| { + key.ident.name == next_seg.ident.name + }) + } else { + false + } + }) + .collect::>() + } else { + Vec::new() } - let ident = prefix_path.last().unwrap().ident; - self.r.lookup_import_candidates( - ident, - Namespace::TypeNS, - &self.parent_scope, - &|res: Res| matches!(res, Res::Def(DefKind::Mod, _)), - ) } /// Handles error reporting for `smart_resolve_path_fragment` function. @@ -351,6 +371,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { pub(crate) fn smart_resolve_report_errors( &mut self, path: &[Segment], + full_path: &[Segment], span: Span, source: PathSource<'_>, res: Option, @@ -392,7 +413,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { } let (found, candidates) = - self.try_lookup_name_relaxed(&mut err, source, path, span, res, &base_error); + self.try_lookup_name_relaxed(&mut err, source, path, full_path, span, res, &base_error); if found { return (err, candidates); } @@ -498,6 +519,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { err: &mut Diagnostic, source: PathSource<'_>, path: &[Segment], + full_path: &[Segment], span: Span, res: Option, base_error: &BaseError, @@ -667,6 +689,10 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { } } + if candidates.is_empty() { + candidates = self.smart_resolve_partial_mod_path_errors(path, full_path); + } + return (false, candidates); } diff --git a/tests/ui/feature-gates/feature-gate-extern_absolute_paths.stderr b/tests/ui/feature-gates/feature-gate-extern_absolute_paths.stderr index 7de67da9b5d14..3bae23a4aaa78 100644 --- a/tests/ui/feature-gates/feature-gate-extern_absolute_paths.stderr +++ b/tests/ui/feature-gates/feature-gate-extern_absolute_paths.stderr @@ -13,15 +13,6 @@ LL | let _: u8 = ::core::default::Default(); | ^^^^ maybe a missing crate `core`? | = help: consider adding `extern crate core` to use the `core` crate -help: consider importing this module - | -LL + use std::default; - | -help: if you import `default`, refer to it directly - | -LL - let _: u8 = ::core::default::Default(); -LL + let _: u8 = default::Default(); - | error: aborting due to 2 previous errors diff --git a/tests/ui/hygiene/extern-prelude-from-opaque-fail.stderr b/tests/ui/hygiene/extern-prelude-from-opaque-fail.stderr index 1f7df7a2e79cd..f1f4caee36197 100644 --- a/tests/ui/hygiene/extern-prelude-from-opaque-fail.stderr +++ b/tests/ui/hygiene/extern-prelude-from-opaque-fail.stderr @@ -24,8 +24,6 @@ LL | fn f() { my_core::mem::drop(0); } LL | a!(); | ---- in this macro invocation | - = help: consider importing this module: - my_core::mem = note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info) error[E0433]: failed to resolve: use of undeclared crate or module `my_core` @@ -33,16 +31,6 @@ error[E0433]: failed to resolve: use of undeclared crate or module `my_core` | LL | fn f() { my_core::mem::drop(0); } | ^^^^^^^ use of undeclared crate or module `my_core` - | -help: consider importing this module - | -LL + use my_core::mem; - | -help: if you import `mem`, refer to it directly - | -LL - fn f() { my_core::mem::drop(0); } -LL + fn f() { mem::drop(0); } - | error: aborting due to 4 previous errors diff --git a/tests/ui/parser/const-param-decl-on-type-instead-of-impl.stderr b/tests/ui/parser/const-param-decl-on-type-instead-of-impl.stderr index ce95633d1145c..96885d11ee07f 100644 --- a/tests/ui/parser/const-param-decl-on-type-instead-of-impl.stderr +++ b/tests/ui/parser/const-param-decl-on-type-instead-of-impl.stderr @@ -21,27 +21,17 @@ error: unexpected `const` parameter declaration LL | path::path::Struct::() | ^^^^^^^^^^^^^^ expected a `const` expression, not a parameter declaration -error[E0412]: cannot find type `T` in this scope - --> $DIR/const-param-decl-on-type-instead-of-impl.rs:8:15 - | -LL | fn banana(a: >::BAR) {} - | ^ not found in this scope - error[E0433]: failed to resolve: use of undeclared crate or module `path` --> $DIR/const-param-decl-on-type-instead-of-impl.rs:12:5 | LL | path::path::Struct::() | ^^^^ use of undeclared crate or module `path` + +error[E0412]: cannot find type `T` in this scope + --> $DIR/const-param-decl-on-type-instead-of-impl.rs:8:15 | -help: consider importing this module - | -LL + use std::path; - | -help: if you import `path`, refer to it directly - | -LL - path::path::Struct::() -LL + path::Struct::() - | +LL | fn banana(a: >::BAR) {} + | ^ not found in this scope error[E0308]: mismatched types --> $DIR/const-param-decl-on-type-instead-of-impl.rs:5:17 diff --git a/tests/ui/resolve/export-fully-qualified-2018.rs b/tests/ui/resolve/export-fully-qualified-2018.rs new file mode 100644 index 0000000000000..afd48acb6bb35 --- /dev/null +++ b/tests/ui/resolve/export-fully-qualified-2018.rs @@ -0,0 +1,13 @@ +// edition:2018 + +// In this test baz isn't resolved when called as foo.baz even though +// it's called from inside foo. This is somewhat surprising and may +// want to change eventually. + +mod foo { + pub fn bar() { foo::baz(); } //~ ERROR failed to resolve: use of undeclared crate or module `foo` + + fn baz() { } +} + +fn main() { } diff --git a/tests/ui/resolve/export-fully-qualified-2018.stderr b/tests/ui/resolve/export-fully-qualified-2018.stderr new file mode 100644 index 0000000000000..366ffd9bb2b45 --- /dev/null +++ b/tests/ui/resolve/export-fully-qualified-2018.stderr @@ -0,0 +1,14 @@ +error[E0433]: failed to resolve: use of undeclared crate or module `foo` + --> $DIR/export-fully-qualified-2018.rs:8:20 + | +LL | pub fn bar() { foo::baz(); } + | ^^^ use of undeclared crate or module `foo` + | +help: consider importing this module + | +LL + use crate::foo; + | + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0433`. diff --git a/tests/ui/resolve/export-fully-qualified.rs b/tests/ui/resolve/export-fully-qualified.rs index 4e73a2c548884..9d4daf4cd7950 100644 --- a/tests/ui/resolve/export-fully-qualified.rs +++ b/tests/ui/resolve/export-fully-qualified.rs @@ -1,3 +1,5 @@ +// edition:2015 + // In this test baz isn't resolved when called as foo.baz even though // it's called from inside foo. This is somewhat surprising and may // want to change eventually. diff --git a/tests/ui/resolve/export-fully-qualified.stderr b/tests/ui/resolve/export-fully-qualified.stderr index 04d62477062fa..0cd516ee1b119 100644 --- a/tests/ui/resolve/export-fully-qualified.stderr +++ b/tests/ui/resolve/export-fully-qualified.stderr @@ -1,5 +1,5 @@ error[E0433]: failed to resolve: use of undeclared crate or module `foo` - --> $DIR/export-fully-qualified.rs:6:20 + --> $DIR/export-fully-qualified.rs:8:20 | LL | pub fn bar() { foo::baz(); } | ^^^ use of undeclared crate or module `foo` diff --git a/tests/ui/suggestions/crate-or-module-typo.stderr b/tests/ui/suggestions/crate-or-module-typo.stderr index 8b988beb5c2c2..5e7a7685ab04f 100644 --- a/tests/ui/suggestions/crate-or-module-typo.stderr +++ b/tests/ui/suggestions/crate-or-module-typo.stderr @@ -20,17 +20,6 @@ help: there is a crate or module with a similar name LL | use bar::bar; | ~~~ -error[E0433]: failed to resolve: use of undeclared crate or module `bar` - --> $DIR/crate-or-module-typo.rs:6:20 - | -LL | pub fn bar() { bar::baz(); } - | ^^^ use of undeclared crate or module `bar` - | -help: consider importing this module - | -LL + use crate::bar; - | - error[E0433]: failed to resolve: use of undeclared crate or module `st` --> $DIR/crate-or-module-typo.rs:14:10 | @@ -41,16 +30,16 @@ help: there is a crate or module with a similar name | LL | bar: std::cell::Cell | ~~~ -help: consider importing one of these items - | -LL + use core::cell; + +error[E0433]: failed to resolve: use of undeclared crate or module `bar` + --> $DIR/crate-or-module-typo.rs:6:20 | -LL + use std::cell; +LL | pub fn bar() { bar::baz(); } + | ^^^ use of undeclared crate or module `bar` | -help: if you import `cell`, refer to it directly +help: consider importing this module | -LL - bar: st::cell::Cell -LL + bar: cell::Cell +LL + use crate::bar; | error: aborting due to 4 previous errors diff --git a/tests/ui/suggestions/issue-112590-suggest-import.rs b/tests/ui/suggestions/issue-112590-suggest-import.rs new file mode 100644 index 0000000000000..0938814c55985 --- /dev/null +++ b/tests/ui/suggestions/issue-112590-suggest-import.rs @@ -0,0 +1,10 @@ +pub struct S; + +impl fmt::Debug for S { //~ ERROR failed to resolve: use of undeclared crate or module `fmt` + fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { //~ ERROR failed to resolve: use of undeclared crate or module `fmt` + //~^ ERROR failed to resolve: use of undeclared crate or module `fmt` + Ok(()) + } +} + +fn main() { } diff --git a/tests/ui/suggestions/issue-112590-suggest-import.stderr b/tests/ui/suggestions/issue-112590-suggest-import.stderr new file mode 100644 index 0000000000000..aeac18c16f047 --- /dev/null +++ b/tests/ui/suggestions/issue-112590-suggest-import.stderr @@ -0,0 +1,36 @@ +error[E0433]: failed to resolve: use of undeclared crate or module `fmt` + --> $DIR/issue-112590-suggest-import.rs:3:6 + | +LL | impl fmt::Debug for S { + | ^^^ use of undeclared crate or module `fmt` + | +help: consider importing this module + | +LL + use std::fmt; + | + +error[E0433]: failed to resolve: use of undeclared crate or module `fmt` + --> $DIR/issue-112590-suggest-import.rs:4:28 + | +LL | fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { + | ^^^ use of undeclared crate or module `fmt` + | +help: consider importing this module + | +LL + use std::fmt; + | + +error[E0433]: failed to resolve: use of undeclared crate or module `fmt` + --> $DIR/issue-112590-suggest-import.rs:4:51 + | +LL | fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result { + | ^^^ use of undeclared crate or module `fmt` + | +help: consider importing this module + | +LL + use std::fmt; + | + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0433`.