1
Fork 0

Rollup merge of #102913 - SparrowLii:import-candidate, r=compiler-errors

unify `IsPattern` and `IsImport` enum in `show_candidates`

Follow-up of #102876
A binding cannot appear in both pattern and import at the same time, so it makes sense to unify them
r? `@compiler-errors`
This commit is contained in:
Dylan DPC 2022-10-12 11:11:26 +05:30 committed by GitHub
commit 252ce10bb0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -139,8 +139,7 @@ impl<'a> Resolver<'a> {
&candidates, &candidates,
if instead { Instead::Yes } else { Instead::No }, if instead { Instead::Yes } else { Instead::No },
found_use, found_use,
IsPattern::No, DiagnosticMode::Normal,
IsImport::No,
path, path,
); );
err.emit(); err.emit();
@ -699,8 +698,7 @@ impl<'a> Resolver<'a> {
&import_suggestions, &import_suggestions,
Instead::No, Instead::No,
FoundUse::Yes, FoundUse::Yes,
IsPattern::Yes, DiagnosticMode::Pattern,
IsImport::No,
vec![], vec![],
); );
} }
@ -1496,8 +1494,7 @@ impl<'a> Resolver<'a> {
&import_suggestions, &import_suggestions,
Instead::No, Instead::No,
FoundUse::Yes, FoundUse::Yes,
IsPattern::No, DiagnosticMode::Normal,
IsImport::No,
vec![], vec![],
); );
@ -2458,18 +2455,13 @@ enum FoundUse {
No, No,
} }
/// Whether a binding is part of a pattern or an expression. Used for diagnostics. /// Whether a binding is part of a pattern or a use statement. Used for diagnostics.
enum IsPattern { enum DiagnosticMode {
Normal,
/// The binding is part of a pattern /// The binding is part of a pattern
Yes, Pattern,
/// The binding is part of an expression /// The binding is part of a use statement
No, Import,
}
/// Whether a binding is part of a use statement. Used for diagnostics.
enum IsImport {
Yes,
No,
} }
pub(crate) fn import_candidates( pub(crate) fn import_candidates(
@ -2488,8 +2480,7 @@ pub(crate) fn import_candidates(
candidates, candidates,
Instead::Yes, Instead::Yes,
FoundUse::Yes, FoundUse::Yes,
IsPattern::No, DiagnosticMode::Import,
IsImport::Yes,
vec![], vec![],
); );
} }
@ -2506,8 +2497,7 @@ fn show_candidates(
candidates: &[ImportSuggestion], candidates: &[ImportSuggestion],
instead: Instead, instead: Instead,
found_use: FoundUse, found_use: FoundUse,
is_pattern: IsPattern, mode: DiagnosticMode,
is_import: IsImport,
path: Vec<Segment>, path: Vec<Segment>,
) { ) {
if candidates.is_empty() { if candidates.is_empty() {
@ -2542,7 +2532,7 @@ fn show_candidates(
}; };
let instead = if let Instead::Yes = instead { " instead" } else { "" }; let instead = if let Instead::Yes = instead { " instead" } else { "" };
let mut msg = if let IsPattern::Yes = is_pattern { let mut msg = if let DiagnosticMode::Pattern = mode {
format!( format!(
"if you meant to match on {}{}{}, use the full path in the pattern", "if you meant to match on {}{}{}, use the full path in the pattern",
kind, instead, name kind, instead, name
@ -2555,19 +2545,24 @@ fn show_candidates(
err.note(note); err.note(note);
} }
if let (IsPattern::Yes, Some(span)) = (is_pattern, use_placement_span) { if let Some(span) = use_placement_span {
err.span_suggestions( let add_use = match mode {
span, DiagnosticMode::Pattern => {
&msg, err.span_suggestions(
accessible_path_strings.into_iter().map(|a| a.0), span,
Applicability::MaybeIncorrect, &msg,
); accessible_path_strings.into_iter().map(|a| a.0),
} else if let Some(span) = use_placement_span { Applicability::MaybeIncorrect,
);
return;
}
DiagnosticMode::Import => "",
DiagnosticMode::Normal => "use ",
};
for candidate in &mut accessible_path_strings { for candidate in &mut accessible_path_strings {
// produce an additional newline to separate the new use statement // produce an additional newline to separate the new use statement
// from the directly following item. // from the directly following item.
let additional_newline = if let FoundUse::Yes = found_use { "" } else { "\n" }; let additional_newline = if let FoundUse::Yes = found_use { "" } else { "\n" };
let add_use = if let IsImport::Yes = is_import { "" } else { "use " };
candidate.0 = format!("{}{};\n{}", add_use, &candidate.0, additional_newline); candidate.0 = format!("{}{};\n{}", add_use, &candidate.0, additional_newline);
} }
@ -2598,11 +2593,14 @@ fn show_candidates(
err.note(&msg); err.note(&msg);
} }
} else if matches!(is_import, IsImport::No) { } else if !matches!(mode, DiagnosticMode::Import) {
assert!(!inaccessible_path_strings.is_empty()); assert!(!inaccessible_path_strings.is_empty());
let prefix = let prefix = if let DiagnosticMode::Pattern = mode {
if let IsPattern::Yes = is_pattern { "you might have meant to match on " } else { "" }; "you might have meant to match on "
} else {
""
};
if inaccessible_path_strings.len() == 1 { if inaccessible_path_strings.len() == 1 {
let (name, descr, def_id, note) = &inaccessible_path_strings[0]; let (name, descr, def_id, note) = &inaccessible_path_strings[0];
let msg = format!( let msg = format!(
@ -2610,7 +2608,7 @@ fn show_candidates(
prefix, prefix,
descr, descr,
name, name,
if let IsPattern::Yes = is_pattern { ", which" } else { "" } if let DiagnosticMode::Pattern = mode { ", which" } else { "" }
); );
if let Some(local_def_id) = def_id.and_then(|did| did.as_local()) { if let Some(local_def_id) = def_id.and_then(|did| did.as_local()) {