Skip to content

Commit 9643070

Browse files
committed
Auto merge of rust-lang#128544 - compiler-errors:perf-warn_if_unreachable, r=<try>
Check divergence value first before doing span operations in `warn_if_unreachable` It's more expensive to extract the span's desugaring first rather than check the value of the divergence enum. For some reason I inverted these checks, probably for readability, but as a consequence I regressed perf: rust-lang#128443 (comment) r? fmease
2 parents 5367673 + f850e37 commit 9643070

File tree

1 file changed

+18
-18
lines changed
  • compiler/rustc_hir_typeck/src/fn_ctxt

1 file changed

+18
-18
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4848
/// Produces warning on the given node, if the current point in the
4949
/// function is unreachable, and there hasn't been another warning.
5050
pub(crate) fn warn_if_unreachable(&self, id: HirId, span: Span, kind: &str) {
51-
// If span arose from a desugaring of `if` or `while`, then it is the condition itself,
52-
// which diverges, that we are about to lint on. This gives suboptimal diagnostics.
53-
// Instead, stop here so that the `if`- or `while`-expression's block is linted instead.
54-
if span.is_desugaring(DesugaringKind::CondTemporary) {
51+
let Diverges::Always { span: orig_span, custom_note } = self.diverges.get() else {
5552
return;
56-
}
53+
};
5754

58-
// Don't lint if the result of an async block or async function is `!`.
59-
// This does not affect the unreachable lints *within* the body.
60-
if span.is_desugaring(DesugaringKind::Async) {
61-
return;
62-
}
55+
match span.desugaring_kind() {
56+
// If span arose from a desugaring of `if` or `while`, then it is the condition
57+
// itself, which diverges, that we are about to lint on. This gives suboptimal
58+
// diagnostics. Instead, stop here so that the `if`- or `while`-expression's
59+
// block is linted instead.
60+
Some(DesugaringKind::CondTemporary) => return,
6361

64-
// Don't lint *within* the `.await` operator, since that's all just desugaring junk.
65-
// We only want to lint if there is a subsequent expression after the `.await`.
66-
if span.is_desugaring(DesugaringKind::Await) {
67-
return;
68-
}
62+
// Don't lint if the result of an async block or async function is `!`.
63+
// This does not affect the unreachable lints *within* the body.
64+
Some(DesugaringKind::Async) => return,
6965

70-
let Diverges::Always { span: orig_span, custom_note } = self.diverges.get() else {
71-
return;
72-
};
66+
// Don't lint *within* the `.await` operator, since that's all just desugaring
67+
// junk. We only want to lint if there is a subsequent expression after the
68+
// `.await` operator.
69+
Some(DesugaringKind::Await) => return,
70+
71+
_ => {}
72+
}
7373

7474
// Don't warn twice.
7575
self.diverges.set(Diverges::WarnedAlways);

0 commit comments

Comments
 (0)