Skip to content

Commit 252ce10

Browse files
authored
Rollup merge of rust-lang#102913 - SparrowLii:import-candidate, r=compiler-errors
unify `IsPattern` and `IsImport` enum in `show_candidates` Follow-up of rust-lang#102876 A binding cannot appear in both pattern and import at the same time, so it makes sense to unify them r? `@compiler-errors`
2 parents c8a8e7d + a7f58af commit 252ce10

File tree

1 file changed

+33
-35
lines changed

1 file changed

+33
-35
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+33-35
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ impl<'a> Resolver<'a> {
139139
&candidates,
140140
if instead { Instead::Yes } else { Instead::No },
141141
found_use,
142-
IsPattern::No,
143-
IsImport::No,
142+
DiagnosticMode::Normal,
144143
path,
145144
);
146145
err.emit();
@@ -699,8 +698,7 @@ impl<'a> Resolver<'a> {
699698
&import_suggestions,
700699
Instead::No,
701700
FoundUse::Yes,
702-
IsPattern::Yes,
703-
IsImport::No,
701+
DiagnosticMode::Pattern,
704702
vec![],
705703
);
706704
}
@@ -1496,8 +1494,7 @@ impl<'a> Resolver<'a> {
14961494
&import_suggestions,
14971495
Instead::No,
14981496
FoundUse::Yes,
1499-
IsPattern::No,
1500-
IsImport::No,
1497+
DiagnosticMode::Normal,
15011498
vec![],
15021499
);
15031500

@@ -2458,18 +2455,13 @@ enum FoundUse {
24582455
No,
24592456
}
24602457

2461-
/// Whether a binding is part of a pattern or an expression. Used for diagnostics.
2462-
enum IsPattern {
2458+
/// Whether a binding is part of a pattern or a use statement. Used for diagnostics.
2459+
enum DiagnosticMode {
2460+
Normal,
24632461
/// The binding is part of a pattern
2464-
Yes,
2465-
/// The binding is part of an expression
2466-
No,
2467-
}
2468-
2469-
/// Whether a binding is part of a use statement. Used for diagnostics.
2470-
enum IsImport {
2471-
Yes,
2472-
No,
2462+
Pattern,
2463+
/// The binding is part of a use statement
2464+
Import,
24732465
}
24742466

24752467
pub(crate) fn import_candidates(
@@ -2488,8 +2480,7 @@ pub(crate) fn import_candidates(
24882480
candidates,
24892481
Instead::Yes,
24902482
FoundUse::Yes,
2491-
IsPattern::No,
2492-
IsImport::Yes,
2483+
DiagnosticMode::Import,
24932484
vec![],
24942485
);
24952486
}
@@ -2506,8 +2497,7 @@ fn show_candidates(
25062497
candidates: &[ImportSuggestion],
25072498
instead: Instead,
25082499
found_use: FoundUse,
2509-
is_pattern: IsPattern,
2510-
is_import: IsImport,
2500+
mode: DiagnosticMode,
25112501
path: Vec<Segment>,
25122502
) {
25132503
if candidates.is_empty() {
@@ -2542,7 +2532,7 @@ fn show_candidates(
25422532
};
25432533

25442534
let instead = if let Instead::Yes = instead { " instead" } else { "" };
2545-
let mut msg = if let IsPattern::Yes = is_pattern {
2535+
let mut msg = if let DiagnosticMode::Pattern = mode {
25462536
format!(
25472537
"if you meant to match on {}{}{}, use the full path in the pattern",
25482538
kind, instead, name
@@ -2555,19 +2545,24 @@ fn show_candidates(
25552545
err.note(note);
25562546
}
25572547

2558-
if let (IsPattern::Yes, Some(span)) = (is_pattern, use_placement_span) {
2559-
err.span_suggestions(
2560-
span,
2561-
&msg,
2562-
accessible_path_strings.into_iter().map(|a| a.0),
2563-
Applicability::MaybeIncorrect,
2564-
);
2565-
} else if let Some(span) = use_placement_span {
2548+
if let Some(span) = use_placement_span {
2549+
let add_use = match mode {
2550+
DiagnosticMode::Pattern => {
2551+
err.span_suggestions(
2552+
span,
2553+
&msg,
2554+
accessible_path_strings.into_iter().map(|a| a.0),
2555+
Applicability::MaybeIncorrect,
2556+
);
2557+
return;
2558+
}
2559+
DiagnosticMode::Import => "",
2560+
DiagnosticMode::Normal => "use ",
2561+
};
25662562
for candidate in &mut accessible_path_strings {
25672563
// produce an additional newline to separate the new use statement
25682564
// from the directly following item.
25692565
let additional_newline = if let FoundUse::Yes = found_use { "" } else { "\n" };
2570-
let add_use = if let IsImport::Yes = is_import { "" } else { "use " };
25712566
candidate.0 = format!("{}{};\n{}", add_use, &candidate.0, additional_newline);
25722567
}
25732568

@@ -2598,19 +2593,22 @@ fn show_candidates(
25982593

25992594
err.note(&msg);
26002595
}
2601-
} else if matches!(is_import, IsImport::No) {
2596+
} else if !matches!(mode, DiagnosticMode::Import) {
26022597
assert!(!inaccessible_path_strings.is_empty());
26032598

2604-
let prefix =
2605-
if let IsPattern::Yes = is_pattern { "you might have meant to match on " } else { "" };
2599+
let prefix = if let DiagnosticMode::Pattern = mode {
2600+
"you might have meant to match on "
2601+
} else {
2602+
""
2603+
};
26062604
if inaccessible_path_strings.len() == 1 {
26072605
let (name, descr, def_id, note) = &inaccessible_path_strings[0];
26082606
let msg = format!(
26092607
"{}{} `{}`{} exists but is inaccessible",
26102608
prefix,
26112609
descr,
26122610
name,
2613-
if let IsPattern::Yes = is_pattern { ", which" } else { "" }
2611+
if let DiagnosticMode::Pattern = mode { ", which" } else { "" }
26142612
);
26152613

26162614
if let Some(local_def_id) = def_id.and_then(|did| did.as_local()) {

0 commit comments

Comments
 (0)