about summary refs log tree commit diff
path: root/compiler/rustc_resolve
diff options
context:
space:
mode:
authorTakayuki Maeda <takoyaki0316@gmail.com>2021-09-29 13:55:24 +0900
committerTakayuki Maeda <takoyaki0316@gmail.com>2021-10-13 12:17:02 +0900
commitf819e6d59c41b6bb3db2810c5c8e340b2fb2a88d (patch)
tree574e3ecd2b8c3aa878307061db9de08289d8281e /compiler/rustc_resolve
parentd7539a6af09e5889ed9bcb8b49571b7a59c32e65 (diff)
downloadrust-f819e6d59c41b6bb3db2810c5c8e340b2fb2a88d.tar.gz
rust-f819e6d59c41b6bb3db2810c5c8e340b2fb2a88d.zip
suggestion for typoed crate or module
avoid suggesting the same name

sort candidates

fix a message

use `opt_def_id` instead of `def_id`

move `find_similarly_named_module_or_crate` to rustc_resolve/src/diagnostics.rs
Diffstat (limited to 'compiler/rustc_resolve')
-rw-r--r--compiler/rustc_resolve/src/diagnostics.rs28
-rw-r--r--compiler/rustc_resolve/src/lib.rs17
2 files changed, 44 insertions, 1 deletions
diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs
index dea47c25a8e..e3970038a33 100644
--- a/compiler/rustc_resolve/src/diagnostics.rs
+++ b/compiler/rustc_resolve/src/diagnostics.rs
@@ -1277,6 +1277,34 @@ impl<'a> Resolver<'a> {
 
         err.emit();
     }
+
+    crate fn find_similarly_named_module_or_crate(
+        &mut self,
+        ident: Symbol,
+        current_module: &Module<'a>,
+    ) -> Option<Symbol> {
+        let mut candidates = self
+            .extern_prelude
+            .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())
+            .collect::<Vec<_>>();
+        candidates.sort();
+        candidates.dedup();
+        match find_best_match_for_name(&candidates, ident, None) {
+            Some(sugg) if sugg == ident => None,
+            sugg => sugg,
+        }
+    }
 }
 
 impl<'a, 'b> ImportResolver<'a, 'b> {
diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs
index 3e7783033ef..9652c483686 100644
--- a/compiler/rustc_resolve/src/lib.rs
+++ b/compiler/rustc_resolve/src/lib.rs
@@ -2555,7 +2555,22 @@ impl<'a> Resolver<'a> {
 
                             (format!("use of undeclared type `{}`", ident), suggestion)
                         } else {
-                            (format!("use of undeclared crate or module `{}`", ident), None)
+                            (
+                                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())],
+                                        String::from(
+                                            "there is a crate or module with a similar name",
+                                        ),
+                                        Applicability::MaybeIncorrect,
+                                    )
+                                }),
+                            )
                         }
                     } else {
                         let parent = path[i - 1].ident.name;