about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authoryukang <moorekang@gmail.com>2024-07-01 13:05:23 +0800
committeryukang <moorekang@gmail.com>2024-07-01 20:07:29 +0800
commit8cc1ed81dfd9e6eeb50d3621e0c862e9d3b4fe92 (patch)
tree078fa454570c46b951836da6b2518d293a222631 /compiler
parentad12a2a5fcf8200943a52eceda1868fd8c590449 (diff)
downloadrust-8cc1ed81dfd9e6eeb50d3621e0c862e9d3b4fe92.tar.gz
rust-8cc1ed81dfd9e6eeb50d3621e0c862e9d3b4fe92.zip
Fix import suggestion error when failed not from starting
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_resolve/src/diagnostics.rs12
1 files changed, 11 insertions, 1 deletions
diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs
index 263daa11ec3..50a4e03d233 100644
--- a/compiler/rustc_resolve/src/diagnostics.rs
+++ b/compiler/rustc_resolve/src/diagnostics.rs
@@ -1987,10 +1987,20 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
             candidates
                 .sort_by_cached_key(|c| (c.path.segments.len(), pprust::path_to_string(&c.path)));
             if let Some(candidate) = candidates.get(0) {
+                let path = {
+                    // remove the possible common prefix of the path
+                    let start_index = (0..failed_segment_idx)
+                        .find(|&i| path[i].ident != candidate.path.segments[i].ident)
+                        .unwrap_or_default();
+                    let segments = (start_index..=failed_segment_idx)
+                        .map(|s| candidate.path.segments[s].clone())
+                        .collect();
+                    Path { segments, span: Span::default(), tokens: None }
+                };
                 (
                     String::from("unresolved import"),
                     Some((
-                        vec![(ident.span, pprust::path_to_string(&candidate.path))],
+                        vec![(ident.span, pprust::path_to_string(&path))],
                         String::from("a similar path exists"),
                         Applicability::MaybeIncorrect,
                     )),