about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-07-02 17:47:49 +0200
committerGitHub <noreply@github.com>2024-07-02 17:47:49 +0200
commit4d2a04914e61c5d35e937f45961aa5e91f829f83 (patch)
tree66ea05f1285e8c0aee89d8c5e5abf5c6b9d713a6 /compiler
parent33b02385866ecf5d066a808099f79676d4581a06 (diff)
parent8cc1ed81dfd9e6eeb50d3621e0c862e9d3b4fe92 (diff)
downloadrust-4d2a04914e61c5d35e937f45961aa5e91f829f83.tar.gz
rust-4d2a04914e61c5d35e937f45961aa5e91f829f83.zip
Rollup merge of #127203 - chenyukang:yukang-fix-120074-import, r=Nadrieril
Fix import suggestion error when path segment failed not from starting

Fixes #120074
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,
                     )),