Rollup merge of #138790 - xizheyin:issue-138626, r=compiler-errors

Note potential but private items in show_candidates

Closes #138626 .
We should add potential private items to give ample hints.
And for the other seemingly false positive ` pub use crate:1️⃣:Foo;` should be kept because we don't know if the user wants to import other module's items or not, and therefore should be given the full option to do so.
r? compiler
This commit is contained in:
Matthias Krüger 2025-04-01 20:25:21 +02:00 committed by GitHub
commit 068594e365
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 135 additions and 14 deletions

View file

@ -1325,11 +1325,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
})
}
// If only some candidates are accessible, take just them
if !candidates.iter().all(|v: &ImportSuggestion| !v.accessible) {
candidates.retain(|x| x.accessible)
}
candidates
}
@ -1793,7 +1788,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
&import_suggestions,
Instead::Yes,
FoundUse::Yes,
DiagMode::Import { append: single_nested },
DiagMode::Import { append: single_nested, unresolved_import: false },
vec![],
"",
);
@ -2750,6 +2745,8 @@ pub(crate) enum DiagMode {
Pattern,
/// The binding is part of a use statement
Import {
/// `true` means diagnostics is for unresolved import
unresolved_import: bool,
/// `true` mean add the tips afterward for case `use a::{b,c}`,
/// rather than replacing within.
append: bool,
@ -2800,6 +2797,7 @@ fn show_candidates(
return false;
}
let mut showed = false;
let mut accessible_path_strings: Vec<PathString<'_>> = Vec::new();
let mut inaccessible_path_strings: Vec<PathString<'_>> = Vec::new();
@ -2958,8 +2956,11 @@ fn show_candidates(
append_candidates(&mut msg, accessible_path_strings);
err.help(msg);
}
true
} else if !(inaccessible_path_strings.is_empty() || matches!(mode, DiagMode::Import { .. })) {
showed = true;
}
if !inaccessible_path_strings.is_empty()
&& (!matches!(mode, DiagMode::Import { unresolved_import: false, .. }))
{
let prefix =
if let DiagMode::Pattern = mode { "you might have meant to match on " } else { "" };
if let [(name, descr, source_span, note, _)] = &inaccessible_path_strings[..] {
@ -3022,10 +3023,9 @@ fn show_candidates(
err.span_note(multi_span, msg);
}
true
} else {
false
showed = true;
}
showed
}
#[derive(Debug)]

View file

@ -734,7 +734,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
&mut diag,
Some(err.span),
candidates,
DiagMode::Import { append: false },
DiagMode::Import { append: false, unresolved_import: true },
(source != target)
.then(|| format!(" as {target}"))
.as_deref()