From b5caa1ee73c38240511891738d7b7b9f81d1f7c7 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Thu, 21 Oct 2021 18:30:09 +0900 Subject: [PATCH 1/2] fix false positive typoed crate or module suggestion fix false positive typoed crate or module suggestion --- compiler/rustc_resolve/src/diagnostics.rs | 59 ++++++++++------ compiler/rustc_resolve/src/lib.rs | 5 +- .../ui/suggestions/crate-or-module-typo.rs | 38 +++++++++-- .../suggestions/crate-or-module-typo.stderr | 68 +++++++++++++++---- 4 files changed, 133 insertions(+), 37 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 6a13627a56314..7964a1673e1b1 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1331,28 +1331,49 @@ impl<'a> Resolver<'a> { crate fn find_similarly_named_module_or_crate( &mut self, - ident: Symbol, - current_module: &Module<'a>, + ns: Namespace, + parent_scope: &ParentScope<'a>, + ident: Ident, ) -> Option { - let mut candidates = self - .extern_prelude + let current_module = parent_scope.module; + let candidates = self + .resolutions(current_module) + .borrow() .iter() - .map(|(ident, _)| ident.name) - .chain( - self.module_map - .iter() - .filter(|(_, module)| { - current_module.is_ancestor_of(module) && !ptr::eq(current_module, *module) - }) - .map(|(_, module)| module.kind.name()) - .flatten(), - ) - .filter(|c| !c.to_string().is_empty()) + .filter_map(|(key, res)| res.borrow().binding.map(|binding| (key, binding))) + .filter(|(_, binding)| { + matches!( + binding.res(), + Res::Def(DefKind::Mod, _) | Res::Def(DefKind::ExternCrate, _) + ) + }) + .map(|(key, binding)| (binding.is_extern_crate(), key.ident)) .collect::>(); - candidates.sort(); - candidates.dedup(); - match find_best_match_for_name(&candidates, ident, None) { - Some(sugg) if sugg == ident => None, + + let candidates = candidates + .iter() + .filter_map(|(is_extern_crate, c)| { + if *is_extern_crate { + Some(c.name) + } else { + let mut ctxt = c.span.ctxt().normalize_to_macros_2_0(); + let module = self.resolve_self(&mut ctxt, parent_scope.module); + self.resolve_ident_in_module( + ModuleOrUniformRoot::Module(module), + *c, + ns, + parent_scope, + false, + c.span, + ) + .ok() + .map(|_| c.name) + } + }) + .collect::>(); + + match find_best_match_for_name(&candidates, ident.name, None) { + Some(sugg) if sugg == ident.name => None, sugg => sugg, } } diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 909a809b7814f..fc2dae68d9a5d 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -2529,8 +2529,9 @@ impl<'a> Resolver<'a> { )) } else { self.find_similarly_named_module_or_crate( - ident.name, - &parent_scope.module, + ns, + parent_scope, + ident, ) .map(|sugg| { ( diff --git a/src/test/ui/suggestions/crate-or-module-typo.rs b/src/test/ui/suggestions/crate-or-module-typo.rs index 2471b11c61efd..20d731fed23a5 100644 --- a/src/test/ui/suggestions/crate-or-module-typo.rs +++ b/src/test/ui/suggestions/crate-or-module-typo.rs @@ -1,14 +1,44 @@ // edition:2018 -use st::cell::Cell; //~ ERROR failed to resolve: use of undeclared crate or module `st` +#![feature(decl_macro)] + +macro a() { + extern crate core as my_core; + use my_cor::mem; + mod a { + pub fn bar() {} + } +} -mod bar { - pub fn bar() { bar::baz(); } //~ ERROR failed to resolve: use of undeclared crate or module `bar` +macro_rules! b { + () => { + mod b { + pub fn bar() {} + } + } +} + +mod foo { + pub fn bar() { fooo::baz(); } //~ ERROR failed to resolve: use of undeclared crate or module `fooo` fn baz() {} } -use bas::bar; //~ ERROR unresolved import `bas` +a!(); + +b!(); + +use my_cor::mem; + +use my_core::mem; + +use aa::bar; //~ ERROR unresolved import `aa` + +use bb::bar; //~ ERROR unresolved import `bb` + +use st::cell::Cell; //~ ERROR failed to resolve: use of undeclared crate or module `st` + +use fooo::bar; //~ ERROR unresolved import `fooo` struct Foo { bar: st::cell::Cell //~ ERROR failed to resolve: use of undeclared crate or module `st` diff --git a/src/test/ui/suggestions/crate-or-module-typo.stderr b/src/test/ui/suggestions/crate-or-module-typo.stderr index e8250c9fa5ff4..f325472a75ff0 100644 --- a/src/test/ui/suggestions/crate-or-module-typo.stderr +++ b/src/test/ui/suggestions/crate-or-module-typo.stderr @@ -1,5 +1,23 @@ +error[E0432]: unresolved import `my_cor` + --> $DIR/crate-or-module-typo.rs:31:5 + | +LL | use my_cor::mem; + | ^^^^^^ use of undeclared crate or module `my_cor` + +error[E0432]: unresolved import `my_core` + --> $DIR/crate-or-module-typo.rs:33:5 + | +LL | use my_core::mem; + | ^^^^^^^ use of undeclared crate or module `my_core` + +error[E0432]: unresolved import `aa` + --> $DIR/crate-or-module-typo.rs:35:5 + | +LL | use aa::bar; + | ^^ use of undeclared crate or module `aa` + error[E0433]: failed to resolve: use of undeclared crate or module `st` - --> $DIR/crate-or-module-typo.rs:3:5 + --> $DIR/crate-or-module-typo.rs:39:5 | LL | use st::cell::Cell; | ^^ use of undeclared crate or module `st` @@ -9,25 +27,51 @@ help: there is a crate or module with a similar name LL | use std::cell::Cell; | ~~~ -error[E0432]: unresolved import `bas` - --> $DIR/crate-or-module-typo.rs:11:5 +error[E0432]: unresolved import `bb` + --> $DIR/crate-or-module-typo.rs:37:5 + | +LL | use bb::bar; + | ^^ use of undeclared crate or module `bb` + | +help: there is a crate or module with a similar name + | +LL | use b::bar; + | ~ + +error[E0432]: unresolved import `fooo` + --> $DIR/crate-or-module-typo.rs:41:5 | -LL | use bas::bar; - | ^^^ use of undeclared crate or module `bas` +LL | use fooo::bar; + | ^^^^ use of undeclared crate or module `fooo` | help: there is a crate or module with a similar name | -LL | use bar::bar; +LL | use foo::bar; | ~~~ -error[E0433]: failed to resolve: use of undeclared crate or module `bar` - --> $DIR/crate-or-module-typo.rs:6:20 +error[E0432]: unresolved import `my_cor` + --> $DIR/crate-or-module-typo.rs:7:9 + | +LL | use my_cor::mem; + | ^^^^^^ use of undeclared crate or module `my_cor` +... +LL | a!(); + | ----- in this macro invocation + | + = note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info) +help: there is a crate or module with a similar name + | +LL | use my_core::mem; + | ~~~~~~~ + +error[E0433]: failed to resolve: use of undeclared crate or module `fooo` + --> $DIR/crate-or-module-typo.rs:22:20 | -LL | pub fn bar() { bar::baz(); } - | ^^^ use of undeclared crate or module `bar` +LL | pub fn bar() { fooo::baz(); } + | ^^^^ use of undeclared crate or module `fooo` error[E0433]: failed to resolve: use of undeclared crate or module `st` - --> $DIR/crate-or-module-typo.rs:14:10 + --> $DIR/crate-or-module-typo.rs:44:10 | LL | bar: st::cell::Cell | ^^ use of undeclared crate or module `st` @@ -37,7 +81,7 @@ help: there is a crate or module with a similar name LL | bar: std::cell::Cell | ~~~ -error: aborting due to 4 previous errors +error: aborting due to 9 previous errors Some errors have detailed explanations: E0432, E0433. For more information about an error, try `rustc --explain E0432`. From eb8e8325e9d3dfb8b0593dd28381bfce18c50ba6 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Fri, 3 Dec 2021 16:12:58 +0900 Subject: [PATCH 2/2] fix ui test errors --- src/test/ui/suggestions/crate-or-module-typo.rs | 6 +++--- src/test/ui/suggestions/crate-or-module-typo.stderr | 7 ++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/test/ui/suggestions/crate-or-module-typo.rs b/src/test/ui/suggestions/crate-or-module-typo.rs index 20d731fed23a5..9459e58d4b6ec 100644 --- a/src/test/ui/suggestions/crate-or-module-typo.rs +++ b/src/test/ui/suggestions/crate-or-module-typo.rs @@ -4,7 +4,7 @@ macro a() { extern crate core as my_core; - use my_cor::mem; + use my_cor::mem; //~ ERROR unresolved import `my_cor` mod a { pub fn bar() {} } @@ -28,9 +28,9 @@ a!(); b!(); -use my_cor::mem; +use my_cor::mem; //~ ERROR unresolved import `my_cor` -use my_core::mem; +use my_core::mem; //~ ERROR unresolved import `my_core` use aa::bar; //~ ERROR unresolved import `aa` diff --git a/src/test/ui/suggestions/crate-or-module-typo.stderr b/src/test/ui/suggestions/crate-or-module-typo.stderr index f325472a75ff0..0d9ff1366b014 100644 --- a/src/test/ui/suggestions/crate-or-module-typo.stderr +++ b/src/test/ui/suggestions/crate-or-module-typo.stderr @@ -3,6 +3,11 @@ error[E0432]: unresolved import `my_cor` | LL | use my_cor::mem; | ^^^^^^ use of undeclared crate or module `my_cor` + | +help: there is a crate or module with a similar name + | +LL | use my_core::mem; + | ~~~~~~~ error[E0432]: unresolved import `my_core` --> $DIR/crate-or-module-typo.rs:33:5 @@ -56,7 +61,7 @@ LL | use my_cor::mem; | ^^^^^^ use of undeclared crate or module `my_cor` ... LL | a!(); - | ----- in this macro invocation + | ---- in this macro invocation | = note: this error originates in the macro `a` (in Nightly builds, run with -Z macro-backtrace for more info) help: there is a crate or module with a similar name