Skip to content
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

Fix false positive for typoed crate or module suggestion #90125

Closed
Closed
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
59 changes: 40 additions & 19 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Symbol> {
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::<Vec<_>>();
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::<Vec<_>>();

match find_best_match_for_name(&candidates, ident.name, None) {
Some(sugg) if sugg == ident.name => None,
sugg => sugg,
}
}
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
(
Expand Down
38 changes: 34 additions & 4 deletions src/test/ui/suggestions/crate-or-module-typo.rs
Original file line number Diff line number Diff line change
@@ -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; //~ ERROR unresolved import `my_cor`
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; //~ ERROR unresolved import `my_cor`

use my_core::mem; //~ ERROR unresolved import `my_core`

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<bool> //~ ERROR failed to resolve: use of undeclared crate or module `st`
Expand Down
73 changes: 61 additions & 12 deletions src/test/ui/suggestions/crate-or-module-typo.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
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`
|
help: there is a crate or module with a similar name
|
LL | use my_core::mem;
| ~~~~~~~
Comment on lines +7 to +10
Copy link
Member Author

@TaKO8Ki TaKO8Ki Dec 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, these lines should not be included in stderr because my_core is defined in a decl_macro a.


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`
Expand All @@ -9,25 +32,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 bas::bar;
| ^^^ use of undeclared crate or module `bas`
LL | use bb::bar;
| ^^ use of undeclared crate or module `bb`
|
help: there is a crate or module with a similar name
|
LL | use bar::bar;
LL | use b::bar;
| ~

error[E0432]: unresolved import `fooo`
--> $DIR/crate-or-module-typo.rs:41:5
|
LL | use fooo::bar;
| ^^^^ use of undeclared crate or module `fooo`
|
help: there is a crate or module with a similar name
|
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<bool>
| ^^ use of undeclared crate or module `st`
Expand All @@ -37,7 +86,7 @@ help: there is a crate or module with a similar name
LL | bar: std::cell::Cell<bool>
| ~~~

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`.