about summary refs log tree commit diff
diff options
context:
space:
mode:
authorByron Zhong <byronzhong@cs.uchicago.edu>2022-10-25 17:29:29 -0500
committerByron Zhong <byronzhong@cs.uchicago.edu>2022-10-25 22:08:41 -0500
commit775328c290599a05f42ee2fd7ed0a414847995fc (patch)
tree4e78e3affd9527ad7ba2c90f06d68c038c8fdea1
parent0b936d2da702848a4d537e745921636490e93060 (diff)
downloadrust-775328c290599a05f42ee2fd7ed0a414847995fc.tar.gz
rust-775328c290599a05f42ee2fd7ed0a414847995fc.zip
Modify check to output 'you might have meant' for indirect reference
-rw-r--r--compiler/rustc_resolve/src/late/diagnostics.rs17
1 files changed, 15 insertions, 2 deletions
diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs
index dfbd982bb7c..dea025310d3 100644
--- a/compiler/rustc_resolve/src/late/diagnostics.rs
+++ b/compiler/rustc_resolve/src/late/diagnostics.rs
@@ -691,8 +691,21 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
         let is_expected = &|res| source.is_expected(res);
         let ident_span = path.last().map_or(span, |ident| ident.ident.span);
         let typo_sugg = self.lookup_typo_candidate(path, source.namespace(), is_expected);
-        let is_local = &|res: Res| res.opt_def_id().map_or(false, |id| id.is_local());
-        if let TypoCandidate::Shadowed(res, Some(sugg_span)) = typo_sugg && is_local(res) {
+        let is_in_same_file = &|sp1, sp2| {
+            let source_map = self.r.session.source_map();
+            let file1 = source_map.span_to_filename(sp1);
+            let file2 = source_map.span_to_filename(sp2);
+            file1 == file2
+        };
+        // print 'you might have meant' if the candidate is (1) is a shadowed name with
+        // accessible definition and (2) either defined in the same crate as the typo
+        // (could be in a different file) or introduced in the same file as the typo
+        // (could belong to a different crate)
+        if let TypoCandidate::Shadowed(res, Some(sugg_span)) = typo_sugg
+            && res
+                .opt_def_id()
+                .map_or(false, |id| id.is_local() || is_in_same_file(span, sugg_span))
+        {
             err.span_label(
                 sugg_span,
                 format!("you might have meant to refer to this {}", res.descr()),