Skip to content

Commit a7f58af

Browse files
committed
unify IsPattern and IsImport enum
1 parent 0571b0a commit a7f58af

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
}
@@ -1483,8 +1481,7 @@ impl<'a> Resolver<'a> {
14831481
&import_suggestions,
14841482
Instead::No,
14851483
FoundUse::Yes,
1486-
IsPattern::No,
1487-
IsImport::No,
1484+
DiagnosticMode::Normal,
14881485
vec![],
14891486
);
14901487

@@ -2445,18 +2442,13 @@ enum FoundUse {
24452442
No,
24462443
}
24472444

2448-
/// Whether a binding is part of a pattern or an expression. Used for diagnostics.
2449-
enum IsPattern {
2445+
/// Whether a binding is part of a pattern or a use statement. Used for diagnostics.
2446+
enum DiagnosticMode {
2447+
Normal,
24502448
/// The binding is part of a pattern
2451-
Yes,
2452-
/// The binding is part of an expression
2453-
No,
2454-
}
2455-
2456-
/// Whether a binding is part of a use statement. Used for diagnostics.
2457-
enum IsImport {
2458-
Yes,
2459-
No,
2449+
Pattern,
2450+
/// The binding is part of a use statement
2451+
Import,
24602452
}
24612453

24622454
pub(crate) fn import_candidates(
@@ -2475,8 +2467,7 @@ pub(crate) fn import_candidates(
24752467
candidates,
24762468
Instead::Yes,
24772469
FoundUse::Yes,
2478-
IsPattern::No,
2479-
IsImport::Yes,
2470+
DiagnosticMode::Import,
24802471
vec![],
24812472
);
24822473
}
@@ -2493,8 +2484,7 @@ fn show_candidates(
24932484
candidates: &[ImportSuggestion],
24942485
instead: Instead,
24952486
found_use: FoundUse,
2496-
is_pattern: IsPattern,
2497-
is_import: IsImport,
2487+
mode: DiagnosticMode,
24982488
path: Vec<Segment>,
24992489
) {
25002490
if candidates.is_empty() {
@@ -2529,7 +2519,7 @@ fn show_candidates(
25292519
};
25302520

25312521
let instead = if let Instead::Yes = instead { " instead" } else { "" };
2532-
let mut msg = if let IsPattern::Yes = is_pattern {
2522+
let mut msg = if let DiagnosticMode::Pattern = mode {
25332523
format!(
25342524
"if you meant to match on {}{}{}, use the full path in the pattern",
25352525
kind, instead, name
@@ -2542,19 +2532,24 @@ fn show_candidates(
25422532
err.note(note);
25432533
}
25442534

2545-
if let (IsPattern::Yes, Some(span)) = (is_pattern, use_placement_span) {
2546-
err.span_suggestions(
2547-
span,
2548-
&msg,
2549-
accessible_path_strings.into_iter().map(|a| a.0),
2550-
Applicability::MaybeIncorrect,
2551-
);
2552-
} else if let Some(span) = use_placement_span {
2535+
if let Some(span) = use_placement_span {
2536+
let add_use = match mode {
2537+
DiagnosticMode::Pattern => {
2538+
err.span_suggestions(
2539+
span,
2540+
&msg,
2541+
accessible_path_strings.into_iter().map(|a| a.0),
2542+
Applicability::MaybeIncorrect,
2543+
);
2544+
return;
2545+
}
2546+
DiagnosticMode::Import => "",
2547+
DiagnosticMode::Normal => "use ",
2548+
};
25532549
for candidate in &mut accessible_path_strings {
25542550
// produce an additional newline to separate the new use statement
25552551
// from the directly following item.
25562552
let additional_newline = if let FoundUse::Yes = found_use { "" } else { "\n" };
2557-
let add_use = if let IsImport::Yes = is_import { "" } else { "use " };
25582553
candidate.0 = format!("{}{};\n{}", add_use, &candidate.0, additional_newline);
25592554
}
25602555

@@ -2585,19 +2580,22 @@ fn show_candidates(
25852580

25862581
err.note(&msg);
25872582
}
2588-
} else if matches!(is_import, IsImport::No) {
2583+
} else if !matches!(mode, DiagnosticMode::Import) {
25892584
assert!(!inaccessible_path_strings.is_empty());
25902585

2591-
let prefix =
2592-
if let IsPattern::Yes = is_pattern { "you might have meant to match on " } else { "" };
2586+
let prefix = if let DiagnosticMode::Pattern = mode {
2587+
"you might have meant to match on "
2588+
} else {
2589+
""
2590+
};
25932591
if inaccessible_path_strings.len() == 1 {
25942592
let (name, descr, def_id, note) = &inaccessible_path_strings[0];
25952593
let msg = format!(
25962594
"{}{} `{}`{} exists but is inaccessible",
25972595
prefix,
25982596
descr,
25992597
name,
2600-
if let IsPattern::Yes = is_pattern { ", which" } else { "" }
2598+
if let DiagnosticMode::Pattern = mode { ", which" } else { "" }
26012599
);
26022600

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

0 commit comments

Comments
 (0)