Skip to content

Commit bce5ab2

Browse files
committed
Use newtype enums instead of bool
1 parent 09f3ea1 commit bce5ab2

File tree

1 file changed

+43
-21
lines changed

1 file changed

+43
-21
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+43-21
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl<'a> Resolver<'a> {
123123
let (span, found_use) = if let Some(def_id) = def_id.as_local() {
124124
UsePlacementFinder::check(krate, self.def_id_to_node_id[def_id])
125125
} else {
126-
(None, false)
126+
(None, FoundUse::No)
127127
};
128128
if !candidates.is_empty() {
129129
show_candidates(
@@ -132,9 +132,9 @@ impl<'a> Resolver<'a> {
132132
&mut err,
133133
span,
134134
&candidates,
135-
instead,
135+
if instead { Instead::Yes } else { Instead::No },
136136
found_use,
137-
false,
137+
IsPattern::No,
138138
);
139139
} else if let Some((span, msg, sugg, appl)) = suggestion {
140140
err.span_suggestion(span, msg, sugg, appl);
@@ -702,9 +702,9 @@ impl<'a> Resolver<'a> {
702702
&mut err,
703703
Some(span),
704704
&import_suggestions,
705-
false,
706-
true,
707-
true,
705+
Instead::No,
706+
FoundUse::Yes,
707+
IsPattern::Yes,
708708
);
709709
}
710710
err
@@ -1482,9 +1482,9 @@ impl<'a> Resolver<'a> {
14821482
err,
14831483
None,
14841484
&import_suggestions,
1485-
false,
1486-
true,
1487-
false,
1485+
Instead::No,
1486+
FoundUse::Yes,
1487+
IsPattern::No,
14881488
);
14891489

14901490
if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) {
@@ -2420,6 +2420,27 @@ fn find_span_immediately_after_crate_name(
24202420
(next_left_bracket == after_second_colon, from_second_colon)
24212421
}
24222422

2423+
/// A suggestion has already been emitted, change the wording slightly to clarify that both are
2424+
/// independent options.
2425+
enum Instead {
2426+
Yes,
2427+
No,
2428+
}
2429+
2430+
/// Whether an existing place with an `use` item was found.
2431+
enum FoundUse {
2432+
Yes,
2433+
No,
2434+
}
2435+
2436+
/// Whether a binding is part of a pattern or an expression. Used for diagnostics.
2437+
enum IsPattern {
2438+
/// The binding is part of a pattern
2439+
Yes,
2440+
/// The binding is part of an expression
2441+
No,
2442+
}
2443+
24232444
/// When an entity with a given name is not available in scope, we search for
24242445
/// entities with that name in all crates. This method allows outputting the
24252446
/// results of this search in a programmer-friendly way
@@ -2430,9 +2451,9 @@ fn show_candidates(
24302451
// This is `None` if all placement locations are inside expansions
24312452
use_placement_span: Option<Span>,
24322453
candidates: &[ImportSuggestion],
2433-
instead: bool,
2434-
found_use: bool,
2435-
is_pattern: bool,
2454+
instead: Instead,
2455+
found_use: FoundUse,
2456+
is_pattern: IsPattern,
24362457
) {
24372458
if candidates.is_empty() {
24382459
return;
@@ -2465,8 +2486,8 @@ fn show_candidates(
24652486
("one of these", "items", String::new())
24662487
};
24672488

2468-
let instead = if instead { " instead" } else { "" };
2469-
let mut msg = if is_pattern {
2489+
let instead = if let Instead::Yes = instead { " instead" } else { "" };
2490+
let mut msg = if let IsPattern::Yes = is_pattern {
24702491
format!(
24712492
"if you meant to match on {}{}{}, use the full path in the pattern",
24722493
kind, instead, name
@@ -2479,7 +2500,7 @@ fn show_candidates(
24792500
err.note(note);
24802501
}
24812502

2482-
if let (true, Some(span)) = (is_pattern, use_placement_span) {
2503+
if let (IsPattern::Yes, Some(span)) = (is_pattern, use_placement_span) {
24832504
err.span_suggestions(
24842505
span,
24852506
&msg,
@@ -2490,7 +2511,7 @@ fn show_candidates(
24902511
for candidate in &mut accessible_path_strings {
24912512
// produce an additional newline to separate the new use statement
24922513
// from the directly following item.
2493-
let additional_newline = if found_use { "" } else { "\n" };
2514+
let additional_newline = if let FoundUse::Yes = found_use { "" } else { "\n" };
24942515
candidate.0 = format!("use {};\n{}", &candidate.0, additional_newline);
24952516
}
24962517

@@ -2513,15 +2534,16 @@ fn show_candidates(
25132534
} else {
25142535
assert!(!inaccessible_path_strings.is_empty());
25152536

2516-
let prefix = if is_pattern { "you might have meant to match on " } else { "" };
2537+
let prefix =
2538+
if let IsPattern::Yes = is_pattern { "you might have meant to match on " } else { "" };
25172539
if inaccessible_path_strings.len() == 1 {
25182540
let (name, descr, def_id, note) = &inaccessible_path_strings[0];
25192541
let msg = format!(
25202542
"{}{} `{}`{} exists but is inaccessible",
25212543
prefix,
25222544
descr,
25232545
name,
2524-
if is_pattern { ", which" } else { "" }
2546+
if let IsPattern::Yes = is_pattern { ", which" } else { "" }
25252547
);
25262548

25272549
if let Some(local_def_id) = def_id.and_then(|did| did.as_local()) {
@@ -2589,14 +2611,14 @@ struct UsePlacementFinder {
25892611
}
25902612

25912613
impl UsePlacementFinder {
2592-
fn check(krate: &Crate, target_module: NodeId) -> (Option<Span>, bool) {
2614+
fn check(krate: &Crate, target_module: NodeId) -> (Option<Span>, FoundUse) {
25932615
let mut finder =
25942616
UsePlacementFinder { target_module, first_legal_span: None, first_use_span: None };
25952617
finder.visit_crate(krate);
25962618
if let Some(use_span) = finder.first_use_span {
2597-
(Some(use_span), true)
2619+
(Some(use_span), FoundUse::Yes)
25982620
} else {
2599-
(finder.first_legal_span, false)
2621+
(finder.first_legal_span, FoundUse::No)
26002622
}
26012623
}
26022624
}

0 commit comments

Comments
 (0)